From eacf70f1d7bc711d2315926ad25ee85f0398562f Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Mon, 30 Jul 2018 21:37:02 -0400 Subject: [PATCH 1/2] Add support for extra FFmpeg args per codec At least one codec requires extra FFmpeg args. This will keep the extra args wit the codec's entry. --- .../videotranscoder/activity/MainActivity.java | 10 +++------- .../videotranscoder/media/AudioCodec.java | 16 ++++++++++------ .../videotranscoder/media/VideoCodec.java | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/protect/videotranscoder/activity/MainActivity.java b/app/src/main/java/protect/videotranscoder/activity/MainActivity.java index 5c118d7d..46ca641a 100644 --- a/app/src/main/java/protect/videotranscoder/activity/MainActivity.java +++ b/app/src/main/java/protect/videotranscoder/activity/MainActivity.java @@ -680,6 +680,8 @@ private List getFfmpegEncodingArgs(String inputFilePath, Integer startTi command.add(videoBitrateK + "k"); } + command.addAll(videoCodec.extraFfmpegArgs); + // Frame size command.add("-s"); command.add(resolution); @@ -699,13 +701,7 @@ private List getFfmpegEncodingArgs(String inputFilePath, Integer startTi command.add("-acodec"); command.add(audioCodec.ffmpegName); - if (audioCodec == AudioCodec.VORBIS) - { - // The vorbis encode is experimental, and needs other - // flags to enable - command.add("-strict"); - command.add("-2"); - } + command.addAll(audioCodec.extraFfmpegArgs); // Sample rate command.add("-ar"); diff --git a/app/src/main/java/protect/videotranscoder/media/AudioCodec.java b/app/src/main/java/protect/videotranscoder/media/AudioCodec.java index 189d0578..4dfcd4a9 100644 --- a/app/src/main/java/protect/videotranscoder/media/AudioCodec.java +++ b/app/src/main/java/protect/videotranscoder/media/AudioCodec.java @@ -11,19 +11,23 @@ */ public enum AudioCodec { - AAC("aac", Arrays.asList("1", "2")), - MP3("mp3", Arrays.asList("1", "2")), - OPUS("libopus", Arrays.asList("1", "2")), - VORBIS("vorbis", Collections.singletonList("2")), - NONE("none", Collections.EMPTY_LIST), + AAC("aac", Collections.EMPTY_LIST, Arrays.asList("1", "2")), + MP3("mp3", Collections.EMPTY_LIST, Arrays.asList("1", "2")), + OPUS("libopus", Collections.EMPTY_LIST, Arrays.asList("1", "2")), + + // The vorbis encode is experimental, and needs other flags to enable + VORBIS("vorbis", Arrays.asList("-strict", "-2"), Collections.singletonList("2")), + NONE("none", Collections.EMPTY_LIST, Collections.EMPTY_LIST), ; public final String ffmpegName; + public final List extraFfmpegArgs; public final List supportedChannels; - AudioCodec(String ffmpegName, List supportedChannels) + AudioCodec(String ffmpegName, List extraFfmpegArgs, List supportedChannels) { this.ffmpegName = ffmpegName; + this.extraFfmpegArgs = extraFfmpegArgs; this.supportedChannels = Collections.unmodifiableList(supportedChannels); } diff --git a/app/src/main/java/protect/videotranscoder/media/VideoCodec.java b/app/src/main/java/protect/videotranscoder/media/VideoCodec.java index d01846af..d1bbe906 100644 --- a/app/src/main/java/protect/videotranscoder/media/VideoCodec.java +++ b/app/src/main/java/protect/videotranscoder/media/VideoCodec.java @@ -2,6 +2,10 @@ import android.support.annotation.Nullable; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import protect.videotranscoder.R; /** @@ -9,21 +13,23 @@ */ public enum VideoCodec { - H264("h264", "H.264", R.string.codecSlowExcellent), - MPEG4("mpeg4", "MPEG-4", R.string.codecFastGood), - MPEG2("mpeg2video", "MPEG-2", R.string.codecFastOk), - MPEG1("mpeg1video", "MPEG-1", R.string.codecFastLow), - GIF("gif", "GIF", null), + H264("h264", "H.264", Collections.EMPTY_LIST, R.string.codecSlowExcellent), + MPEG4("mpeg4", "MPEG-4", Collections.EMPTY_LIST, R.string.codecFastGood), + MPEG2("mpeg2video", "MPEG-2", Collections.EMPTY_LIST, R.string.codecFastOk), + MPEG1("mpeg1video", "MPEG-1", Collections.EMPTY_LIST, R.string.codecFastLow), + GIF("gif", "GIF", Collections.EMPTY_LIST, null), ; public final String ffmpegName; public final String prettyName; + public final List extraFfmpegArgs; public final Integer helperTextId; - VideoCodec(String ffmpegName, String prettyName, Integer helperTextId) + VideoCodec(String ffmpegName, String prettyName, List extraFfmpegArgs, Integer helperTextId) { this.ffmpegName = ffmpegName; this.prettyName = prettyName; + this.extraFfmpegArgs = extraFfmpegArgs; this.helperTextId = helperTextId; } From b8b77eee7ded756fb178a70985623ed8d2424d2b Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Mon, 30 Jul 2018 21:39:56 -0400 Subject: [PATCH 2/2] Set the 'preset' setting for h264 to 'faster' An article examined the time/quality trade-off for h264 and concluded that the best default option was 'faster'. Compared to 'medium', which is the default for FFmpeg, 'faster' reduced encoding times ~73% and reduced the quality in a likely imperceptible manner. This sounds like a good trade-off generally, especially for a mobile device where power usage is a concern. This commit changes the 'preset' setting to 'faster'. The article was found here: https://streaminglearningcenter.com/blogs/saving-encoding-adjust-encoding-configuration-increase-capacity.html --- .../main/java/protect/videotranscoder/media/VideoCodec.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/protect/videotranscoder/media/VideoCodec.java b/app/src/main/java/protect/videotranscoder/media/VideoCodec.java index d1bbe906..ea60751d 100644 --- a/app/src/main/java/protect/videotranscoder/media/VideoCodec.java +++ b/app/src/main/java/protect/videotranscoder/media/VideoCodec.java @@ -13,7 +13,11 @@ */ public enum VideoCodec { - H264("h264", "H.264", Collections.EMPTY_LIST, R.string.codecSlowExcellent), + // The 'preset' setting for h264 is changed from its default of 'medium'. The + // 'faster' setting reduces encoding times by ~73% while only reducing quality + // a near imperceptible amount. This seems like a good trade-off for encoding + // on a mobile devices where power usage is a concern. + H264("h264", "H.264", Arrays.asList("-preset", "faster"), R.string.codecSlowExcellent), MPEG4("mpeg4", "MPEG-4", Collections.EMPTY_LIST, R.string.codecFastGood), MPEG2("mpeg2video", "MPEG-2", Collections.EMPTY_LIST, R.string.codecFastOk), MPEG1("mpeg1video", "MPEG-1", Collections.EMPTY_LIST, R.string.codecFastLow),