aboutsummaryrefslogtreecommitdiff
path: root/src/Main.java
blob: 00f35ac3bfd169b3226fd80c17185291188d9a8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.io.File;
import java.io.IOException;

import org.jcodec.api.JCodecException;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
//import JCodec.*;


public class Main extends Application implements EventHandler <ActionEvent>{

	public static void main(String[] args) {
		
		launch(args);
	}

	
	MediaPlayer mediaPlayer, outputPlayer;
	Button play, play2, edit, preview, preview2;
	MediaView mediaView2;

	boolean sceneTwo, edited;

	//@Override //not sure what this does yet
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("Assignment 4: Video Processor");
		
		//buttons
		play = new Button("\u25B6");
		//play.setTranslateX(0-350);
		play.setTranslateY(420);
		play.setPrefSize(500, 100);
		play.setStyle("-fx-font: 24 arial;");
		
		play2 = new Button("\u25B6");
		play2.setTranslateY(420);
		play2.setPrefSize(500, 100);
		play2.setStyle("-fx-font: 24 arial;");
		
		edit = new Button("Apply Filter (this may take a while)");
		edit.setTranslateX(0-400);
		edit.setTranslateY(420);
		
		preview = new Button("Preview Output");
		preview.setTranslateX(350);
		preview.setTranslateY(420);
		
		preview2 = new Button("Back to input video");
		preview2.setTranslateX(350);
		preview2.setTranslateY(420);
		
		
		//Choosing media file and displaying the media
		File video;
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Choose File (Currently only supports mp4 files)");
		fileChooser.getExtensionFilters().addAll(new ExtensionFilter("mp4 Video Files", "*.mp4"));
		
		video = fileChooser.showOpenDialog(primaryStage);
		Media media = new Media(video.toURI().toString()); 
		//create the player
		mediaPlayer = new MediaPlayer(media);
		MediaView mediaView = new MediaView(mediaPlayer); 
		
		
		//Play and pause events for play button - explicitly declared in handle function
		play.setOnAction(this::handle);
		//Play and pause events for the play2 button, handling the outputPlayer
		play2.setOnAction(e -> {
			if(edited) {
				if(outputPlayer.getStatus() == Status.PLAYING) {
					outputPlayer.pause();
					play2.setText("\u25B6");
				}
				else {
					outputPlayer.play();
					play2.setText("II");
				}
			}
			else {
				play2.setText("There is no output to display!");
			}
		
		});
		
		//video processing event for the process video button
		edited = false;
		edit.setOnAction(e -> {
			try {
				edit.setText(getFrames.encoded);
				getFrames.editVideo(video);
				edited = true;
				edit.setText("Done!");
			} catch (IOException e1) {
				//Eclipse Auto-generated catch block
				e1.printStackTrace();
			} catch (JCodecException e1) {
				//Eclipse Auto-generated catch block
				e1.printStackTrace();
			}
		});
		
		
		
		//Layout and scene
		StackPane layout = new StackPane();
		layout.getChildren().addAll(mediaView, play, edit, preview);
		Scene scene = new Scene(layout, 1680, 1050);
		
		StackPane layout2 = new StackPane();
		Scene scene2 = new Scene(layout2, 1680, 1050); 
		//scene switching events for preview button
		preview.setOnAction(e -> {
			
			mediaPlayer.pause();
			if(edited) {
				//create the player for the output video
				File output = new File("output.mp4");
				Media outputMedia = new Media(output.toURI().toString());
				outputPlayer = new MediaPlayer(outputMedia);
				mediaView2 = new MediaView(outputPlayer);
				layout2.getChildren().add(mediaView2);
			}
			//mediaView.setMediaPlayer(outputPlayer);
			primaryStage.setScene(scene2);	
		});
		preview2.setOnAction(e -> {
			if(edited) {
				outputPlayer.pause();
			}
			
			//mediaView.setMediaPlayer(mediaPlayer);
			primaryStage.setScene(scene);
			
		});
		layout2.getChildren().addAll(play2, preview2);
		
		
		//putting the scene in the stage and showing the stage
		primaryStage.setScene(scene);
		sceneTwo = false;
		primaryStage.show();

		
		
	}

	@Override
	public void handle(ActionEvent event) {
		
		if(mediaPlayer.getStatus() == Status.PLAYING) {
			mediaPlayer.pause();
			play.setText("\u25B6");
		}
		else {
			mediaPlayer.play();
			play.setText("II");
		}	
		
	}

	

}