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

Parse -codecs output from 1.x-4.x builds of FFmpeg #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/main/java/net/bramp/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class FFmpeg extends FFcommon {
public static final int AUDIO_SAMPLE_96000 = 96000;

static final Pattern CODECS_REGEX =
Pattern.compile("^ ([\\.D][\\.E][VAS][\\.I][\\.L][\\.S]) (?![\\=])(\\S+)\\s+(.*)$");
static final Pattern LEGACY_CODECS_REGEX =
Pattern.compile("^ ([ D][ E][VAS][ S][ D][ T]) (\\S+)\\s+(.*)$");
static final Pattern FORMATS_REGEX = Pattern.compile("^ ([ D][ E]) (\\S+)\\s+(.*)$");

Expand Down Expand Up @@ -123,15 +125,17 @@ private void checkIfFFmpeg() throws IllegalArgumentException, IOException {
Process p = runFunc.run(ImmutableList.of(path, "-codecs"));
try {
BufferedReader r = wrapInReader(p);
Pattern codecRegex = this.isLegacyVersion() ? LEGACY_CODECS_REGEX : CODECS_REGEX;
String line;
while ((line = r.readLine()) != null) {
Matcher m = CODECS_REGEX.matcher(line);
Matcher m = codecRegex.matcher(line);
if (!m.matches()) continue;

codecs.add(new Codec(m.group(2), m.group(3), m.group(1)));
}

throwOnError(p);

this.codecs = ImmutableList.copyOf(codecs);
} finally {
p.destroy();
Expand All @@ -141,6 +145,11 @@ private void checkIfFFmpeg() throws IllegalArgumentException, IOException {
return codecs;
}

private boolean isLegacyVersion() throws IOException {
String version = this.version();
return version.startsWith("ffmpeg version 0.");
}

public synchronized @Nonnull List<Format> formats() throws IOException {
checkIfFFmpeg();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/bramp/ffmpeg/info/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
@Immutable
public class Codec {

enum Type {
public enum Type {
VIDEO,
AUDIO,
INVALID,
SUBTITLE
}

Expand Down Expand Up @@ -66,7 +67,7 @@ public Codec(String name, String longName, String flags) {
this.type = Type.SUBTITLE;
break;
default:
throw new IllegalArgumentException("Invalid codec type '" + flags.charAt(3) + "'");
throw new IllegalArgumentException("Invalid codec type '" + flags.charAt(2) + "'");
}

// TODO There are more flags to parse
Expand Down
37 changes: 21 additions & 16 deletions src/test/java/net/bramp/ffmpeg/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ExamplesTest {
@Before
public void before() throws IOException {
when(runFunc.run(argThatHasItem("-version")))
.thenAnswer(new NewProcessAnswer("ffmpeg-version"));
.thenAnswer(new NewProcessAnswer("ffmpeg-version_0.10.9"));
ffmpeg = new FFmpeg("ffmpeg", runFunc);
}

Expand Down Expand Up @@ -264,16 +264,17 @@ public void testExample9() throws IOException {
// A test with videos added in a loop.
@Test
public void testExample10() throws IOException {
String expected = "ffmpeg -y -v error"
+ " -f webm_dash_manifest"
+ " -i audio.webm"
+ " -i video_1.webm"
+ " -i video_2.webm"
+ " -i video_3.webm"
+ " -vcodec copy -acodec copy"
+ " -map 0 -map 1 -map 2 -map 3"
+ " -adaptation_sets \"id=0,streams=0 id=1,streams=1,2,3\""
+ " output.mpd";
String expected =
"ffmpeg -y -v error"
+ " -f webm_dash_manifest"
+ " -i audio.webm"
+ " -i video_1.webm"
+ " -i video_2.webm"
+ " -i video_3.webm"
+ " -vcodec copy -acodec copy"
+ " -map 0 -map 1 -map 2 -map 3"
+ " -adaptation_sets \"id=0,streams=0 id=1,streams=1,2,3\""
+ " output.mpd";

ArrayList<String> streams = new ArrayList<>();
FFmpegBuilder builder = new FFmpegBuilder();
Expand All @@ -285,16 +286,20 @@ public void testExample10() throws IOException {
streams.add(String.format("%d", i));
}

FFmpegOutputBuilder out = builder.addOutput("output.mpd")
.setVideoCodec("copy").setAudioCodec("copy") // TODO Add a new setCodec(..) method.
.addExtraArgs("-map", "0");
FFmpegOutputBuilder out =
builder
.addOutput("output.mpd")
.setVideoCodec("copy")
.setAudioCodec("copy") // TODO Add a new setCodec(..) method.
.addExtraArgs("-map", "0");

for (String stream : streams) {
out.addExtraArgs("-map", stream);
}

out.addExtraArgs("-adaptation_sets",
String.format("\"id=0,streams=0 id=1,streams=%s\"", Joiner.on(",").join(streams)))
out.addExtraArgs(
"-adaptation_sets",
String.format("\"id=0,streams=0 id=1,streams=%s\"", Joiner.on(",").join(streams)))
.done();

String actual = Joiner.on(" ").join(ffmpeg.path(builder.build()));
Expand Down
36 changes: 32 additions & 4 deletions src/test/java/net/bramp/ffmpeg/FFmpegTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ public class FFmpegTest {

@Before
public void before() throws IOException {
when(runFunc.run(argThatHasItem("-version")))
.thenAnswer(new NewProcessAnswer("ffmpeg-version"));
when(runFunc.run(argThatHasItem("-formats")))
.thenAnswer(new NewProcessAnswer("ffmpeg-formats"));
when(runFunc.run(argThatHasItem("-codecs"))).thenAnswer(new NewProcessAnswer("ffmpeg-codecs"));
}

private void mockVersion0_10_9() throws IOException {
when(runFunc.run(argThatHasItem("-version")))
.thenAnswer(new NewProcessAnswer("ffmpeg-version_0.10.9"));
when(runFunc.run(argThatHasItem("-codecs")))
.thenAnswer(new NewProcessAnswer("ffmpeg-codecs_0.10.9"));
ffmpeg = new FFmpeg(runFunc);
}

private void mockVersion4_1_1() throws IOException {
when(runFunc.run(argThatHasItem("-version")))
.thenAnswer(new NewProcessAnswer("ffmpeg-version_4.1.1"));
when(runFunc.run(argThatHasItem("-codecs")))
.thenAnswer(new NewProcessAnswer("ffmpeg-codecs_4.1.1"));
ffmpeg = new FFmpeg(runFunc);
}

Expand All @@ -42,14 +53,29 @@ public static <T> List<T> argThatHasItem(T s) {

@Test
public void testVersion() throws Exception {
mockVersion0_10_9();

assertEquals("ffmpeg version 0.10.9-7:0.10.9-1~raring1", ffmpeg.version());
assertEquals("ffmpeg version 0.10.9-7:0.10.9-1~raring1", ffmpeg.version());

verify(runFunc, times(1)).run(argThatHasItem("-version"));
}

@Test
public void testCodecs() throws IOException {
public void testCodecsInLegacyVersions() throws IOException {
mockVersion0_10_9();

// Run twice, the second should be cached
assertEquals(Codecs.LEGACY_CODECS, ffmpeg.codecs());
assertEquals(Codecs.LEGACY_CODECS, ffmpeg.codecs());

verify(runFunc, times(1)).run(argThatHasItem("-codecs"));
}

@Test
public void testCodecsInModernVersions() throws IOException {
mockVersion4_1_1();

// Run twice, the second should be cached
assertEquals(Codecs.CODECS, ffmpeg.codecs());
assertEquals(Codecs.CODECS, ffmpeg.codecs());
Expand All @@ -59,6 +85,8 @@ public void testCodecs() throws IOException {

@Test
public void testFormats() throws IOException {
mockVersion0_10_9();

// Run twice, the second should be cached
assertEquals(Formats.FORMATS, ffmpeg.formats());
assertEquals(Formats.FORMATS, ffmpeg.formats());
Expand Down
Loading