2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2019, 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 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.encoder.converters;
12 import eu.svjatoslav.meviz.encoder.BitrateParameter.Bitrate;
13 import eu.svjatoslav.meviz.encoder.EncodingOptions;
16 import java.util.List;
18 public class FFMpegVideo extends AbstractConverter {
20 private String constructCodecParamsString(final EncodingOptions options,
21 final int videoBitrate, final int audioBitrate,
22 final String videoCodec, final String audioCodec) {
24 final StringBuilder codecParams = new StringBuilder();
26 if (audioCodec == null)
27 codecParams.append("-an ");
29 codecParams.append("-acodec " + audioCodec + " ");
31 codecParams.append("-vcodec " + videoCodec + " ");
33 if (videoBitrate != -1)
34 codecParams.append("-b " + videoBitrate + "k ");
36 if (audioBitrate != -1)
37 codecParams.append("-b:a " + audioBitrate + "k ");
39 if (options.isDeinterlace())
40 codecParams.append("-filter:v yadif ");
42 if (options.getTargetFps() != null)
43 codecParams.append("-r " + options.getTargetFps() + " ");
45 return codecParams.toString();
48 private int getAudioBitrateValue(final Bitrate bitRate) {
69 throw new RuntimeException("Audio bitrate: " + bitRate
70 + " is not supported.");
75 public String getCommand(final File inputFile, final File targetFile,
76 final EncodingOptions options, final String targetFormat) {
78 int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
79 int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
82 final StringBuilder codecParams = new StringBuilder();
84 String videoCodec = "libx264";
85 String audioCodec = "libmp3lame";
87 if (targetFormat.equals("webm")) {
90 codecParams.append("-s 800x450 ");
93 if (options.getVideoBitrate() == Bitrate.COPY)
96 if (options.getAudioBitrate() == Bitrate.COPY)
99 if (options.getAudioBitrate() == Bitrate.LOSSLESS){
101 codecParams.append("-strict -2 ");
104 if (options.getAudioBitrate() == Bitrate.NONE)
107 if (options.isForPortablePlayer()) {
110 videoCodec = "libxvid";
113 codecParams.append("-s 640x480 ");
115 // enforce maximum keyframe interval
116 codecParams.append("-g 150 ");
119 codecParams.append(constructCodecParamsString(options, videoBitrate,
120 audioBitrate, videoCodec, audioCodec));
122 return "ffmpeg -i \"" + inputFile.getAbsolutePath() + "\" "
123 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
128 public List<String> getSourceFileExtensions() {
129 return getSupportedExtensions();
132 private List<String> getSupportedExtensions() {
133 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
134 "webm", "mov", "asf", "3gp");
138 public List<String> getTargetFileExtensions() {
139 return getSupportedExtensions();
142 private int getVideoBitrateValue(final Bitrate bitRate) {
157 throw new RuntimeException("Video bitrate: " + bitRate
158 + " is not supported.");
163 public boolean isTerminalMandatory() {