-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameBinder.java
116 lines (107 loc) · 2.91 KB
/
FrameBinder.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
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Frame binder
*/
final public class FrameBinder {
private final String movieFileName;
private final String path;
private final String imgExt;
private final String soundFilePath;
private final int rate;
/**
* constructor
*
* @param fileName Movie file name to bind
* @param targetPath Path where image files exist
* @param ext Extension of target image file name
* @param soundFile Sound file path
* @param frameRate Movie file frame rate(per second)
*/
public FrameBinder(
String fileName,
String targetPath,
String ext,
String soundFile,
int frameRate
) {
movieFileName = fileName;
path = targetPath;
imgExt = ext;
soundFilePath = soundFile;
rate = frameRate;
}
/**
* Bind image files to a movie file
*/
public void bind() {
try {
final File imgDir = new File(path);
final File movie = new File(imgDir, movieFileName);
if (movie.exists()) {
movie.delete();
}
execFfmpeg(imgDir);
deleteFrames(imgDir);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private String[] createCommandLineParam() {
final ArrayList<String> command = new ArrayList<String>(Arrays.asList(
"ffmpeg",
"-framerate",
Integer.toString(rate),
"-i",
String.format("%%08d.%s", imgExt)
));
if (soundFilePath != null) {
command.addAll(Arrays.asList("-i", soundFilePath));
final String[] compressOptions = {
"-codec:a",
"aac",
"-b:a",
"320k"
};
final String[] copyOptions = {
"-codec:a",
"copy"
};
command.addAll(Arrays.asList(soundFilePath.toLowerCase().endsWith(".wav")
? compressOptions
: copyOptions));
}
command.addAll(Arrays.asList(
"-pix_fmt",
"yuv420p",
"-r",
Integer.toString(rate),
movieFileName
));
return command.toArray(new String[0]);
}
private void execFfmpeg(File imgDir) throws IOException, InterruptedException {
final ProcessBuilder processBuilder = new ProcessBuilder(createCommandLineParam());
processBuilder.redirectErrorStream(true);
processBuilder.inheritIO();
processBuilder.directory(imgDir);
final Process process = processBuilder.start();
process.waitFor();
process.destroy();
}
private void deleteFrames(File imgDir) {
final File[] files = imgDir.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(imgExt);
}
});
for (File file : files) {
file.delete();
}
}
}