Skip to content

Commit

Permalink
Feature/pr214 Appendable to output and error streams - changes (#304)
Browse files Browse the repository at this point in the history
* FEAT Define Appendable to read input and error streams of ffmpeg/ffprobe process

Co-authored-by: Mickael GREGORI <[email protected]>
  • Loading branch information
Euklios and Mickael GREGORI authored Mar 11, 2024
1 parent 4bc84da commit 8890104
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/main/java/net/bramp/ffmpeg/FFcommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -28,6 +29,12 @@ abstract class FFcommon {
/** 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());
}
Expand All @@ -38,8 +45,26 @@ protected FFcommon(@Nonnull String path, @Nonnull ProcessFunction 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 new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
return _wrapInReader(p.getInputStream());
}

protected BufferedReader wrapErrorInReader(Process p) {
return _wrapInReader(p.getErrorStream());
}

protected void throwOnError(Process p) throws IOException {
Expand Down Expand Up @@ -107,8 +132,8 @@ public void run(List<String> args) throws IOException {
// 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), System.out); // TODO Should I be outputting to stdout?

CharStreams.copy(wrapInReader(p), processOutputStream);
CharStreams.copy(wrapErrorInReader(p), processErrorStream);
throwOnError(p);

} finally {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/bramp/ffmpeg/FFmpegTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.IOException;
import java.util.List;

import com.google.common.collect.Lists;
import net.bramp.ffmpeg.fixtures.Codecs;
import net.bramp.ffmpeg.fixtures.Filters;
import net.bramp.ffmpeg.fixtures.Formats;
Expand Down Expand Up @@ -35,6 +37,8 @@ public void before() throws IOException {
when(runFunc.run(argThatHasItem("-codecs"))).thenAnswer(new NewProcessAnswer("ffmpeg-codecs"));
when(runFunc.run(argThatHasItem("-pix_fmts")))
.thenAnswer(new NewProcessAnswer("ffmpeg-pix_fmts"));
when(runFunc.run(argThatHasItem("toto.mp4")))
.thenAnswer(new NewProcessAnswer("ffmpeg-version", "ffmpeg-no-such-file"));
when(runFunc.run(argThatHasItem("-filters")))
.thenAnswer(new NewProcessAnswer("ffmpeg-filters"));

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

@Test
public void testReadProcessStreams() throws IOException {
// process input stream
Appendable processInputStream = mock(Appendable.class);
ffmpeg.setProcessOutputStream(processInputStream);
// process error stream
Appendable processErrStream = mock(Appendable.class);
ffmpeg.setProcessErrorStream(processErrStream);
// run ffmpeg with non existing file
ffmpeg.run(Lists.newArrayList("-i", "toto.mp4"));
// check calls to Appendables
verify(processInputStream, times(1)).append(any(CharSequence.class));
verify(processErrStream, times(1)).append(any(CharSequence.class));
}

@Test
public void testPixelFormat() throws IOException {
// Run twice, the second should be cached
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/net/bramp/ffmpeg/lang/NewProcessAnswer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
public class NewProcessAnswer implements Answer<Process> {
final String resource;

final String errResource;

public NewProcessAnswer(String resource) {
this(resource, null);
}

public NewProcessAnswer(String resource, String errResource) {
this.resource = resource;
this.errResource = errResource;
}

@Override
public Process answer(InvocationOnMock invocationOnMock) throws Throwable {
return new MockProcess(Helper.loadResource(resource));
return errResource == null
? new MockProcess(Helper.loadResource(resource))
: new MockProcess(null, Helper.loadResource(resource), Helper.loadResource(errResource));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toto.mp4: No such file or directory

0 comments on commit 8890104

Please sign in to comment.