more elegant commandline handling
[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.bitrate;
16 import eu.svjatoslav.meviz.encoder.EncodingOptions;
17
18 public class AvconvVideo extends AbstractConverter {
19
20         private void constructCodecParamsString(final EncodingOptions options,
21                         final int videoBitrate, final int audioBitrate,
22                         final StringBuffer codecParams, final String videoCodec,
23                         final String audioCodec) {
24
25                 codecParams.append("-acodec " + audioCodec + " -vcodec " + videoCodec
26                                 + " ");
27
28                 if (videoBitrate != -1)
29                         codecParams.append("-b " + videoBitrate + "k ");
30
31                 if (audioBitrate != -1)
32                         codecParams.append("-b:a " + audioBitrate + "k ");
33
34                 if (options.deinterlace)
35                         codecParams.append("-filter:v yadif ");
36         }
37
38         @Override
39         public String getCommand(final File inputFile, final File targetFile,
40                         final EncodingOptions options) {
41
42                 int videoBitrate = -1;
43                 int audioBitrate = -1;
44
45                 switch (options.getVideoBitrate()) {
46                 case LOW:
47                         videoBitrate = 1000;
48                         audioBitrate = 128;
49                         break;
50
51                 case MEDIUM:
52                         videoBitrate = 4000;
53                         audioBitrate = 192;
54                         break;
55
56                 case HIGH:
57                         videoBitrate = 40000;
58                         audioBitrate = 500;
59                         break;
60
61                 case COPY:
62                         break;
63
64                 default:
65                         throw new RuntimeException("Video bitrate: " + options.getVideoBitrate()
66                                         + " is not supported.");
67                 }
68
69                 // convert
70                 final StringBuffer codecParams = new StringBuffer();
71
72                 String videoCodec = "libx264";
73                 String audioCodec = "libmp3lame";
74
75                 if (options.getVideoBitrate() == bitrate.COPY) {
76                         videoCodec = "copy";
77                         audioCodec = "copy";
78                 }
79
80                 if (options.forPortablePlayer) {
81                         videoBitrate = 1000;
82                         audioBitrate = 128;
83                         videoCodec = "libxvid";
84                         codecParams.append("-s 640x480 ");
85                 }
86
87                 constructCodecParamsString(options, videoBitrate, audioBitrate,
88                                 codecParams, videoCodec, audioCodec);
89
90                 return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
91                                 + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
92                                 + "\"";
93         }
94
95         @Override
96         public List<String> getSourceFileExtensions() {
97                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v");
98         }
99
100         @Override
101         public List<String> getTargetFileExtensions() {
102                 return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v");
103         }
104
105         @Override
106         public boolean isTerminalMandatory() {
107                 return true;
108         }
109
110 }