possibility to specify bitrate for MTS to MP4 converter
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / CommandlineHandler.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;
11
12 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
13 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
14 import eu.svjatoslav.commons.commandline.parameterparser.arguments.StringArgument;
15
16 public class CommandlineHandler {
17
18         Parameter outputFormatParameter = new Parameter(true, true, true,
19                         new StringArgument(), "Encoding output format.", "-o",
20                         "--output-format");
21
22         Parameter terminalParameter = new Parameter("Enable popup terminal.",
23                         "--terminal");
24
25         Parameter deinterlaceParameter = new Parameter("Deinterlace video.",
26                         "--deinterlace", "-d");
27
28         Parameter testParameter = new Parameter("Simulate file encoding.", "-t",
29                         "--test");
30
31         Parameter recursiveParameter = new Parameter("Enable recursive mode.",
32                         "-r", "--recursive");
33
34         Parameter inputPatternParameter = new Parameter(true, true, true,
35                         new StringArgument(), "File input pattern.", "-i",
36                         "--input-pattern");
37
38         Parameter workingDirectoryParameter = new Parameter(
39                         false,
40                         true,
41                         true,
42                         new eu.svjatoslav.commons.commandline.parameterparser.arguments.ExistingDirectory(),
43                         "Working directory.", "-w", "--working-directory");
44
45         Parameter videoBitrateParameter = new Parameter(false, true, false,
46                         new Bitrate(), "Video bitrate.", "-v", "--video-bitrate");
47
48         public Parser initParser() {
49                 final Parser parser = new Parser();
50
51                 parser.addParameter(recursiveParameter);
52
53                 parser.addParameter(deinterlaceParameter);
54
55                 parser.addParameter(testParameter);
56
57                 parser.addParameter(terminalParameter);
58
59                 parser.addParameter(outputFormatParameter);
60
61                 parser.addParameter(inputPatternParameter);
62
63                 parser.addParameter(workingDirectoryParameter);
64
65                 parser.addParameter(videoBitrateParameter);
66
67                 return parser;
68         }
69
70         /**
71          * @return {@link EncodingOptions} if commandline arguments were
72          *         successfully parsed, or <code>null</code> if parsing error
73          *         occurred.
74          */
75         public EncodingOptions parseCommandlineArguments(final String[] args) {
76
77                 final EncodingOptions options = new EncodingOptions();
78
79                 final Parser parser = initParser();
80                 parser.parse(args);
81
82                 if (recursiveParameter.isParameterSpecified())
83                         options.recursive = true;
84
85                 if (deinterlaceParameter.isParameterSpecified())
86                         options.deinterlace = true;
87
88                 if (terminalParameter.isParameterSpecified())
89                         options.terminal = true;
90
91                 if (testParameter.isParameterSpecified())
92                         options.testOnly = true;
93
94                 if (outputFormatParameter.isParameterSpecified())
95                         options.outputFormats.addAll(outputFormatParameter
96                                         .getArgumentsAsStrings());
97
98                 if (workingDirectoryParameter.isParameterSpecified())
99                         options.workingDirectory = workingDirectoryParameter
100                                         .getArgumentsAsFiles().get(0);
101
102                 if (inputPatternParameter.isParameterSpecified())
103                         options.inputPatterns.addAll(inputPatternParameter
104                                         .getArgumentsAsStrings());
105
106                 if (videoBitrateParameter.isParameterSpecified())
107                         try {
108                                 options.videoBitrate = Bitrate.bitrate
109                                                 .valueOf(videoBitrateParameter.getArgumentAsString()
110                                                                 .toUpperCase());
111                         } catch (final Exception e) {
112                                 System.out
113                                                 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");
114                                 return null;
115                         }
116
117                 return options;
118         }
119 }