-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedia.java
443 lines (362 loc) · 13.8 KB
/
Media.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
import java.text.Format;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.io.*;
class Time
{
int seconds;
int minutes;
int hours;
void error(JFrame frame)
{
JOptionPane.showMessageDialog(frame,"Please enter a valid time value","Error!", JOptionPane.ERROR_MESSAGE);
}
void errorDuration(JFrame frame)
{
JOptionPane.showMessageDialog(frame,"Minimum duration is 1 second!","Error!", JOptionPane.ERROR_MESSAGE);
}
}
class UI extends JFrame implements ActionListener
{
Border blackline = BorderFactory.createLineBorder(Color.black);
File file;
String saveFile="";
Font fontCustom,style,sub;
String extension;
Format time = DateFormat.getTimeInstance(DateFormat.SHORT);
String[] tList = new String[]{" ","mp3","wav","mp4","mkv","MOV"};
private static final long serialVersionUID = -4737755530764696856L;
JFrame frame = new JFrame("Kronos");
JLabel chooseIn = new JLabel();
JLabel inputfield = new JLabel();
JButton open = new JButton("Choose");
JCheckBox trim = new JCheckBox("Trim");
JLabel from = new JLabel("From");
JFormattedTextField fromT;
JLabel to = new JLabel("To");
JFormattedTextField toT;
JLabel duration = new JLabel("Duration");
JLabel durationT = new JLabel();
JLabel format = new JLabel("Format");
JLabel source = new JLabel("Source Format");
JLabel sourceText = new JLabel();
JLabel target = new JLabel("Target Format");
JComboBox<String> targetText = new JComboBox<String>(tList);
JLabel chooseOu = new JLabel();
JLabel outputfield = new JLabel();
JButton save = new JButton("Choose");
JButton start = new JButton("Start Conversion");
JFileChooser fileOpen = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
JFileChooser fileSave = new JFileChooser();
void errorMessage(String message)
{
JOptionPane.showMessageDialog(frame,message,"Error!", JOptionPane.ERROR_MESSAGE);
}
UI()
{
try
{
InputStream is = this.getClass().getResourceAsStream("font.ttf");
fontCustom = Font.createFont(Font.TRUETYPE_FONT,is);
style = fontCustom.deriveFont(18f);
sub = fontCustom.deriveFont(16f);
}
catch(Exception e)
{
e.printStackTrace();
}
frame.setSize(600,600);
chooseIn.setBounds(10,10,300,20);
chooseIn.setText("Choose the input file");
chooseIn.setFont(style);
inputfield.setBounds(10,40,450,30);
inputfield.setOpaque(true);
inputfield.setBackground(Color.WHITE);
inputfield.setBorder(blackline);
inputfield.setFont(sub);
open.setBounds(470,40,100,30);
open.setFont(sub);
open.addActionListener(this);
trim.setBounds(10,100,100,50);
trim.addActionListener(this);
try
{
MaskFormatter mask = new MaskFormatter("##:##:##");
mask.setPlaceholderCharacter('0');
fromT = new JFormattedTextField(mask);
toT = new JFormattedTextField(mask);
}
catch(ParseException e)
{
e.printStackTrace();
}
from.setBounds(10,160,50,30);
from.setFont(sub);
fromT.setBounds(70,160,100,30);
fromT.setFont(sub);
fromT.setHorizontalAlignment(JLabel.CENTER);
fromT.setBorder(blackline);
fromT.addActionListener(this);
to.setBounds(190,160,50,30);
to.setFont(sub);
toT.setBounds(230,160,100,30);
toT.setFont(sub);
toT.setHorizontalAlignment(JLabel.CENTER);
toT.setBorder(blackline);
toT.addActionListener(this);
from.setEnabled(false);
fromT.setEnabled(false);
to.setEnabled(false);
toT.setEnabled(false);
duration.setBounds(400,160,80,30);
duration.setFont(sub);
duration.setEnabled(false);
durationT.setBounds(470,160,100,30);
durationT.setFont(sub);
durationT.setHorizontalAlignment(JLabel.CENTER);
durationT.setOpaque(true);
durationT.setBorder(blackline);
durationT.setBackground(Color.WHITE);
durationT.setText("00:00:00");
durationT.setEnabled(false);
format.setBounds(10,250,300,20);
format.setFont(style);
source.setBounds(40,300,120,20);
source.setFont(sub);
sourceText.setBounds(170,300,70,20);
sourceText.setHorizontalAlignment(JLabel.CENTER);
sourceText.setOpaque(true);
sourceText.setBorder(blackline);
sourceText.setBackground(Color.WHITE);
target.setBounds(300,300,120,20);
target.setFont(sub);
targetText.setBounds(430,300,90,20);
targetText.setBackground(Color.WHITE);
targetText.addActionListener(this);
chooseOu.setBounds(10,400,300,20);
chooseOu.setText("Choose the save location");
chooseOu.setFont(style);
outputfield.setBounds(10,430,450,30);
outputfield.setOpaque(true);
outputfield.setBackground(Color.WHITE);
outputfield.setBorder(blackline);
outputfield.setFont(sub);
save.setBounds(470,430,100,30);
save.setFont(sub);
save.addActionListener(this);
start.setBounds(200,500,200,30);
start.setFont(style);
start.setHorizontalAlignment(JButton.CENTER);
start.addActionListener(this);
frame.add(chooseIn);
frame.add(inputfield);
frame.add(open);
frame.add(trim);
frame.add(from);
frame.add(fromT);
frame.add(to);
frame.add(toT);
frame.add(duration);
frame.add(durationT);
frame.add(format);
frame.add(source);
frame.add(sourceText);
frame.add(target);
frame.add(targetText);
frame.add(chooseOu);
frame.add(outputfield);
frame.add(save);
frame.add(start);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e)
{
String filename;
if(e.getSource()==open)
{
int r = fileOpen.showOpenDialog(frame);
if (r==JFileChooser.APPROVE_OPTION)
{
filename = fileOpen.getSelectedFile().getAbsolutePath();
this.file = new File(filename);
fileSave.setCurrentDirectory(this.file.getParentFile());
inputfield.setText(filename);
int index = filename.lastIndexOf('.');
extension = filename.substring(index+1);
boolean flag=false;
int i;
for(i=0;i<tList.length;++i)
if(tList[i].compareTo(extension)==0)
{
flag=true;
break;
}
if(flag==true)
{
sourceText.setText(extension);
targetText.setSelectedIndex(i);
}
else
sourceText.setText("Unsupported Format");
}
}
else if(e.getSource()==save)
{
int r = fileSave.showSaveDialog(frame);
if(r==JFileChooser.APPROVE_OPTION)
{
saveFile = new String(fileSave.getSelectedFile().getAbsolutePath());
outputfield.setText(saveFile+"."+targetText.getSelectedItem().toString());
}
}
else if(e.getSource()==trim)
{
if(trim.isSelected())
{
from.setEnabled(true);
fromT.setEnabled(true);
to.setEnabled(true);
toT.setEnabled(true);
duration.setEnabled(true);
durationT.setEnabled(true);
}
else
{
from.setEnabled(false);
fromT.setEnabled(false);
to.setEnabled(false);
toT.setEnabled(false);
duration.setEnabled(false);
durationT.setEnabled(false);
}
}
else if(e.getSource()==fromT || e.getSource()==toT)
{
Time fromTime = new Time();
Time toTime = new Time();
Time diff = new Time();
fromTime.hours = Integer.parseInt(fromT.getText().substring(0,2));
fromTime.minutes = Integer.parseInt(fromT.getText().substring(3,5));
fromTime.seconds = Integer.parseInt(fromT.getText().substring(6,8));
toTime.hours = Integer.parseInt(toT.getText().substring(0,2));
toTime.minutes = Integer.parseInt(toT.getText().substring(3,5));
toTime.seconds = Integer.parseInt(toT.getText().substring(6,8));
if(fromTime.minutes>59 || fromTime.seconds>59 || toTime.minutes>59 || toTime.seconds>59)
diff.error(frame);
else
{
if(fromTime.seconds>toTime.seconds)
{
--toTime.minutes;
toTime.seconds+=60;
}
diff.seconds = toTime.seconds-fromTime.seconds;
if(fromTime.minutes>toTime.minutes)
{
--toTime.hours;
toTime.minutes+=60;
}
diff.minutes=toTime.minutes-fromTime.minutes;
diff.hours=toTime.hours-fromTime.hours;
if(diff.hours<0 || diff.minutes<0 || diff.seconds<0)
diff.error(frame);
else if(diff.hours==0 && diff.minutes==0 && diff.seconds==0)
diff.errorDuration(frame);
else
{
DecimalFormat formatter = new DecimalFormat("00");
String hours = formatter.format((diff.hours));
String minutes = formatter.format((diff.minutes));
String seconds = formatter.format((diff.seconds));
String durationTime = hours+":"+minutes+":"+seconds;
this.durationT.setText(durationTime);
}
}
}
else if(e.getSource()==targetText)
{
String outputExtension = targetText.getSelectedItem().toString();
int index;
String currentOutput,newOutput;
if(saveFile=="")
{
currentOutput = inputfield.getText();
index = currentOutput.lastIndexOf('.');
newOutput = currentOutput.substring(0,index)+"_edit."+outputExtension;
}
else
{
currentOutput = outputfield.getText();
index = currentOutput.lastIndexOf('.');
newOutput = currentOutput.substring(0,index)+"."+outputExtension;
}
this.outputfield.setText(newOutput);
}
else if(e.getSource()==start)
{
String ffmpegCommand;
File file = new File(outputfield.getText());
if(inputfield.getText()=="" || outputfield.getText()=="")
errorMessage("Please fill all the valid fields");
else if(trim.isSelected() && durationT.getText()=="00:00:00")
errorMessage("Duration cannot be zero");
else if(sourceText.getText()=="Unsupported Format" || sourceText.getText()==" ")
errorMessage("Please choose a valid file");
else if(targetText.getSelectedIndex()==0)
errorMessage("Please choose a valid target format");
else if(sourceText.getText().compareTo(targetText.getSelectedItem().toString())==0)
errorMessage("Source format and target format cannot be the same");
else if(file.exists())
errorMessage("File already exists!\nPlease enter a valid file name");
else
{
if(trim.isSelected())
ffmpegCommand = "ffmpeg -i \""+inputfield.getText()+"\" -ss "+fromT.getText()+" -to "+toT.getText()+" \""+outputfield.getText()+"\"";
else
ffmpegCommand = "ffmpeg -i \""+inputfield.getText()+"\" \""+outputfield.getText()+"\"";
System.out.println(ffmpegCommand);
try
{
Process process = Runtime.getRuntime().exec(ffmpegCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line="";
while ((line = reader.readLine ()) != null)
System.out.println(line);
int exitCode = process.waitFor();
if(exitCode==0)
{
System.out.println("Process Complete");
JOptionPane.showMessageDialog(frame,"Conversion Complete");
}
else
{
System.out.println("\nExited with error code : " + exitCode);
throw new RuntimeException();
}
}
catch(Exception ex)
{
errorMessage("An Exception occurred!\nPlease try again");
ex.printStackTrace();
}
}
}
}
}
public class Media
{
public static void main(String[] args)
{
new UI();
}
}