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.Parser;
13 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
14 import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
15 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
17 public class CommandlineHandler {
19 Parser parser = new Parser();
21 StringParameter outputFormatParameter = parser
22 .createStringParameter("Encoding output format.")
23 .addAliases("-o", "--output-format").setMandatory();
25 NullParameter terminalParameter = parser.createNullParameter(
26 "Enable popup terminal.").addAliases("--terminal");
28 NullParameter deinterlaceParameter = parser.createNullParameter(
29 "Deinterlace video.").addAliases("--deinterlace", "-d");
31 NullParameter testParameter = parser.createNullParameter(
32 "Simulate file encoding.").addAliases("-t", "--test");
34 NullParameter recursiveParameter = parser.createNullParameter(
35 "Enable recursive mode.").addAliases("-r", "--recursive");
37 StringParameter inputPatternParameter = parser
38 .createStringParameter("File input pattern.")
39 .addAliases("-i", "--input-pattern").setMandatory();
41 DirectoryParameter workingDirectoryParameter = parser
42 .createDirectoryParameter("Working directory.")
43 .addAliases("-w", "--working-directory").mustExist();
45 BitrateParameter videoBitrateParameter = new BitrateParameter(
48 public CommandlineHandler() {
49 videoBitrateParameter.addAliases("-v", "--video-bitrate");
50 parser.addParameter(videoBitrateParameter);
54 * @return {@link EncodingOptions} if commandline arguments were
55 * successfully parsed, or <code>null</code> if parsing error
58 public EncodingOptions parseCommandlineArguments(final String[] args) {
60 final EncodingOptions options = new EncodingOptions();
64 if (recursiveParameter.isParameterSpecified())
65 options.recursive = true;
67 if (deinterlaceParameter.isParameterSpecified())
68 options.deinterlace = true;
70 if (terminalParameter.isParameterSpecified())
71 options.terminal = true;
73 if (testParameter.isParameterSpecified())
74 options.testOnly = true;
76 if (outputFormatParameter.isParameterSpecified())
77 options.outputFormats.addAll(outputFormatParameter
78 .getArgumentsAsStrings());
80 if (workingDirectoryParameter.isParameterSpecified())
81 options.workingDirectory = workingDirectoryParameter
82 .getArgumentsAsFiles().get(0);
84 if (inputPatternParameter.isParameterSpecified())
85 options.inputPatterns.addAll(inputPatternParameter
86 .getArgumentsAsStrings());
88 if (videoBitrateParameter.isParameterSpecified())
90 options.videoBitrate = videoBitrateParameter.getValue();
91 } catch (final Exception e) {
93 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");