2 * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.meviz.encoder.converters;
9 import eu.svjatoslav.meviz.encoder.BitrateParameter.Bitrate;
10 import eu.svjatoslav.meviz.encoder.EncodingOptions;
13 import java.util.List;
15 public class FFMpegVideo extends AbstractConverter {
17 private String constructCodecParamsString(final EncodingOptions options,
18 final int videoBitrate, final int audioBitrate,
19 final String videoCodec, final String audioCodec) {
21 final StringBuilder codecParams = new StringBuilder();
23 if (audioCodec == null)
24 codecParams.append("-an ");
26 codecParams.append("-acodec " + audioCodec + " ");
28 codecParams.append("-vcodec " + videoCodec + " ");
30 if (videoBitrate != -1)
31 codecParams.append("-b " + videoBitrate + "k ");
33 if (audioBitrate != -1)
34 codecParams.append("-b:a " + audioBitrate + "k ");
36 if (options.isDeinterlace())
37 codecParams.append("-filter:v yadif ");
39 if (options.getTargetFps() != null)
40 codecParams.append("-r " + options.getTargetFps() + " ");
42 return codecParams.toString();
45 private int getAudioBitrateValue(final Bitrate bitRate) {
66 throw new RuntimeException("Audio bitrate: " + bitRate
67 + " is not supported.");
72 public String getCommand(final File inputFile, final File targetFile,
73 final EncodingOptions options, final String targetFormat) {
75 int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
76 int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
79 final StringBuilder codecParams = new StringBuilder();
81 String videoCodec = "libx264";
82 String audioCodec = "libmp3lame";
84 if (targetFormat.equals("webm")) {
87 codecParams.append("-s 800x450 ");
90 if (options.getVideoBitrate() == Bitrate.COPY)
93 if (options.getAudioBitrate() == Bitrate.COPY)
96 if (options.getAudioBitrate() == Bitrate.LOSSLESS){
98 codecParams.append("-strict -2 ");
101 if (options.getAudioBitrate() == Bitrate.NONE)
104 if (options.isForPortablePlayer()) {
107 videoCodec = "libxvid";
110 codecParams.append("-s 640x480 ");
112 // enforce maximum keyframe interval
113 codecParams.append("-g 150 ");
116 codecParams.append(constructCodecParamsString(options, videoBitrate,
117 audioBitrate, videoCodec, audioCodec));
119 return "ffmpeg -i \"" + inputFile.getAbsolutePath() + "\" "
120 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
125 public List<String> getSourceFileExtensions() {
126 return getSupportedExtensions();
129 private List<String> getSupportedExtensions() {
130 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
131 "webm", "mov", "asf", "3gp");
135 public List<String> getTargetFileExtensions() {
136 return getSupportedExtensions();
139 private int getVideoBitrateValue(final Bitrate bitRate) {
154 throw new RuntimeException("Video bitrate: " + bitRate
155 + " is not supported.");
160 public boolean isTerminalMandatory() {