Use FFMpeg instead of Avconv
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 1 Jan 2019 12:10:47 +0000 (14:10 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 1 Jan 2019 12:10:47 +0000 (14:10 +0200)
src/main/java/eu/svjatoslav/meviz/encoder/FormatsRegistry.java
src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvAudio.java [deleted file]
src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java [deleted file]
src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegAudio.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegVideo.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java

index 0ceffeb..b106cba 100755 (executable)
@@ -21,7 +21,7 @@ public class FormatsRegistry {
     public FormatsRegistry() {
         // video conversion
         registerEncoder(new Ffmpeg2theora());
-        registerEncoder(new AvconvVideo());
+        registerEncoder(new FFMpegVideo());
 
         // image conversion
         registerEncoder(new Convert());
@@ -32,7 +32,7 @@ public class FormatsRegistry {
         // audio conversion
         registerEncoder(new Ogg2Wav());
         registerEncoder(new Flac());
-        registerEncoder(new AvconvAudio());
+        registerEncoder(new FFMpegAudio());
         registerEncoder(new Midi2Wav());
     }
 
diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvAudio.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvAudio.java
deleted file mode 100755 (executable)
index ae6cd28..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
-*/
-
-package eu.svjatoslav.meviz.encoder.converters;
-
-import eu.svjatoslav.meviz.encoder.EncodingOptions;
-
-import java.io.File;
-import java.util.List;
-
-public class AvconvAudio extends AbstractConverter {
-
-    private static final String[] SUPPORTED_FORMATS = new String[]{"ogg", "wav", "mp3", "m4a", "flac"};
-
-    @Override
-    public String getCommand(final File inputFile, final File targetFile,
-                             final EncodingOptions options, String targetFormat) {
-
-        final String codecParams = "-b:a 192k";
-
-        return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
-                + codecParams + " \"" + targetFile.getAbsolutePath() + "\"";
-    }
-
-    @Override
-    public List<String> getSourceFileExtensions() {
-        return toList(SUPPORTED_FORMATS);
-    }
-
-    @Override
-    public List<String> getTargetFileExtensions() {
-        return toList(SUPPORTED_FORMATS);
-    }
-
-    @Override
-    public boolean isTerminalMandatory() {
-        return false;
-    }
-
-}
diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java
deleted file mode 100644 (file)
index e8ed780..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
-*/
-
-package eu.svjatoslav.meviz.encoder.converters;
-
-import eu.svjatoslav.meviz.encoder.BitrateParameter.Bitrate;
-import eu.svjatoslav.meviz.encoder.EncodingOptions;
-
-import java.io.File;
-import java.util.List;
-
-public class AvconvVideo extends AbstractConverter {
-
-    private String constructCodecParamsString(final EncodingOptions options,
-                                              final int videoBitrate, final int audioBitrate,
-                                              final String videoCodec, final String audioCodec) {
-
-        final StringBuilder codecParams = new StringBuilder();
-
-        if (audioCodec == null)
-            codecParams.append("-an ");
-        else
-            codecParams.append("-acodec " + audioCodec + " ");
-
-        codecParams.append("-vcodec " + videoCodec + " ");
-
-        if (videoBitrate != -1)
-            codecParams.append("-b " + videoBitrate + "k ");
-
-        if (audioBitrate != -1)
-            codecParams.append("-b:a " + audioBitrate + "k ");
-
-        if (options.isDeinterlace())
-            codecParams.append("-filter:v yadif ");
-
-        if (options.getTargetFps() != null)
-            codecParams.append("-r " + options.getTargetFps() + " ");
-
-        return codecParams.toString();
-    }
-
-    private int getAudioBitrateValue(final Bitrate bitRate) {
-        switch (bitRate) {
-            case LOW:
-                return 128;
-
-            case MEDIUM:
-                return 160;
-
-            case HIGH:
-                return 320;
-
-            case COPY:
-                return -1;
-
-            case NONE:
-                return -1;
-
-            default:
-                throw new RuntimeException("Audio bitrate: " + bitRate
-                        + " is not supported.");
-        }
-    }
-
-    @Override
-    public String getCommand(final File inputFile, final File targetFile,
-                             final EncodingOptions options, final String targetFormat) {
-
-        int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
-        int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
-
-        // convert
-        final StringBuilder codecParams = new StringBuilder();
-
-        String videoCodec = "libx264";
-        String audioCodec = "libmp3lame";
-
-        if (targetFormat.equals("webm")) {
-            videoCodec = "vp8";
-            audioCodec = "opus";
-            codecParams.append("-s 800x450 ");
-        }
-
-        if (options.getVideoBitrate() == Bitrate.COPY)
-            videoCodec = "copy";
-
-        if (options.getAudioBitrate() == Bitrate.COPY)
-            audioCodec = "copy";
-
-        if (options.getAudioBitrate() == Bitrate.NONE)
-            audioCodec = null;
-
-        if (options.isForPortablePlayer()) {
-            videoBitrate = 1000;
-            audioBitrate = 128;
-            videoCodec = "libxvid";
-
-            // reduce resolution
-            codecParams.append("-s 640x480 ");
-
-            // enforce maximum keyframe interval
-            codecParams.append("-g 150 ");
-        }
-
-        codecParams.append(constructCodecParamsString(options, videoBitrate,
-                audioBitrate, videoCodec, audioCodec));
-
-        return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
-                + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
-                + "\"";
-    }
-
-    @Override
-    public List<String> getSourceFileExtensions() {
-        return getSupportedExtensions();
-    }
-
-    private List<String> getSupportedExtensions() {
-        return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
-                "webm", "mov", "asf", "3gp");
-    }
-
-    @Override
-    public List<String> getTargetFileExtensions() {
-        return getSupportedExtensions();
-    }
-
-    private int getVideoBitrateValue(final Bitrate bitRate) {
-        switch (bitRate) {
-            case LOW:
-                return 1000;
-
-            case MEDIUM:
-                return 4000;
-
-            case HIGH:
-                return 16000;
-
-            case COPY:
-                return -1;
-
-            default:
-                throw new RuntimeException("Video bitrate: " + bitRate
-                        + " is not supported.");
-        }
-    }
-
-    @Override
-    public boolean isTerminalMandatory() {
-        return true;
-    }
-
-}
diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegAudio.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegAudio.java
new file mode 100755 (executable)
index 0000000..11a787b
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Meviz - Various tools collection to work with multimedia.
+ * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+*/
+
+package eu.svjatoslav.meviz.encoder.converters;
+
+import eu.svjatoslav.meviz.encoder.EncodingOptions;
+
+import java.io.File;
+import java.util.List;
+
+public class FFMpegAudio extends AbstractConverter {
+
+    private static final String[] SUPPORTED_FORMATS = new String[]{"ogg", "wav", "mp3", "m4a", "flac"};
+
+    @Override
+    public String getCommand(final File inputFile, final File targetFile,
+                             final EncodingOptions options, String targetFormat) {
+
+        final String codecParams = "-b:a 192k";
+
+        return "ffmpeg -i \"" + inputFile.getAbsolutePath() + "\" "
+                + codecParams + " \"" + targetFile.getAbsolutePath() + "\"";
+    }
+
+    @Override
+    public List<String> getSourceFileExtensions() {
+        return toList(SUPPORTED_FORMATS);
+    }
+
+    @Override
+    public List<String> getTargetFileExtensions() {
+        return toList(SUPPORTED_FORMATS);
+    }
+
+    @Override
+    public boolean isTerminalMandatory() {
+        return false;
+    }
+
+}
diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegVideo.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/FFMpegVideo.java
new file mode 100644 (file)
index 0000000..3cc3da9
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Meviz - Various tools collection to work with multimedia.
+ * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+*/
+
+package eu.svjatoslav.meviz.encoder.converters;
+
+import eu.svjatoslav.meviz.encoder.BitrateParameter.Bitrate;
+import eu.svjatoslav.meviz.encoder.EncodingOptions;
+
+import java.io.File;
+import java.util.List;
+
+public class FFMpegVideo extends AbstractConverter {
+
+    private String constructCodecParamsString(final EncodingOptions options,
+                                              final int videoBitrate, final int audioBitrate,
+                                              final String videoCodec, final String audioCodec) {
+
+        final StringBuilder codecParams = new StringBuilder();
+
+        if (audioCodec == null)
+            codecParams.append("-an ");
+        else
+            codecParams.append("-acodec " + audioCodec + " ");
+
+        codecParams.append("-vcodec " + videoCodec + " ");
+
+        if (videoBitrate != -1)
+            codecParams.append("-b " + videoBitrate + "k ");
+
+        if (audioBitrate != -1)
+            codecParams.append("-b:a " + audioBitrate + "k ");
+
+        if (options.isDeinterlace())
+            codecParams.append("-filter:v yadif ");
+
+        if (options.getTargetFps() != null)
+            codecParams.append("-r " + options.getTargetFps() + " ");
+
+        return codecParams.toString();
+    }
+
+    private int getAudioBitrateValue(final Bitrate bitRate) {
+        switch (bitRate) {
+            case LOW:
+                return 128;
+
+            case MEDIUM:
+                return 160;
+
+            case HIGH:
+                return 320;
+
+            case COPY:
+                return -1;
+
+            case NONE:
+                return -1;
+
+            default:
+                throw new RuntimeException("Audio bitrate: " + bitRate
+                        + " is not supported.");
+        }
+    }
+
+    @Override
+    public String getCommand(final File inputFile, final File targetFile,
+                             final EncodingOptions options, final String targetFormat) {
+
+        int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
+        int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
+
+        // convert
+        final StringBuilder codecParams = new StringBuilder();
+
+        String videoCodec = "libx264";
+        String audioCodec = "libmp3lame";
+
+        if (targetFormat.equals("webm")) {
+            videoCodec = "vp8";
+            audioCodec = "opus";
+            codecParams.append("-s 800x450 ");
+        }
+
+        if (options.getVideoBitrate() == Bitrate.COPY)
+            videoCodec = "copy";
+
+        if (options.getAudioBitrate() == Bitrate.COPY)
+            audioCodec = "copy";
+
+        if (options.getAudioBitrate() == Bitrate.NONE)
+            audioCodec = null;
+
+        if (options.isForPortablePlayer()) {
+            videoBitrate = 1000;
+            audioBitrate = 128;
+            videoCodec = "libxvid";
+
+            // reduce resolution
+            codecParams.append("-s 640x480 ");
+
+            // enforce maximum keyframe interval
+            codecParams.append("-g 150 ");
+        }
+
+        codecParams.append(constructCodecParamsString(options, videoBitrate,
+                audioBitrate, videoCodec, audioCodec));
+
+        return "ffmpeg -i \"" + inputFile.getAbsolutePath() + "\" "
+                + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
+                + "\"";
+    }
+
+    @Override
+    public List<String> getSourceFileExtensions() {
+        return getSupportedExtensions();
+    }
+
+    private List<String> getSupportedExtensions() {
+        return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
+                "webm", "mov", "asf", "3gp");
+    }
+
+    @Override
+    public List<String> getTargetFileExtensions() {
+        return getSupportedExtensions();
+    }
+
+    private int getVideoBitrateValue(final Bitrate bitRate) {
+        switch (bitRate) {
+            case LOW:
+                return 1000;
+
+            case MEDIUM:
+                return 4000;
+
+            case HIGH:
+                return 16000;
+
+            case COPY:
+                return -1;
+
+            default:
+                throw new RuntimeException("Video bitrate: " + bitRate
+                        + " is not supported.");
+        }
+    }
+
+    @Override
+    public boolean isTerminalMandatory() {
+        return true;
+    }
+
+}
index 4000e7e..aa8c42b 100755 (executable)
@@ -17,7 +17,7 @@ import eu.svjatoslav.meviz.encoder.EncodingOptions;
 import eu.svjatoslav.meviz.encoder.EncodingPlan;
 import eu.svjatoslav.meviz.encoder.EncodingTask;
 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
-import eu.svjatoslav.meviz.encoder.converters.AvconvVideo;
+import eu.svjatoslav.meviz.encoder.converters.FFMpegVideo;
 
 import java.io.File;
 import java.io.IOException;
@@ -94,7 +94,7 @@ public class Main implements Module {
         final File targetDirectory = getTargetDirectory();
         targetDirectory.mkdirs();
 
-        final AbstractConverter converter = new AvconvVideo();
+        final AbstractConverter converter = new FFMpegVideo();
         final EncodingPlan encodingPlan = new EncodingPlan();
 
         final String userName = System.getProperty("user.name");