-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFmpeg.pde
282 lines (216 loc) · 8.44 KB
/
FFmpeg.pde
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
import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class FFmpegEngine {
Pattern framePattern = Pattern.compile("^frame=(\\d+)$");
String ffmpegPath;
public int framecount = 0;
Map<String, String> variantToFilename = new HashMap<>();
// https://github.com/eugeneware/ffmpeg-static/releases/download/b5.0.1/darwin-arm64.gz
static final String DOWNLOAD_URL = "";
public String log = "";
FFmpegEngine() {
variantToFilename.put("macos-x86_64", "darwin-x64");
variantToFilename.put("macos-aarch64", "darwin-arm64");
variantToFilename.put("windows-amd64", "win32-x64");
variantToFilename.put("linux-amd64", "linux-x64");
variantToFilename.put("linux-arm", "linux-arm");
variantToFilename.put("linux-aarch64", "linux-arm64");
String ffmpegName = isWindows() ? "ffmpeg.exe" : "ffmpeg";
File ffmpegFile = new File(sketchPath()+"/data/engine/ffmpeg/"+ffmpegName);
ffmpegPath = ffmpegFile.getAbsolutePath();
if (!ffmpegFile.exists()) {
System.err.println("FFMPEG NOT FOUND");
// download the binary from DOWNLOAD_URL, use a ProgressMonitor to track it
// https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html
}
}
String[] getFormats() {
return new String[] {
"MPEG-4",
"MPEG-4 (Lossless 4:2:0)",
"MPEG-4 (Lossless 4:4:4)",
"Apple ProRes 4444",
"Animated GIF",
"Animated GIF (Loop)"
};
}
void write(File movieFile, File[] imgFiles, File soundFile,
int width, int height, double fps, String formatName) throws IOException {
log = "";
// Write a temporary file with the names of all the images.
// This removes the requirement for images using %04d format (and having
// to detect the exact variant). Also, the number of images is likely to
// be too long for command line arguments on some platforms.
File listingFile = File.createTempFile("listing", ".txt");
PrintWriter writer = new PrintWriter(new FileWriter(listingFile));
for (File file : imgFiles) {
writer.println("file '" + file.getAbsolutePath() + "'");
}
writer.flush();
writer.close();
// path specified by the user (may be changed later to append file type)
String outputPath = movieFile.getAbsolutePath();
List<String> cmd = new ArrayList<>();
cmd.add(ffmpegPath);
// delete the file if it already exists
cmd.add("-y");
// set frame rate
cmd.add("-r");
cmd.add(String.valueOf(fps));
// input format is image files
cmd.add("-f");
cmd.add("image2");
// allow absolute paths in the file list
cmd.add("-safe");
cmd.add("0");
// use the concatenation filter to read our entries from a file
// https://trac.ffmpeg.org/wiki/Concatenate
// (not enough to simply specify -i with a .txt file)
cmd.add("-f");
cmd.add("concat");
// temporary file with the list of all images
cmd.add("-i");
cmd.add(listingFile.getAbsolutePath());
// pipe:1 sends progress to stdout, pipe:2 to stderr
cmd.add("-progress");
cmd.add("pipe:2");
String formatArgs = "fps=" + fps;
if (width != 0 && height != 0) {
formatArgs += ",scale=" + width + ":" + height + ":flags=lanczos";
}
if (soundFile != null && !formatName.contains("GIF")) {
cmd.add("-i");
cmd.add(soundFile.getAbsolutePath());
}
if (formatName.startsWith("MPEG-4")) {
// slideshow: http://trac.ffmpeg.org/wiki/Slideshow
// options for compatibility: https://superuser.com/a/424024
// use the h.264 video codec
cmd.add("-vcodec");
cmd.add("libx264");
if (formatName.contains("4:2:0")) {
// Best compatibility with QuickTime, macOS, and others
cmd.add("-pix_fmt");
cmd.add("yuv420p");
// https://trac.ffmpeg.org/wiki/Encode/H.264
cmd.add("-preset");
// can also use "veryslow" for better compression
cmd.add("ultrafast");
cmd.add("-crf");
cmd.add("0");
} else if (formatName.contains("4:4:4")) {
// Based on https://stackoverflow.com/a/18506577
// Not compatible with QuickTime and macOS, but works in VLC
cmd.add("-pix_fmt");
cmd.add("yuv444p");
cmd.add("-profile:v");
cmd.add("high444");
cmd.add("-preset:v");
cmd.add("slow");
cmd.add("-crf");
cmd.add("0");
} else {
cmd.add("-pix_fmt");
cmd.add("yuv420p");
// decent quality images
cmd.add("-crf");
cmd.add("21"); // 18 to 25, with 18 the lowest
}
// if there's a resize, specify it and the type of scaling
if (width != 0 && height != 0) {
cmd.add("-vf");
cmd.add(formatArgs);
}
if (soundFile != null) {
cmd.add("-acodec");
cmd.add("aac");
}
// move container metadata to the beginning of the file
cmd.add("-movflags");
cmd.add("faststart");
if (!outputPath.toLowerCase().endsWith(".mp4")) {
outputPath += ".mp4";
}
} else if (formatName.contains("ProRes 4444")) {
// https://ottverse.com/ffmpeg-convert-to-apple-prores-422-4444-hq/
// https://avpres.net/FFmpeg/im_ProRes.html
cmd.add("-c:v");
cmd.add("prores_ks");
cmd.add("-profile:v");
cmd.add("4");
cmd.add("-vendor");
cmd.add("apl0");
cmd.add("-bits_per_mb");
cmd.add("8000");
cmd.add("-pix_fmt");
cmd.add("yuva444p10le");
if (soundFile != null) {
cmd.add("-c:a");
cmd.add("copy");
}
if (!outputPath.toLowerCase().endsWith(".mov")) {
outputPath += ".mov";
}
} else if (formatName.startsWith("Animated GIF")) {
// ffmpeg -r 30 -f image2 -safe 0 -f concat -i listing.txt -vf "fps=30,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 ~/Desktop/output.gif
// sets the gif palette nicely, based on https://superuser.com/a/556031
formatArgs += ",split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse";
cmd.add("-vf");
cmd.add(formatArgs);
// https://www.ffmpeg.org/ffmpeg-formats.html#gif-2
cmd.add("-loop");
// -1 means no loop, 0 means infinite loop (1+ means number of loops?)
cmd.add(formatName.contains("Loop") ? "0" : "-1");
if (!outputPath.toLowerCase().endsWith(".gif")) {
outputPath += ".gif";
}
}
cmd.add(outputPath);
// pass cmd to Runtime exec
// read the output and set the progress bar
// show a message (success/failure) when complete
// https://stackoverflow.com/a/33386692
Process p = new ProcessBuilder(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), System.out::println);
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), line -> {
log += line+"\n";
// TODO handle process.isCanceled()
Matcher m = framePattern.matcher(line);
if (m.find()) {
framecount = Integer.parseInt(m.group(1));
}
//System.out.println(line);
});
new Thread(outputGobbler).start();
new Thread(errorGobbler).start();
try {
int result = p.waitFor();
//System.out.println("encoding result was " + result);
if (result != 0) {
throw new IOException("Unknown error while creating movie. " +
"(FFmpeg result was " + result + ")");
}
} catch (InterruptedException ignored) { }
}
class StreamGobbler implements Runnable {
final private InputStream inputStream;
final private Consumer<String> consumeInputLine;
public StreamGobbler(InputStream inputStream, Consumer<String> consumeInputLine) {
this.inputStream = inputStream;
this.consumeInputLine = consumeInputLine;
}
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumeInputLine);
}
}
NumberFormat formatter = NumberFormat.getInstance();
}