/
FFmpeg.java
312 lines (254 loc) · 9.77 KB
/
FFmpeg.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
package net.bramp.ffmpeg;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.bramp.ffmpeg.builder.FFmpegBuilder;
import net.bramp.ffmpeg.info.*;
import net.bramp.ffmpeg.progress.ProgressListener;
import net.bramp.ffmpeg.progress.ProgressParser;
import net.bramp.ffmpeg.progress.TcpProgressParser;
import org.apache.commons.lang3.math.Fraction;
/**
* Wrapper around FFmpeg
*
* @author bramp
*/
public class FFmpeg extends FFcommon {
public static final String FFMPEG = "ffmpeg";
public static final String DEFAULT_PATH = firstNonNull(System.getenv("FFMPEG"), FFMPEG);
public static final Fraction FPS_30 = Fraction.getFraction(30, 1);
public static final Fraction FPS_29_97 = Fraction.getFraction(30000, 1001);
public static final Fraction FPS_24 = Fraction.getFraction(24, 1);
public static final Fraction FPS_23_976 = Fraction.getFraction(24000, 1001);
public static final int AUDIO_MONO = 1;
public static final int AUDIO_STEREO = 2;
public static final String AUDIO_FORMAT_U8 = "u8"; // 8
public static final String AUDIO_FORMAT_S16 = "s16"; // 16
public static final String AUDIO_FORMAT_S32 = "s32"; // 32
public static final String AUDIO_FORMAT_FLT = "flt"; // 32
public static final String AUDIO_FORMAT_DBL = "dbl"; // 64
@Deprecated public static final String AUDIO_DEPTH_U8 = AUDIO_FORMAT_U8;
@Deprecated public static final String AUDIO_DEPTH_S16 = AUDIO_FORMAT_S16;
@Deprecated public static final String AUDIO_DEPTH_S32 = AUDIO_FORMAT_S32;
@Deprecated public static final String AUDIO_DEPTH_FLT = AUDIO_FORMAT_FLT;
@Deprecated public static final String AUDIO_DEPTH_DBL = AUDIO_FORMAT_DBL;
public static final int AUDIO_SAMPLE_8000 = 8000;
public static final int AUDIO_SAMPLE_11025 = 11025;
public static final int AUDIO_SAMPLE_12000 = 12000;
public static final int AUDIO_SAMPLE_16000 = 16000;
public static final int AUDIO_SAMPLE_22050 = 22050;
public static final int AUDIO_SAMPLE_32000 = 32000;
public static final int AUDIO_SAMPLE_44100 = 44100;
public static final int AUDIO_SAMPLE_48000 = 48000;
public static final int AUDIO_SAMPLE_96000 = 96000;
static final Pattern CODECS_REGEX =
Pattern.compile("^ ([.D][.E][VASD][.I][.L][.S]) (\\S{2,})\\s+(.*)$");
static final Pattern FORMATS_REGEX = Pattern.compile("^ ([ D][ E]) (\\S+)\\s+(.*)$");
static final Pattern PIXEL_FORMATS_REGEX =
Pattern.compile("^([.I][.O][.H][.P][.B]) (\\S{2,})\\s+(\\d+)\\s+(\\d+)$");
static final Pattern FILTERS_REGEX = Pattern.compile("^\\s*(?<timelinesupport>[T.])(?<slicethreading>[S.])(?<commandsupport>[C.])\\s(?<name>[A-Za-z0-9_]+)\\s+(?<inputpattern>[AVN|]+)->(?<outputpattern>[AVN|]+)\\s+(?<description>.*)$");
/** Supported codecs */
List<Codec> codecs = null;
/** Supported formats */
List<Format> formats = null;
/** Supported pixel formats */
private List<PixelFormat> pixelFormats = null;
/** Supported filters */
private List<Filter> filters = null;
/** Supported channel layouts */
private List<ChannelLayout> channelLayouts = null;
public FFmpeg() throws IOException {
this(DEFAULT_PATH, new RunProcessFunction());
}
public FFmpeg(@Nonnull ProcessFunction runFunction) throws IOException {
this(DEFAULT_PATH, runFunction);
}
public FFmpeg(@Nonnull String path) throws IOException {
this(path, new RunProcessFunction());
}
@SuppressWarnings("this-escape")
public FFmpeg(@Nonnull String path, @Nonnull ProcessFunction runFunction) throws IOException {
super(path, runFunction);
version();
}
/**
* Returns true if the binary we are using is the true ffmpeg. This is to avoid conflict with
* avconv (from the libav project), that some symlink to ffmpeg.
*
* @return true iff this is the official ffmpeg binary.
* @throws IOException If a I/O error occurs while executing ffmpeg.
*/
public boolean isFFmpeg() throws IOException {
return version().startsWith("ffmpeg");
}
/**
* Throws an exception if this is an unsupported version of ffmpeg.
*
* @throws IllegalArgumentException if this is not the official ffmpeg binary.
* @throws IOException If a I/O error occurs while executing ffmpeg.
*/
private void checkIfFFmpeg() throws IllegalArgumentException, IOException {
if (!isFFmpeg()) {
throw new IllegalArgumentException(
"This binary '" + path + "' is not a supported version of ffmpeg");
}
}
public synchronized @Nonnull List<Codec> codecs() throws IOException {
checkIfFFmpeg();
if (this.codecs == null) {
codecs = new ArrayList<>();
Process p = runFunc.run(ImmutableList.of(path, "-codecs"));
try {
BufferedReader r = wrapInReader(p);
String line;
while ((line = r.readLine()) != null) {
Matcher m = CODECS_REGEX.matcher(line);
if (!m.matches()) continue;
codecs.add(new Codec(m.group(2), m.group(3), m.group(1)));
}
throwOnError(p);
this.codecs = ImmutableList.copyOf(codecs);
} finally {
p.destroy();
}
}
return codecs;
}
public synchronized @Nonnull List<Filter> filters() throws IOException {
checkIfFFmpeg();
if (this.filters == null) {
filters = new ArrayList<>();
Process p = runFunc.run(ImmutableList.of(path, "-filters"));
try {
BufferedReader r = wrapInReader(p);
String line;
while ((line = r.readLine()) != null) {
Matcher m = FILTERS_REGEX.matcher(line);
if (!m.matches()) continue;
// (?<inputpattern>[AVN|]+)->(?<outputpattern>[AVN|]+)\s+(?<description>.*)$
filters.add(new Filter(
m.group("timelinesupport").equals("T"),
m.group("slicethreading").equals("S"),
m.group("commandsupport").equals("C"),
m.group("name"),
new FilterPattern(m.group("inputpattern")),
new FilterPattern(m.group("outputpattern")),
m.group("description")
));
}
throwOnError(p);
this.filters = ImmutableList.copyOf(filters);
} finally {
p.destroy();
}
}
return this.filters;
}
public synchronized @Nonnull List<Format> formats() throws IOException {
checkIfFFmpeg();
if (this.formats == null) {
formats = new ArrayList<>();
Process p = runFunc.run(ImmutableList.of(path, "-formats"));
try {
BufferedReader r = wrapInReader(p);
String line;
while ((line = r.readLine()) != null) {
Matcher m = FORMATS_REGEX.matcher(line);
if (!m.matches()) continue;
formats.add(new Format(m.group(2), m.group(3), m.group(1)));
}
throwOnError(p);
this.formats = ImmutableList.copyOf(formats);
} finally {
p.destroy();
}
}
return formats;
}
public synchronized List<PixelFormat> pixelFormats() throws IOException {
checkIfFFmpeg();
if (this.pixelFormats == null) {
pixelFormats = new ArrayList<>();
Process p = runFunc.run(ImmutableList.of(path, "-pix_fmts"));
try {
BufferedReader r = wrapInReader(p);
String line;
while ((line = r.readLine()) != null) {
Matcher m = PIXEL_FORMATS_REGEX.matcher(line);
if (!m.matches()) continue;
String flags = m.group(1);
pixelFormats.add(
new PixelFormat(
m.group(2), Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)), flags));
}
throwOnError(p);
this.pixelFormats = ImmutableList.copyOf(pixelFormats);
} finally {
p.destroy();
}
}
return pixelFormats;
}
public synchronized List<ChannelLayout> channelLayouts() throws IOException {
checkIfFFmpeg();
if (this.channelLayouts == null) {
Process p = runFunc.run(ImmutableList.of(path, "-layouts"));
try {
BufferedReader r = wrapInReader(p);
this.channelLayouts = Collections.unmodifiableList(InfoParser.parseLayouts(r));
} finally {
p.destroy();
}
}
return this.channelLayouts;
}
protected ProgressParser createProgressParser(ProgressListener listener) throws IOException {
// TODO In future create the best kind for this OS, unix socket, named pipe, or TCP.
try {
// Default to TCP because it is supported across all OSes, and is better than UDP because it
// provides good properties such as in-order packets, reliability, error checking, etc.
return new TcpProgressParser(checkNotNull(listener));
} catch (URISyntaxException e) {
throw new IOException(e);
}
}
@Override
public void run(List<String> args) throws IOException {
checkIfFFmpeg();
super.run(args);
}
public void run(FFmpegBuilder builder) throws IOException {
run(builder, null);
}
public void run(FFmpegBuilder builder, @Nullable ProgressListener listener) throws IOException {
checkNotNull(builder);
if (listener != null) {
try (ProgressParser progressParser = createProgressParser(listener)) {
progressParser.start();
builder = builder.addProgress(progressParser.getUri());
run(builder.build());
}
} else {
run(builder.build());
}
}
@CheckReturnValue
public FFmpegBuilder builder() {
return new FFmpegBuilder();
}
@Override
public String getPath() {
return path;
}
}