-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathFFcommon.java
More file actions
157 lines (131 loc) · 4.96 KB
/
FFcommon.java
File metadata and controls
157 lines (131 loc) · 4.96 KB
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
package net.bramp.ffmpeg;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nonnull;
import net.bramp.ffmpeg.io.ProcessUtils;
import net.bramp.ffmpeg.probe.FFmpegError;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
/** Private class to contain common methods for both FFmpeg and FFprobe. */
abstract class FFcommon {
/** Path to the binary (e.g. /usr/bin/ffmpeg) */
final String path;
/** Function to run FFmpeg. We define it like this so we can swap it out (during testing) */
final ProcessFunction runFunc;
/** Version string */
String version = null;
/** Process input stream */
Appendable processOutputStream = System.out;
/** Process error stream */
Appendable processErrorStream = System.err;
public FFcommon(@Nonnull String path) {
this(path, new RunProcessFunction());
}
protected FFcommon(@Nonnull String path, @Nonnull ProcessFunction runFunction) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
this.runFunc = checkNotNull(runFunction);
this.path = path;
}
public void setProcessOutputStream(@Nonnull Appendable processOutputStream) {
Preconditions.checkNotNull(processOutputStream);
this.processOutputStream = processOutputStream;
}
public void setProcessErrorStream(@Nonnull Appendable processErrorStream) {
Preconditions.checkNotNull(processErrorStream);
this.processErrorStream = processErrorStream;
}
private BufferedReader _wrapInReader(final InputStream inputStream) {
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
}
protected BufferedReader wrapInReader(Process p) {
return _wrapInReader(p.getInputStream());
}
protected BufferedReader wrapErrorInReader(Process p) {
return _wrapInReader(p.getErrorStream());
}
protected void throwOnError(Process p) throws IOException {
try {
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
// TODO Parse the error
throw new IOException(path + " returned non-zero exit status. Check stdout.");
}
} catch (TimeoutException e) {
throw new IOException("Timed out waiting for " + path + " to finish.");
}
}
protected void throwOnError(Process p, FFmpegProbeResult result) throws IOException {
try {
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
// TODO Parse the error
final FFmpegError ffmpegError = null == result ? null : result.getError();
throw new FFmpegException(
path + " returned non-zero exit status. Check stdout.", ffmpegError);
}
} catch (TimeoutException e) {
throw new IOException("Timed out waiting for " + path + " to finish.");
}
}
/**
* Returns the version string for this binary.
*
* @return the version string.
* @throws IOException If there is an error capturing output from the binary.
*/
public synchronized @Nonnull String version() throws IOException {
if (this.version == null) {
Process p = runFunc.run(ImmutableList.of(path, "-version"));
try {
BufferedReader r = wrapInReader(p);
this.version = r.readLine();
CharStreams.copy(r, CharStreams.nullWriter()); // Throw away rest of the output
throwOnError(p);
} finally {
p.destroy();
}
}
return version;
}
public String getPath() {
return path;
}
/**
* Returns the full path to the binary with arguments appended.
*
* @param args The arguments to pass to the binary.
* @return The full path and arguments to execute the binary.
* @throws IOException If there is an error capturing output from the binary
*/
public List<String> path(List<String> args) throws IOException {
return ImmutableList.<String>builder().add(path).addAll(args).build();
}
/**
* Runs the binary (ffmpeg) with the supplied args. Blocking until finished.
*
* @param args The arguments to pass to the binary.
* @throws IOException If there is a problem executing the binary.
*/
public void run(List<String> args) throws IOException {
checkNotNull(args);
Process p = runFunc.run(path(args));
assert (p != null);
try {
// TODO Move the copy onto a thread, so that FFmpegProgressListener can be on this thread.
// Now block reading ffmpeg's stdout. We are effectively throwing away the output.
CharStreams.copy(wrapInReader(p), processOutputStream);
CharStreams.copy(wrapErrorInReader(p), processErrorStream);
throwOnError(p);
} finally {
p.destroy();
}
}
}