Refactored to remove code duplication.
[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.EncodingOptions;
16
17 public class AvconvVideo extends AbstractConverter {
18
19         @Override
20         public String getCommand(final File inputFile, final File targetFile,
21                         final EncodingOptions options) {
22
23                 int videoBitrate;
24                 int audioBitrate;
25
26                 switch (options.videoBitrate) {
27                 case LOW:
28                         videoBitrate = 1000;
29                         audioBitrate = 128;
30                         break;
31
32                 case MEDIUM:
33                         videoBitrate = 3500;
34                         audioBitrate = 128;
35                         break;
36
37                 case HIGH:
38                         videoBitrate = 40000;
39                         audioBitrate = 500;
40                         break;
41
42                 default:
43                         throw new RuntimeException("Video bitrate: " + options.videoBitrate
44                                         + " is not supported.");
45                 }
46
47                 // convert
48                 final StringBuffer codecParams = new StringBuffer();
49
50                 codecParams.append("-acodec libmp3lame -vcodec libx264");
51
52                 codecParams.append(" -b " + videoBitrate + "k");
53                 codecParams.append(" -b:a " + audioBitrate + "k");
54
55                 if (options.deinterlace)
56                         codecParams.append(" -filter:v yadif");
57
58                 return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
59                                 + codecParams.toString() + " \"" + targetFile.getAbsolutePath()
60                                 + "\"";
61         }
62
63         @Override
64         public List<String> getSourceFileExtensions() {
65                 return toList("mkv", "mts");
66         }
67
68         @Override
69         public List<String> getTargetFileExtensions() {
70                 return toList("mp4");
71         }
72
73         @Override
74         public boolean isTerminalMandatory() {
75                 return true;
76         }
77
78 }