Skip to content

Commit

Permalink
Merge remote-tracking branch 'barnettj/master'
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/net/bramp/ffmpeg/FFcommon.java
	src/test/java/net/bramp/ffmpeg/FFprobeTest.java
  • Loading branch information
Euklios committed Aug 9, 2024
2 parents 46c41e3 + 76afbfd commit 69b2912
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/main/java/net/bramp/ffmpeg/FFcommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import net.bramp.ffmpeg.io.ProcessUtils;
import net.bramp.ffmpeg.probe.FFmpegError;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -15,7 +19,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nonnull;
import net.bramp.ffmpeg.io.ProcessUtils;

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

protected void throwOnError(Process p, FFmpegProbeResult result) throws IOException {
try {
// TODO In java 8 use waitFor(long timeout, TimeUnit unit)
if (ProcessUtils.waitForWithTimeout(p, 1, TimeUnit.SECONDS) != 0) {
// TODO Parse the error
final FFmpegError ffmpegError = null == result ? null : result.getError();
throw new FFmpegException(
path + " returned non-zero exit status. Check stdout.", ffmpegError);
}
} catch (TimeoutException e) {
throw new IOException("Timed out waiting for " + path + " to finish.");
}
}

/**
* Returns the version string for this binary.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/bramp/ffmpeg/FFmpegException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.bramp.ffmpeg;

import net.bramp.ffmpeg.probe.FFmpegError;

import java.io.IOException;

public class FFmpegException extends IOException {

private static final long serialVersionUID = 3048288225568984942L;
private FFmpegError error;

public FFmpegException(String message, FFmpegError error) {
super(message);
this.error = error;
}

public FFmpegError getError() {
return error;
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/bramp/ffmpeg/FFprobe.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public FFmpegProbeResult probe(List<String> args) throws IOException {

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

throwOnError(p);
throwOnError(p, result);

if (result == null) {
throw new IllegalStateException("Gson returned null, which shouldn't happen :(");
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/net/bramp/ffmpeg/FFprobeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.IOException;
Expand All @@ -31,8 +32,8 @@
@RunWith(MockitoJUnitRunner.class)
public class FFprobeTest {

@Mock
ProcessFunction runFunc;
@Mock ProcessFunction runFunc;
@Mock Process mockProcess;

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

@Test
public void shouldThrowOnErrorWithFFmpegProbeResult() throws InterruptedException {
Mockito.when(mockProcess.waitFor()).thenReturn(-1);
final FFmpegError error = new FFmpegError();
final FFmpegProbeResult result = new FFmpegProbeResult();
result.error = error;
FFmpegException e = assertThrows(FFmpegException.class, () -> ffprobe.throwOnError(mockProcess, result));
assertEquals(error, e.getError());
}

@Test
public void testProbeSideDataList() throws IOException {
FFmpegProbeResult info = ffprobe.probe(Samples.side_data_list);
Expand Down

0 comments on commit 69b2912

Please sign in to comment.