2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.encoder.converters;
12 import eu.svjatoslav.meviz.encoder.BitrateParameter;
13 import eu.svjatoslav.meviz.encoder.BitrateParameter.bitrate;
14 import eu.svjatoslav.meviz.encoder.EncodingOptions;
17 import java.util.List;
19 public class AvconvVideo extends AbstractConverter {
21 private String constructCodecParamsString(final EncodingOptions options,
22 final int videoBitrate, final int audioBitrate,
23 final String videoCodec, final String audioCodec) {
25 final StringBuilder codecParams = new StringBuilder();
27 if (audioCodec == null)
28 codecParams.append("-an ");
30 codecParams.append("-acodec " + audioCodec + " ");
32 codecParams.append("-vcodec " + videoCodec + " ");
34 if (videoBitrate != -1)
35 codecParams.append("-b " + videoBitrate + "k ");
37 if (audioBitrate != -1)
38 codecParams.append("-b:a " + audioBitrate + "k ");
40 if (options.isDeinterlace())
41 codecParams.append("-filter:v yadif ");
43 if (options.getTargetFps() != null)
44 codecParams.append("-r " + options.getTargetFps() + " ");
46 return codecParams.toString();
49 private int getAudioBitrateValue(final BitrateParameter.bitrate bitRate) {
67 throw new RuntimeException("Audio bitrate: " + bitRate
68 + " is not supported.");
73 public String getCommand(final File inputFile, final File targetFile,
74 final EncodingOptions options, final String targetFormat) {
76 int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
77 int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
80 final StringBuilder codecParams = new StringBuilder();
82 String videoCodec = "libx264";
83 String audioCodec = "libmp3lame";
85 if (targetFormat.equals("webm")) {
88 codecParams.append("-s 800x600 ");
91 if (options.getVideoBitrate() == bitrate.COPY)
94 if (options.getAudioBitrate() == bitrate.COPY)
97 if (options.getAudioBitrate() == bitrate.NONE)
100 if (options.isForPortablePlayer()) {
103 videoCodec = "libxvid";
106 codecParams.append("-s 640x480 ");
108 // enforce maximum keyframe interval
109 codecParams.append("-g 150 ");
112 codecParams.append(constructCodecParamsString(options, videoBitrate,
113 audioBitrate, videoCodec, audioCodec));
115 return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
116 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
121 public List<String> getSourceFileExtensions() {
122 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
127 public List<String> getTargetFileExtensions() {
128 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
132 private int getVideoBitrateValue(final BitrateParameter.bitrate bitRate) {
147 throw new RuntimeException("Video bitrate: " + bitRate
148 + " is not supported.");
153 public boolean isTerminalMandatory() {