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