ba5459991b42738d21d1ab925da603e83e6066b6
[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.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;
16
17 public class CommandlineHandler {
18
19         Parser parser = new Parser();
20
21         StringParameter outputFormatParameter = parser
22                         .createStringParameter("Encoding output format.")
23                         .addAliases("-o", "--output-format").setMandatory();
24
25         NullParameter deinterlaceParameter = parser.createNullParameter(
26                         "Deinterlace video.").addAliases("--deinterlace", "-d");
27
28         NullParameter testParameter = parser.createNullParameter(
29                         "Simulate file encoding.").addAliases("-t", "--test");
30
31         NullParameter recursiveParameter = parser.createNullParameter(
32                         "Enable recursive mode.").addAliases("-r", "--recursive");
33
34         NullParameter forPortableParameter = parser.createNullParameter(
35                         "Encode for portable player.").addAliases("-p", "--portable");
36
37         StringParameter inputPatternParameter = parser
38                         .createStringParameter("File input pattern.")
39                         .addAliases("-i", "--input-pattern").setMandatory();
40
41         DirectoryParameter workingDirectoryParameter = parser
42                         .createDirectoryParameter("Working directory.")
43                         .addAliases("-w", "--working-directory").mustExist();
44
45         BitrateParameter videoBitrateParameter = new BitrateParameter(
46                         "Video bitrate");
47
48         public CommandlineHandler() {
49                 videoBitrateParameter.addAliases("-v", "--video-bitrate");
50                 parser.addParameter(videoBitrateParameter);
51         }
52
53         /**
54          * @return {@link EncodingOptions} if commandline arguments were
55          *         successfully parsed, or <code>null</code> if parsing error
56          *         occurred.
57          */
58         public EncodingOptions parseCommandlineArguments(final String[] args) {
59
60                 final EncodingOptions options = new EncodingOptions();
61
62                 parser.parse(args);
63
64                 options.recursive = recursiveParameter.isParameterSpecified();
65
66                 options.deinterlace = deinterlaceParameter.isParameterSpecified();
67
68                 options.testOnly = testParameter.isParameterSpecified();
69
70                 if (outputFormatParameter.isParameterSpecified())
71                         options.outputFormats.addAll(outputFormatParameter
72                                         .getArgumentsAsStrings());
73
74                 if (workingDirectoryParameter.isParameterSpecified())
75                         options.workingDirectory = workingDirectoryParameter
76                                         .getArgumentsAsFiles().get(0);
77
78                 if (inputPatternParameter.isParameterSpecified())
79                         options.inputPatterns.addAll(inputPatternParameter
80                                         .getArgumentsAsStrings());
81
82                 options.forPortablePlayer = forPortableParameter.isParameterSpecified();
83
84                 if (videoBitrateParameter.isParameterSpecified())
85                         try {
86                                 options.videoBitrate = videoBitrateParameter.getValue();
87                         } catch (final Exception e) {
88                                 System.out
89                                                 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");
90                                 return null;
91                         }
92
93                 return options;
94         }
95 }