Skip to content

Commit 76afbfd

Browse files
author
James Barnett
committed
Propagate FFmpegError through FFmpegException on FFprobe failures
1 parent 19923fb commit 76afbfd

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.google.common.collect.ImmutableList;
66
import com.google.common.io.CharStreams;
77
import net.bramp.ffmpeg.io.ProcessUtils;
8+
import net.bramp.ffmpeg.probe.FFmpegError;
9+
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
810

911
import javax.annotation.Nonnull;
1012
import java.io.BufferedReader;
@@ -55,6 +57,20 @@ protected void throwOnError(Process p) throws IOException {
5557
}
5658
}
5759

60+
protected void throwOnError(Process p, FFmpegProbeResult result) throws IOException {
61+
try {
62+
// TODO In java 8 use waitFor(long timeout, TimeUnit unit)
63+
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
64+
// TODO Parse the error
65+
final FFmpegError ffmpegError = null == result ? null : result.getError();
66+
throw new FFmpegException(
67+
path + " returned non-zero exit status. Check stdout.", ffmpegError);
68+
}
69+
} catch (TimeoutException e) {
70+
throw new IOException("Timed out waiting for " + path + " to finish.");
71+
}
72+
}
73+
5874
/**
5975
* Returns the version string for this binary.
6076
*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.bramp.ffmpeg;
2+
3+
import net.bramp.ffmpeg.probe.FFmpegError;
4+
5+
import java.io.IOException;
6+
7+
public class FFmpegException extends IOException {
8+
9+
private static final long serialVersionUID = 3048288225568984942L;
10+
private FFmpegError error;
11+
12+
public FFmpegException(String message, FFmpegError error) {
13+
super(message);
14+
this.error = error;
15+
}
16+
17+
public FFmpegError getError() {
18+
return error;
19+
}
20+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public FFmpegProbeResult probe(String mediaPath, @Nullable String userAgent) thr
110110

111111
FFmpegProbeResult result = gson.fromJson(reader, FFmpegProbeResult.class);
112112

113-
throwOnError(p);
113+
throwOnError(p, result);
114114

115115
if (result == null) {
116116
throw new IllegalStateException("Gson returned null, which shouldn't happen :(");

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import net.bramp.ffmpeg.fixtures.Samples;
55
import net.bramp.ffmpeg.lang.NewProcessAnswer;
66
import net.bramp.ffmpeg.probe.FFmpegChapter;
7+
import net.bramp.ffmpeg.probe.FFmpegError;
78
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
89
import net.bramp.ffmpeg.probe.FFmpegStream;
910
import org.apache.commons.lang3.math.Fraction;
1011
import org.junit.Before;
1112
import org.junit.Test;
1213
import org.junit.runner.RunWith;
1314
import org.mockito.Mock;
15+
import org.mockito.Mockito;
1416
import org.mockito.junit.MockitoJUnitRunner;
1517

1618
import java.io.IOException;
@@ -25,6 +27,7 @@
2527
public class FFprobeTest {
2628

2729
@Mock ProcessFunction runFunc;
30+
@Mock Process mockProcess;
2831

2932
FFprobe ffprobe;
3033

@@ -143,4 +146,17 @@ public void testProbeDivideByZero() throws IOException {
143146

144147
// System.out.println(FFmpegUtils.getGson().toJson(info));
145148
}
149+
150+
@Test
151+
public void shouldThrowOnErrorWithFFmpegProbeResult() throws IOException, InterruptedException {
152+
Mockito.when(mockProcess.waitFor()).thenReturn(-1);
153+
final FFmpegError error = new FFmpegError();
154+
final FFmpegProbeResult result = new FFmpegProbeResult();
155+
result.error = error;
156+
try {
157+
ffprobe.throwOnError(mockProcess, result);
158+
} catch (FFmpegException e) {
159+
assertEquals(error, e.getError());
160+
}
161+
}
146162
}

0 commit comments

Comments
 (0)