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