/*
* Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2012 -- 2018, 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
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;
-import eu.svjatoslav.meviz.encoder.BitrateParameter;
-import eu.svjatoslav.meviz.encoder.BitrateParameter.bitrate;
-import eu.svjatoslav.meviz.encoder.EncodingOptions;
-
public class AvconvVideo extends AbstractConverter {
- private String constructCodecParamsString(final EncodingOptions options,
- final int videoBitrate, final int audioBitrate,
- final String videoCodec, final String audioCodec) {
+ private String constructCodecParamsString(final EncodingOptions options,
+ final int videoBitrate, final int audioBitrate,
+ final String videoCodec, final String audioCodec) {
+
+ final StringBuilder codecParams = new StringBuilder();
- final StringBuffer codecParams = new StringBuffer();
+ if (audioCodec == null)
+ codecParams.append("-an ");
+ else
+ codecParams.append("-acodec " + audioCodec + " ");
- if (audioCodec == null)
- codecParams.append("-an ");
- else
- codecParams.append("-acodec " + audioCodec + " ");
+ codecParams.append("-vcodec " + videoCodec + " ");
- codecParams.append("-vcodec " + videoCodec + " ");
+ if (videoBitrate != -1)
+ codecParams.append("-b " + videoBitrate + "k ");
- if (videoBitrate != -1)
- codecParams.append("-b " + videoBitrate + "k ");
+ if (audioBitrate != -1)
+ codecParams.append("-b:a " + audioBitrate + "k ");
- if (audioBitrate != -1)
- codecParams.append("-b:a " + audioBitrate + "k ");
+ if (options.isDeinterlace())
+ codecParams.append("-filter:v yadif ");
- if (options.isDeinterlace())
- codecParams.append("-filter:v yadif ");
+ if (options.getTargetFps() != null)
+ codecParams.append("-r " + options.getTargetFps() + " ");
- if (options.getTargetFps() != null)
- codecParams.append("-r " + options.getTargetFps() + " ");
+ return codecParams.toString();
+ }
- return codecParams.toString();
- }
+ private int getAudioBitrateValue(final Bitrate bitRate) {
+ switch (bitRate) {
+ case LOW:
+ return 128;
- private int getAudioBitrateValue(final BitrateParameter.bitrate bitRate) {
- switch (bitRate) {
- case LOW:
- return 128;
+ case MEDIUM:
+ return 160;
- case MEDIUM:
- return 160;
+ case HIGH:
+ return 320;
- case HIGH:
- return 320;
+ case COPY:
+ return -1;
- case COPY:
- return -1;
+ case NONE:
+ return -1;
- case NONE:
- return -1;
+ default:
+ throw new RuntimeException("Audio bitrate: " + bitRate
+ + " is not supported.");
+ }
+ }
- 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) {
- @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());
- int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
- int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
+ // convert
+ final StringBuilder codecParams = new StringBuilder();
- // convert
- final StringBuffer codecParams = new StringBuffer();
+ String videoCodec = "libx264";
+ String audioCodec = "libmp3lame";
- String videoCodec = "libx264";
- String audioCodec = "libmp3lame";
+ if (targetFormat.equals("webm")) {
+ videoCodec = "vp8";
+ audioCodec = "opus";
+ codecParams.append("-s 800x450 ");
+ }
- if (targetFormat.equals("webm")) {
- videoCodec = "vp8";
- audioCodec = "opus";
- codecParams.append("-s 800x600 ");
- }
+ if (options.getVideoBitrate() == Bitrate.COPY)
+ videoCodec = "copy";
- if (options.getVideoBitrate() == bitrate.COPY)
- videoCodec = "copy";
+ if (options.getAudioBitrate() == Bitrate.COPY)
+ audioCodec = "copy";
- if (options.getAudioBitrate() == bitrate.COPY)
- audioCodec = "copy";
+ if (options.getAudioBitrate() == Bitrate.NONE)
+ audioCodec = null;
- if (options.getAudioBitrate() == bitrate.NONE)
- audioCodec = null;
+ if (options.isForPortablePlayer()) {
+ videoBitrate = 1000;
+ audioBitrate = 128;
+ videoCodec = "libxvid";
- if (options.isForPortablePlayer()) {
- videoBitrate = 1000;
- audioBitrate = 128;
- videoCodec = "libxvid";
+ // reduce resolution
+ codecParams.append("-s 640x480 ");
- // reduce resolution
- codecParams.append("-s 640x480 ");
+ // enforce maximum keyframe interval
+ codecParams.append("-g 150 ");
+ }
- // enforce maximum keyframe interval
- codecParams.append("-g 150 ");
- }
+ codecParams.append(constructCodecParamsString(options, videoBitrate,
+ audioBitrate, videoCodec, audioCodec));
- codecParams.append(constructCodecParamsString(options, videoBitrate,
- audioBitrate, videoCodec, audioCodec));
+ return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
+ + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
+ + "\"";
+ }
- return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
- + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
- + "\"";
- }
+ @Override
+ public List<String> getSourceFileExtensions() {
+ return getSupportedExtensions();
+ }
- @Override
- public List<String> getSourceFileExtensions() {
- return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
- "webm");
- }
+ private List<String> getSupportedExtensions() {
+ return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
+ "webm", "mov", "asf", "3gp");
+ }
- @Override
- public List<String> getTargetFileExtensions() {
- return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
- "webm");
- }
+ @Override
+ public List<String> getTargetFileExtensions() {
+ return getSupportedExtensions();
+ }
- private int getVideoBitrateValue(final BitrateParameter.bitrate bitRate) {
- switch (bitRate) {
- case LOW:
- return 1000;
+ private int getVideoBitrateValue(final Bitrate bitRate) {
+ switch (bitRate) {
+ case LOW:
+ return 1000;
- case MEDIUM:
- return 4000;
+ case MEDIUM:
+ return 4000;
- case HIGH:
- return 16000;
+ case HIGH:
+ return 16000;
- case COPY:
- return -1;
+ case COPY:
+ return -1;
- default:
- throw new RuntimeException("Video bitrate: " + bitRate
- + " is not supported.");
- }
- }
+ default:
+ throw new RuntimeException("Video bitrate: " + bitRate
+ + " is not supported.");
+ }
+ }
- @Override
- public boolean isTerminalMandatory() {
- return true;
- }
+ @Override
+ public boolean isTerminalMandatory() {
+ return true;
+ }
}