642095d5abf6f846152a3b4c7813153942f1e62e
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / converters / AvconvVideo.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8  */
9
10 package eu.svjatoslav.meviz.encoder.converters;
11
12 import eu.svjatoslav.meviz.encoder.BitrateParameter.Bitrate;
13 import eu.svjatoslav.meviz.encoder.EncodingOptions;
14
15 import java.io.File;
16 import java.util.List;
17
18 public class AvconvVideo extends AbstractConverter {
19
20     private String constructCodecParamsString(final EncodingOptions options,
21                                               final int videoBitrate, final int audioBitrate,
22                                               final String videoCodec, final String audioCodec) {
23
24         final StringBuilder codecParams = new StringBuilder();
25
26         if (audioCodec == null)
27             codecParams.append("-an ");
28         else
29             codecParams.append("-acodec " + audioCodec + " ");
30
31         codecParams.append("-vcodec " + videoCodec + " ");
32
33         if (videoBitrate != -1)
34             codecParams.append("-b " + videoBitrate + "k ");
35
36         if (audioBitrate != -1)
37             codecParams.append("-b:a " + audioBitrate + "k ");
38
39         if (options.isDeinterlace())
40             codecParams.append("-filter:v yadif ");
41
42         if (options.getTargetFps() != null)
43             codecParams.append("-r " + options.getTargetFps() + " ");
44
45         return codecParams.toString();
46     }
47
48     private int getAudioBitrateValue(final Bitrate bitRate) {
49         switch (bitRate) {
50             case LOW:
51                 return 128;
52
53             case MEDIUM:
54                 return 160;
55
56             case HIGH:
57                 return 320;
58
59             case COPY:
60                 return -1;
61
62             case NONE:
63                 return -1;
64
65             default:
66                 throw new RuntimeException("Audio bitrate: " + bitRate
67                         + " is not supported.");
68         }
69     }
70
71     @Override
72     public String getCommand(final File inputFile, final File targetFile,
73                              final EncodingOptions options, final String targetFormat) {
74
75         int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
76         int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
77
78         // convert
79         final StringBuilder codecParams = new StringBuilder();
80
81         String videoCodec = "libx264";
82         String audioCodec = "libmp3lame";
83
84         if (targetFormat.equals("webm")) {
85             videoCodec = "vp8";
86             audioCodec = "opus";
87             codecParams.append("-s 800x600 ");
88         }
89
90         if (options.getVideoBitrate() == Bitrate.COPY)
91             videoCodec = "copy";
92
93         if (options.getAudioBitrate() == Bitrate.COPY)
94             audioCodec = "copy";
95
96         if (options.getAudioBitrate() == Bitrate.NONE)
97             audioCodec = null;
98
99         if (options.isForPortablePlayer()) {
100             videoBitrate = 1000;
101             audioBitrate = 128;
102             videoCodec = "libxvid";
103
104             // reduce resolution
105             codecParams.append("-s 640x480 ");
106
107             // enforce maximum keyframe interval
108             codecParams.append("-g 150 ");
109         }
110
111         codecParams.append(constructCodecParamsString(options, videoBitrate,
112                 audioBitrate, videoCodec, audioCodec));
113
114         return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
115                 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
116                 + "\"";
117     }
118
119     @Override
120     public List<String> getSourceFileExtensions() {
121         return getSupportedExtensions();
122     }
123
124     private List<String> getSupportedExtensions() {
125         return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
126                 "webm", "mov", "asf");
127     }
128
129     @Override
130     public List<String> getTargetFileExtensions() {
131         return getSupportedExtensions();
132     }
133
134     private int getVideoBitrateValue(final Bitrate bitRate) {
135         switch (bitRate) {
136             case LOW:
137                 return 1000;
138
139             case MEDIUM:
140                 return 4000;
141
142             case HIGH:
143                 return 16000;
144
145             case COPY:
146                 return -1;
147
148             default:
149                 throw new RuntimeException("Video bitrate: " + bitRate
150                         + " is not supported.");
151         }
152     }
153
154     @Override
155     public boolean isTerminalMandatory() {
156         return true;
157     }
158
159 }