possibility to change video framerate
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / converters / AvconvVideo.java
index de295ca..4d2743a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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.
@@ -12,57 +12,105 @@ package eu.svjatoslav.meviz.encoder.converters;
 import java.io.File;
 import java.util.List;
 
+import eu.svjatoslav.meviz.encoder.BitrateParameter;
+import eu.svjatoslav.meviz.encoder.BitrateParameter.bitrate;
 import eu.svjatoslav.meviz.encoder.EncodingOptions;
 
 public class AvconvVideo extends AbstractConverter {
 
-       @Override
-       public String getCommand(final File inputFile, final File targetFile,
-                       final EncodingOptions options) {
+       private String constructCodecParamsString(final EncodingOptions options,
+                       final int videoBitrate, final int audioBitrate,
+                       final String videoCodec, final String audioCodec) {
+
+               final StringBuffer codecParams = new StringBuffer();
+
+               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() + " ");
 
-               int videoBitrate;
-               int audioBitrate;
+               return codecParams.toString();
+       }
 
-               switch (options.videoBitrate) {
+       private int getAudioBitrateValue(final BitrateParameter.bitrate bitRate) {
+               switch (bitRate) {
                case LOW:
-                       videoBitrate = 1000;
-                       audioBitrate = 128;
-                       break;
+                       return 128;
 
                case MEDIUM:
-                       videoBitrate = 3500;
-                       audioBitrate = 128;
-                       break;
+                       return 160;
 
                case HIGH:
-                       videoBitrate = 40000;
-                       audioBitrate = 500;
-                       break;
+                       return 320;
+
+               case COPY:
+                       return -1;
+
+               case NONE:
+                       return -1;
 
                default:
-                       throw new RuntimeException("Video bitrate: " + options.videoBitrate
+                       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 StringBuffer codecParams = new StringBuffer();
 
-               String videoCodec = "libx264 ";
+               String videoCodec = "libx264";
+               String audioCodec = "libmp3lame";
 
-               if (options.forPortablePlayer) {
+               if (targetFormat.equals("webm")) {
+                       videoCodec = "vp8";
+                       audioCodec = "opus";
+                       codecParams.append("-s 800x600 ");
+               }
+
+               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 ";
-                       codecParams.append("-s 640x480 ");
-               }
+                       videoCodec = "libxvid";
 
-               codecParams.append("-acodec libmp3lame -vcodec " + videoCodec);
+                       // reduce resolution
+                       codecParams.append("-s 640x480 ");
 
-               codecParams.append("-b " + videoBitrate + "k ");
-               codecParams.append("-b:a " + audioBitrate + "k ");
+                       // enforce maximum keyframe interval
+                       codecParams.append("-g 150 ");
+               }
 
-               if (options.deinterlace)
-                       codecParams.append("-filter:v yadif ");
+               codecParams.append(constructCodecParamsString(options, videoBitrate,
+                               audioBitrate, videoCodec, audioCodec));
 
                return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
                                + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
@@ -71,12 +119,34 @@ public class AvconvVideo extends AbstractConverter {
 
        @Override
        public List<String> getSourceFileExtensions() {
-               return toList("mkv", "mts", "mp4", "avi");
+               return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
+                               "webm");
        }
 
        @Override
        public List<String> getTargetFileExtensions() {
-               return toList("mkv", "mts", "mp4", "avi");
+               return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
+                               "webm");
+       }
+
+       private int getVideoBitrateValue(final BitrateParameter.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