Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pr214 Appendable to output and error streams - changes #304

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading