mkv to mp4 conversion
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 19 Nov 2013 14:44:19 +0000 (16:44 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 19 Nov 2013 14:44:19 +0000 (16:44 +0200)
src/main/java/eu/svjatoslav/meviz/encoder/FormatsRegistry.java
src/main/java/eu/svjatoslav/meviz/encoder/converters/Mkv2Mp4.java [new file with mode: 0644]

index ab794c3..9555e6e 100755 (executable)
@@ -15,6 +15,7 @@ import java.util.List;
 import eu.svjatoslav.meviz.encoder.converters.Avi2Ogv;
 import eu.svjatoslav.meviz.encoder.converters.Converter;
 import eu.svjatoslav.meviz.encoder.converters.Jpeg2Png;
+import eu.svjatoslav.meviz.encoder.converters.Mkv2Mp4;
 import eu.svjatoslav.meviz.encoder.converters.Mp42Ogv;
 import eu.svjatoslav.meviz.encoder.converters.Mts2Mkv;
 import eu.svjatoslav.meviz.encoder.converters.Mts2Mp4;
@@ -26,47 +27,50 @@ import eu.svjatoslav.meviz.encoder.converters.Wav2mp3;
 
 public class FormatsRegistry {
 
-    public ArrayList<Converter> encoders = new ArrayList<Converter>();
+       public ArrayList<Converter> encoders = new ArrayList<Converter>();
 
-    public FormatsRegistry() {
-       // video conversion
-        registerEncoder(new Avi2Ogv());
-        registerEncoder(new Mp42Ogv());
-        registerEncoder(new Mts2Mp4());
-        registerEncoder(new Mts2Mkv());
+       public FormatsRegistry() {
+               // video conversion
+               registerEncoder(new Avi2Ogv());
+               registerEncoder(new Mp42Ogv());
+               registerEncoder(new Mts2Mp4());
+               registerEncoder(new Mts2Mkv());
+               registerEncoder(new Mkv2Mp4());
 
-        // image conversion
-        registerEncoder(new Jpeg2Png());
-        registerEncoder(new Png2Tiff());
-        registerEncoder(new Tiff2Png());
+               // image conversion
+               registerEncoder(new Jpeg2Png());
+               registerEncoder(new Png2Tiff());
+               registerEncoder(new Tiff2Png());
 
-        // audio conversion
-        registerEncoder(new Ogg2Wav());
-        registerEncoder(new Wav2mp3());
-        registerEncoder(new Ogg2Mp3());
-    }
+               // audio conversion
+               registerEncoder(new Ogg2Wav());
+               registerEncoder(new Wav2mp3());
+               registerEncoder(new Ogg2Mp3());
+       }
 
-    public List<Converter> getEncoders(final String sourceFormat, final String targetFormat) {
+       public List<Converter> getEncoders(final String sourceFormat,
+                       final String targetFormat) {
 
-        final String sourceFormatLowerCase = sourceFormat.toLowerCase();
-        final String targetFormatLowerCase = targetFormat.toLowerCase();
+               final String sourceFormatLowerCase = sourceFormat.toLowerCase();
+               final String targetFormatLowerCase = targetFormat.toLowerCase();
 
-        final ArrayList<Converter> encoders = new ArrayList<Converter>();
+               final ArrayList<Converter> encoders = new ArrayList<Converter>();
 
-        for (final Converter encoder : this.encoders) {
-            if (encoder.getSourceFileExtension().equals(sourceFormatLowerCase)) {
-                if (encoder.getTargetFileExtension().equals(targetFormatLowerCase)) {
-                    encoders.add(encoder);
-                    return encoders;
-                }
-            }
-        }
+               for (final Converter encoder : this.encoders) {
+                       if (encoder.getSourceFileExtension().equals(sourceFormatLowerCase)) {
+                               if (encoder.getTargetFileExtension().equals(
+                                               targetFormatLowerCase)) {
+                                       encoders.add(encoder);
+                                       return encoders;
+                               }
+                       }
+               }
 
-        return encoders;
-    }
+               return encoders;
+       }
 
-    public void registerEncoder(final Converter encoder) {
-        encoders.add(encoder);
-    }
+       public void registerEncoder(final Converter encoder) {
+               encoders.add(encoder);
+       }
 
 }
diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/Mkv2Mp4.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/Mkv2Mp4.java
new file mode 100644 (file)
index 0000000..dba7a1e
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Meviz - Various tools collection to work with multimedia.
+ * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.meviz.encoder.converters;
+
+import java.io.File;
+
+import eu.svjatoslav.meviz.encoder.EncodingOptions;
+
+public class Mkv2Mp4 implements Converter {
+
+       @Override
+       public String getCommand(final File inputFile, final File targetFile,
+                       final EncodingOptions options) {
+
+               int videoBitrate;
+               int audioBitrate;
+
+               switch (options.videoBitrate) {
+               case LOW:
+                       videoBitrate = 1000;
+                       audioBitrate = 128;
+                       break;
+
+               case MEDIUM:
+                       videoBitrate = 3500;
+                       audioBitrate = 128;
+                       break;
+
+               case HIGH:
+                       videoBitrate = 15000;
+                       audioBitrate = 500;
+                       break;
+
+               default:
+                       throw new RuntimeException("Video bitrate: " + options.videoBitrate
+                                       + " is not supported.");
+               }
+
+               // convert
+               final StringBuffer codecParams = new StringBuffer();
+
+               codecParams.append("-acodec libmp3lame -vcodec libx264");
+
+               codecParams.append(" -b " + videoBitrate + "k");
+               codecParams.append(" -b:a " + audioBitrate + "k");
+
+               if (options.deinterlace) {
+                       codecParams.append(" -filter:v yadif");
+               }
+
+               return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
+                               + codecParams.toString() + " \"" + targetFile.getAbsolutePath()
+                               + "\"";
+       }
+
+       @Override
+       public String getSourceFileExtension() {
+               return "mkv";
+       }
+
+       @Override
+       public String getTargetFileExtension() {
+               return "mp4";
+       }
+
+       @Override
+       public boolean isTerminalMandatory() {
+               return true;
+       }
+
+}