Skip to content

Commit 8890104

Browse files
EukliosMickael GREGORI
and
Mickael GREGORI
authored
Feature/pr214 Appendable to output and error streams - changes (#304)
* FEAT Define Appendable to read input and error streams of ffmpeg/ffprobe process Co-authored-by: Mickael GREGORI <[email protected]>
1 parent 4bc84da commit 8890104

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/main/java/net/bramp/ffmpeg/FFcommon.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.common.io.CharStreams;
99
import java.io.BufferedReader;
1010
import java.io.IOException;
11+
import java.io.InputStream;
1112
import java.io.InputStreamReader;
1213
import java.nio.charset.StandardCharsets;
1314
import java.util.List;
@@ -28,6 +29,12 @@ abstract class FFcommon {
2829
/** Version string */
2930
String version = null;
3031

32+
/** Process input stream */
33+
Appendable processOutputStream = System.out;
34+
35+
/** Process error stream */
36+
Appendable processErrorStream = System.err;
37+
3138
public FFcommon(@Nonnull String path) {
3239
this(path, new RunProcessFunction());
3340
}
@@ -38,8 +45,26 @@ protected FFcommon(@Nonnull String path, @Nonnull ProcessFunction runFunction) {
3845
this.path = path;
3946
}
4047

48+
public void setProcessOutputStream(@Nonnull Appendable processOutputStream) {
49+
Preconditions.checkNotNull(processOutputStream);
50+
this.processOutputStream = processOutputStream;
51+
}
52+
53+
public void setProcessErrorStream(@Nonnull Appendable processErrorStream) {
54+
Preconditions.checkNotNull(processErrorStream);
55+
this.processErrorStream = processErrorStream;
56+
}
57+
58+
private BufferedReader _wrapInReader(final InputStream inputStream) {
59+
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
60+
}
61+
4162
protected BufferedReader wrapInReader(Process p) {
42-
return new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
63+
return _wrapInReader(p.getInputStream());
64+
}
65+
66+
protected BufferedReader wrapErrorInReader(Process p) {
67+
return _wrapInReader(p.getErrorStream());
4368
}
4469

4570
protected void throwOnError(Process p) throws IOException {
@@ -107,8 +132,8 @@ public void run(List<String> args) throws IOException {
107132
// TODO Move the copy onto a thread, so that FFmpegProgressListener can be on this thread.
108133

109134
// Now block reading ffmpeg's stdout. We are effectively throwing away the output.
110-
CharStreams.copy(wrapInReader(p), System.out); // TODO Should I be outputting to stdout?
111-
135+
CharStreams.copy(wrapInReader(p), processOutputStream);
136+
CharStreams.copy(wrapErrorInReader(p), processErrorStream);
112137
throwOnError(p);
113138

114139
} finally {

src/test/java/net/bramp/ffmpeg/FFmpegTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.io.IOException;
99
import java.util.List;
10+
11+
import com.google.common.collect.Lists;
1012
import net.bramp.ffmpeg.fixtures.Codecs;
1113
import net.bramp.ffmpeg.fixtures.Filters;
1214
import net.bramp.ffmpeg.fixtures.Formats;
@@ -35,6 +37,8 @@ public void before() throws IOException {
3537
when(runFunc.run(argThatHasItem("-codecs"))).thenAnswer(new NewProcessAnswer("ffmpeg-codecs"));
3638
when(runFunc.run(argThatHasItem("-pix_fmts")))
3739
.thenAnswer(new NewProcessAnswer("ffmpeg-pix_fmts"));
40+
when(runFunc.run(argThatHasItem("toto.mp4")))
41+
.thenAnswer(new NewProcessAnswer("ffmpeg-version", "ffmpeg-no-such-file"));
3842
when(runFunc.run(argThatHasItem("-filters")))
3943
.thenAnswer(new NewProcessAnswer("ffmpeg-filters"));
4044

@@ -72,6 +76,21 @@ public void testFormats() throws IOException {
7276
verify(runFunc, times(1)).run(argThatHasItem("-formats"));
7377
}
7478

79+
@Test
80+
public void testReadProcessStreams() throws IOException {
81+
// process input stream
82+
Appendable processInputStream = mock(Appendable.class);
83+
ffmpeg.setProcessOutputStream(processInputStream);
84+
// process error stream
85+
Appendable processErrStream = mock(Appendable.class);
86+
ffmpeg.setProcessErrorStream(processErrStream);
87+
// run ffmpeg with non existing file
88+
ffmpeg.run(Lists.newArrayList("-i", "toto.mp4"));
89+
// check calls to Appendables
90+
verify(processInputStream, times(1)).append(any(CharSequence.class));
91+
verify(processErrStream, times(1)).append(any(CharSequence.class));
92+
}
93+
7594
@Test
7695
public void testPixelFormat() throws IOException {
7796
// Run twice, the second should be cached

src/test/java/net/bramp/ffmpeg/lang/NewProcessAnswer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
public class NewProcessAnswer implements Answer<Process> {
88
final String resource;
99

10+
final String errResource;
11+
1012
public NewProcessAnswer(String resource) {
13+
this(resource, null);
14+
}
15+
16+
public NewProcessAnswer(String resource, String errResource) {
1117
this.resource = resource;
18+
this.errResource = errResource;
1219
}
1320

1421
@Override
1522
public Process answer(InvocationOnMock invocationOnMock) throws Throwable {
16-
return new MockProcess(Helper.loadResource(resource));
23+
return errResource == null
24+
? new MockProcess(Helper.loadResource(resource))
25+
: new MockProcess(null, Helper.loadResource(resource), Helper.loadResource(errResource));
1726
}
1827
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
toto.mp4: No such file or directory

0 commit comments

Comments
 (0)