added mov support
[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                 final 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.isDeinterlace())
41                         codecParams.append("-filter:v yadif ");
42
43                 if (options.getTargetFps() != null)
44                         codecParams.append("-r " + options.getTargetFps() + " ");
45
46                 return codecParams.toString();
47         }
48
49         private int getAudioBitrateValue(final BitrateParameter.bitrate bitRate) {
50                 switch (bitRate) {
51                 case LOW:
52                         return 128;
53
54                 case MEDIUM:
55                         return 160;
56
57                 case HIGH:
58                         return 320;
59
60                 case COPY:
61                         return -1;
62
63                 case NONE:
64                         return -1;
65
66                 default:
67                         throw new RuntimeException("Audio bitrate: " + bitRate
68                                         + " is not supported.");
69                 }
70         }
71
72         @Override
73         public String getCommand(final File inputFile, final File targetFile,
74                         final EncodingOptions options, final String targetFormat) {
75
76                 int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
77                 int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
78
79                 // convert
80                 final StringBuffer codecParams = new StringBuffer();
81
82                 String videoCodec = "libx264";
83                 String audioCodec = "libmp3lame";
84
85                 if (targetFormat.equals("webm")) {
86                         videoCodec = "vp8";
87                         audioCodec = "opus";
88                         codecParams.append("-s 800x600 ");
89                 }
90
91                 if (options.getVideoBitrate() == bitrate.COPY)
92                         videoCodec = "copy";
93
94                 if (options.getAudioBitrate() == bitrate.COPY)
95                         audioCodec = "copy";
96
97                 if (options.getAudioBitrate() == bitrate.NONE)
98                         audioCodec = null;
99
100                 if (options.isForPortablePlayer()) {
101                         videoBitrate = 1000;
102                         audioBitrate = 128;
103                         videoCodec = "libxvid";
104
105                         // reduce resolution
106                         codecParams.append("-s 640x480 ");
107
108                         // enforce maximum keyframe interval
109                         codecParams.append("-g 150 ");
110                 }
111
112                 codecParams.append(constructCodecParamsString(options, videoBitrate,
113                                 audioBitrate, videoCodec, audioCodec));
114
115                 return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
116                                 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
117                                 + "\"";
118         }
119
120         @Override
121         public List<String> getSourceFileExtensions() {
122                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
123                                 "webm", "mov");
124         }
125
126         @Override
127         public List<String> getTargetFileExtensions() {
128                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v",
129                                 "webm", "mov");
130         }
131
132         private int getVideoBitrateValue(final BitrateParameter.bitrate bitRate) {
133                 switch (bitRate) {
134                 case LOW:
135                         return 1000;
136
137                 case MEDIUM:
138                         return 4000;
139
140                 case HIGH:
141                         return 16000;
142
143                 case COPY:
144                         return -1;
145
146                 default:
147                         throw new RuntimeException("Video bitrate: " + bitRate
148                                         + " is not supported.");
149                 }
150         }
151
152         @Override
153         public boolean isTerminalMandatory() {
154                 return true;
155         }
156
157 }