2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.encoder;
12 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
13 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
14 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
15 import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
16 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
18 public class CommandlineHandler {
20 Parser parser = new Parser();
22 StringParameter outputFormatParameter = parser
23 .createStringParameter("Encoding output format.")
24 .addAliases("-o", "--output-format").setMandatory();
26 NullParameter terminalParameter = parser.createNullParameter(
27 "Enable popup terminal.").addAliases("--terminal");
29 NullParameter deinterlaceParameter = parser.createNullParameter(
30 "Deinterlace video.").addAliases("--deinterlace", "-d");
32 NullParameter testParameter = parser.createNullParameter(
33 "Simulate file encoding.").addAliases("-t", "--test");
35 NullParameter recursiveParameter = parser.createNullParameter(
36 "Enable recursive mode.").addAliases("-r", "--recursive");
38 StringParameter inputPatternParameter = parser
39 .createStringParameter("File input pattern.")
40 .addAliases("-i", "--input-pattern").setMandatory();
42 DirectoryParameter workingDirectoryParameter = parser
43 .createDirectoryParameter("Working directory.")
44 .addAliases("-w", "--working-directory").mustExist();
46 Parameter videoBitrateParameter = parser.createCustomParameter(
47 new Bitrate(), "Video bitrate.")
48 .addAliases("-v", "--video-bitrate");
50 public Parser initParser() {
51 final Parser parser = new Parser();
53 parser.addParameter(recursiveParameter);
55 parser.addParameter(deinterlaceParameter);
57 parser.addParameter(testParameter);
59 parser.addParameter(terminalParameter);
61 parser.addParameter(outputFormatParameter);
63 parser.addParameter(inputPatternParameter);
65 parser.addParameter(workingDirectoryParameter);
67 parser.addParameter(videoBitrateParameter);
73 * @return {@link EncodingOptions} if commandline arguments were
74 * successfully parsed, or <code>null</code> if parsing error
77 public EncodingOptions parseCommandlineArguments(final String[] args) {
79 final EncodingOptions options = new EncodingOptions();
81 final Parser parser = initParser();
84 if (recursiveParameter.isParameterSpecified())
85 options.recursive = true;
87 if (deinterlaceParameter.isParameterSpecified())
88 options.deinterlace = true;
90 if (terminalParameter.isParameterSpecified())
91 options.terminal = true;
93 if (testParameter.isParameterSpecified())
94 options.testOnly = true;
96 if (outputFormatParameter.isParameterSpecified())
97 options.outputFormats.addAll(outputFormatParameter
98 .getArgumentsAsStrings());
100 if (workingDirectoryParameter.isParameterSpecified())
101 options.workingDirectory = workingDirectoryParameter
102 .getArgumentsAsFiles().get(0);
104 if (inputPatternParameter.isParameterSpecified())
105 options.inputPatterns.addAll(inputPatternParameter
106 .getArgumentsAsStrings());
108 if (videoBitrateParameter.isParameterSpecified())
110 options.videoBitrate = Bitrate.bitrate
111 .valueOf(videoBitrateParameter.getArgumentsAsStrings()
112 .get(0).toUpperCase());
113 } catch (final Exception e) {
115 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");