Skip to content

Commit 69b2912

Browse files
committed
Merge remote-tracking branch 'barnettj/master'
Conflicts: src/main/java/net/bramp/ffmpeg/FFcommon.java src/test/java/net/bramp/ffmpeg/FFprobeTest.java
2 parents 46c41e3 + 76afbfd commit 69b2912

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import com.google.common.base.Strings;
77
import com.google.common.collect.ImmutableList;
88
import com.google.common.io.CharStreams;
9+
import net.bramp.ffmpeg.io.ProcessUtils;
10+
import net.bramp.ffmpeg.probe.FFmpegError;
11+
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
12+
913
import java.io.BufferedReader;
1014
import java.io.IOException;
1115
import java.io.InputStream;
@@ -15,7 +19,6 @@
1519
import java.util.concurrent.TimeUnit;
1620
import java.util.concurrent.TimeoutException;
1721
import javax.annotation.Nonnull;
18-
import net.bramp.ffmpeg.io.ProcessUtils;
1922

2023
/** Private class to contain common methods for both FFmpeg and FFprobe. */
2124
abstract class FFcommon {
@@ -79,6 +82,20 @@ protected void throwOnError(Process p) throws IOException {
7982
}
8083
}
8184

85+
protected void throwOnError(Process p, FFmpegProbeResult result) throws IOException {
86+
try {
87+
// TODO In java 8 use waitFor(long timeout, TimeUnit unit)
88+
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
89+
// TODO Parse the error
90+
final FFmpegError ffmpegError = null == result ? null : result.getError();
91+
throw new FFmpegException(
92+
path + " returned non-zero exit status. Check stdout.", ffmpegError);
93+
}
94+
} catch (TimeoutException e) {
95+
throw new IOException("Timed out waiting for " + path + " to finish.");
96+
}
97+
}
98+
8299
/**
83100
* Returns the version string for this binary.
84101
*
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
@@ -107,7 +107,7 @@ public FFmpegProbeResult probe(List<String> args) throws IOException {
107107

108108
FFmpegProbeResult result = gson.fromJson(reader, FFmpegProbeResult.class);
109109

110-
throwOnError(p);
110+
throwOnError(p, result);
111111

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

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.mockito.ArgumentCaptor;
1515
import org.mockito.Captor;
1616
import org.mockito.Mock;
17+
import org.mockito.Mockito;
1718
import org.mockito.junit.MockitoJUnitRunner;
1819

1920
import java.io.IOException;
@@ -31,8 +32,8 @@
3132
@RunWith(MockitoJUnitRunner.class)
3233
public class FFprobeTest {
3334

34-
@Mock
35-
ProcessFunction runFunc;
35+
@Mock ProcessFunction runFunc;
36+
@Mock Process mockProcess;
3637

3738
@Captor
3839
ArgumentCaptor<List<String>> argsCaptor;
@@ -387,6 +388,16 @@ public void testProbeDivideByZero() throws IOException {
387388
// System.out.println(FFmpegUtils.getGson().toJson(info));
388389
}
389390

391+
@Test
392+
public void shouldThrowOnErrorWithFFmpegProbeResult() throws InterruptedException {
393+
Mockito.when(mockProcess.waitFor()).thenReturn(-1);
394+
final FFmpegError error = new FFmpegError();
395+
final FFmpegProbeResult result = new FFmpegProbeResult();
396+
result.error = error;
397+
FFmpegException e = assertThrows(FFmpegException.class, () -> ffprobe.throwOnError(mockProcess, result));
398+
assertEquals(error, e.getError());
399+
}
400+
390401
@Test
391402
public void testProbeSideDataList() throws IOException {
392403
FFmpegProbeResult info = ffprobe.probe(Samples.side_data_list);

0 commit comments

Comments
 (0)