possibility to specify video and audio bitrate separately
[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 void constructCodecParamsString(final EncodingOptions options,
22                         final int videoBitrate, final int audioBitrate,
23                         final StringBuffer codecParams, final String videoCodec,
24                         final String audioCodec) {
25
26                 codecParams.append("-acodec " + audioCodec + " -vcodec " + videoCodec
27                                 + " ");
28
29                 if (videoBitrate != -1)
30                         codecParams.append("-b " + videoBitrate + "k ");
31
32                 if (audioBitrate != -1)
33                         codecParams.append("-b:a " + audioBitrate + "k ");
34
35                 if (options.deinterlace)
36                         codecParams.append("-filter:v yadif ");
37         }
38
39         private int getAudioBitrateValue(final BitrateParameter.bitrate bitRate) {
40                 switch (bitRate) {
41                 case LOW:
42                         return 128;
43
44                 case MEDIUM:
45                         return 160;
46
47                 case HIGH:
48                         return 320;
49
50                 case COPY:
51                         return -1;
52
53                 default:
54                         throw new RuntimeException("Audio bitrate: " + bitRate
55                                         + " is not supported.");
56                 }
57         }
58
59         @Override
60         public String getCommand(final File inputFile, final File targetFile,
61                         final EncodingOptions options) {
62
63                 int videoBitrate = getVideoBitrateValue(options.getVideoBitrate());
64                 int audioBitrate = getAudioBitrateValue(options.getAudioBitrate());
65
66                 // convert
67                 final StringBuffer codecParams = new StringBuffer();
68
69                 String videoCodec = "libx264";
70                 String audioCodec = "libmp3lame";
71
72                 if (options.getVideoBitrate() == bitrate.COPY)
73                         videoCodec = "copy";
74
75                 if (options.getAudioBitrate() == bitrate.COPY)
76                         audioCodec = "copy";
77
78                 if (options.forPortablePlayer) {
79                         videoBitrate = 1000;
80                         audioBitrate = 128;
81                         videoCodec = "libxvid";
82                         codecParams.append("-s 640x480 ");
83                 }
84
85                 constructCodecParamsString(options, videoBitrate, audioBitrate,
86                                 codecParams, videoCodec, audioCodec);
87
88                 return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
89                                 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
90                                 + "\"";
91         }
92
93         @Override
94         public List<String> getSourceFileExtensions() {
95                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v");
96         }
97
98         @Override
99         public List<String> getTargetFileExtensions() {
100                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v");
101         }
102
103         private int getVideoBitrateValue(final BitrateParameter.bitrate bitRate) {
104                 switch (bitRate) {
105                 case LOW:
106                         return 1000;
107
108                 case MEDIUM:
109                         return 4000;
110
111                 case HIGH:
112                         return 40000;
113
114                 case COPY:
115                         return -1;
116
117                 default:
118                         throw new RuntimeException("Video bitrate: " + bitRate
119                                         + " is not supported.");
120                 }
121         }
122
123         @Override
124         public boolean isTerminalMandatory() {
125                 return true;
126         }
127
128 }