Skip to content

Commit

Permalink
Fixed the parsing of the uppercase enums and added some extra tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bramp committed Mar 6, 2016
1 parent 25b00e4 commit d50e308
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/bramp/ffmpeg/FFmpegUtils.java
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.bramp.commons.lang3.math.gson.FractionAdapter;
import net.bramp.ffmpeg.gson.LowercaseEnumTypeAdapterFactory;
import net.bramp.ffmpeg.gson.NamedBitsetAdapter;
import net.bramp.ffmpeg.io.ProcessUtils;
import net.bramp.ffmpeg.probe.FFmpegDisposition;
Expand Down Expand Up @@ -66,6 +67,8 @@ static Gson getGson() {

private static Gson setupGson() {
GsonBuilder builder = new GsonBuilder();

builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
builder.registerTypeAdapter(Fraction.class, new FractionAdapter());
builder.registerTypeAdapter(FFmpegDisposition.class, new NamedBitsetAdapter<>(
FFmpegDisposition.class));
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/net/bramp/ffmpeg/FFprobe.java
Expand Up @@ -77,11 +77,19 @@ private BufferedReader wrapInReader(Process p) {
public FFmpegProbeResult probe(String mediaPath) throws IOException {
ImmutableList.Builder<String> args = new ImmutableList.Builder<String>();

// TODO Add:
// .add("--show_packets")
// .add("--show_frames")

args.add(path).add("-v", "quiet").add("-print_format", "json").add("-show_error")
.add("-show_format").add("-show_streams").add(mediaPath);
// @formatter:off
args.add(path)
.add("-v", "quiet")
.add("-print_format", "json")
.add("-show_error")
.add("-show_format")
.add("-show_streams")
.add(mediaPath);
// @formatter:on

Process p = runFunc.run(args.build());
try {
Expand Down
@@ -0,0 +1,54 @@
package net.bramp.ffmpeg.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* Taken from https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/TypeAdapterFactory.html
*/
public class LowercaseEnumTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (!rawType.isEnum()) {
return null;
}

// Setup mapping of consts
final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
for (T constant : rawType.getEnumConstants()) {
lowercaseToConstant.put(toLowercase(constant), constant);
}

return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(toLowercase(value));
}
}

public T read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
return lowercaseToConstant.get(reader.nextString());
}
};
}

private String toLowercase(Object o) {
return o.toString().toLowerCase(Locale.UK);
}
}
34 changes: 32 additions & 2 deletions src/test/java/net/bramp/ffmpeg/FFprobeTest.java
Expand Up @@ -4,6 +4,7 @@
import net.bramp.ffmpeg.fixtures.Samples;
import net.bramp.ffmpeg.lang.NewProcessAnswer;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
import net.bramp.ffmpeg.probe.FFmpegStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -13,7 +14,10 @@
import java.io.IOException;

import static net.bramp.ffmpeg.FFmpegTest.argThatHasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -30,13 +34,39 @@ public class FFprobeTest {
public void before() throws IOException {
when(runFunc.run(argThatHasItem(Samples.big_buck_bunny_720p_1mb))).thenAnswer(
new NewProcessAnswer("ffprobe-big_buck_bunny_720p_1mb.mp4"));
ffprobe = new FFprobe();

when(runFunc.run(argThatHasItem(Samples.always_on_my_mind))).thenAnswer(
new NewProcessAnswer("ffprobe-Always On My Mind [Program Only] - Adelén.mp4"));

ffprobe = new FFprobe(runFunc);
}

@Test
public void testProbe() throws IOException {
public void testProbeVideo() throws IOException {
FFmpegProbeResult info = ffprobe.probe(Samples.big_buck_bunny_720p_1mb);
assertFalse(info.hasError());

// Only a quick sanity check until we do something better
assertThat(info.getStreams(), hasSize(2));
assertThat(info.getStreams().get(0).codec_type, is(FFmpegStream.CodecType.VIDEO));
assertThat(info.getStreams().get(1).codec_type, is(FFmpegStream.CodecType.AUDIO));

System.out.println(FFmpegUtils.getGson().toJson(info));
}

@Test
public void testProbeVideo2() throws IOException {
FFmpegProbeResult info = ffprobe.probe(Samples.always_on_my_mind);
assertFalse(info.hasError());

// Only a quick sanity check until we do something better
assertThat(info.getStreams(), hasSize(2));
assertThat(info.getStreams().get(0).codec_type, is(FFmpegStream.CodecType.VIDEO));
assertThat(info.getStreams().get(1).codec_type, is(FFmpegStream.CodecType.AUDIO));

// Test a UTF-8 name
assertThat(info.getFormat().filename, is("c:\\Users\\Bob\\Always On My Mind [Program Only] - Adelén.mp4"));

System.out.println(FFmpegUtils.getGson().toJson(info));
}

Expand Down

0 comments on commit d50e308

Please sign in to comment.