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) {
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.NONE)
99 if (options.isForPortablePlayer()) {
102 videoCodec = "libxvid";
105 codecParams.append("-s 640x480 ");
107 // enforce maximum keyframe interval
108 codecParams.append("-g 150 ");
111 codecParams.append(constructCodecParamsString(options, videoBitrate,
112 audioBitrate, videoCodec, audioCodec));
114 return "ffmpeg -i \"" + inputFile.getAbsolutePath() + "\" "
115 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
120 public List<String> getSourceFileExtensions() {
121 return getSupportedExtensions();
124 private List<String> getSupportedExtensions() {
125 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
126 "webm", "mov", "asf", "3gp");
130 public List<String> getTargetFileExtensions() {
131 return getSupportedExtensions();
134 private int getVideoBitrateValue(final Bitrate bitRate) {
149 throw new RuntimeException("Video bitrate: " + bitRate
150 + " is not supported.");
155 public boolean isTerminalMandatory() {