code cleanup and formatting
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / Encoder.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.file.FilePathParser;
13 import eu.svjatoslav.commons.string.WildCardMatcher;
14 import eu.svjatoslav.meviz.Module;
15 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
16
17 import java.io.File;
18 import java.util.List;
19
20 public class Encoder implements Module {
21
22     public FormatsRegistry encoderRegistry = new FormatsRegistry();
23
24     public EncodingOptions encodingOptions;
25
26     public EncodingPlan encodingPlan;
27
28     CommandlineHandler commandlineHandler = new CommandlineHandler();
29
30     /**
31      * Generate encoding plan
32      *
33      * @param sourceFile Source directory of file
34      */
35     public void compileEncodingPlan(final File sourceFile) {
36         if (!sourceFile.exists()) {
37             System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
38                     + "\" does not exist.");
39             return;
40         }
41
42         if (sourceFile.isDirectory()) {
43             // handle directory
44             for (final File subFile : sourceFile.listFiles())
45                 if (subFile.isDirectory()) {
46                     if (encodingOptions.isRecursive())
47                         compileEncodingPlan(subFile);
48                 } else
49                     compileEncodingPlan(subFile);
50         } else if (sourceFile.isFile())
51             if (fileMatchesInputPattern(sourceFile)) {
52
53                 // System.out.println("Processing file: " +
54                 // file.getAbsolutePath());
55
56                 final String sourceFileExtension = FilePathParser
57                         .getFileExtension(sourceFile);
58
59                 // encode source file into every desired target format
60                 for (final String targetFormat : encodingOptions.getOutputFormats()) {
61
62                     // construct target file
63                     final File targetFile = getTargetFile(sourceFile,
64                             targetFormat);
65
66                     // System.out.println("target path: " +
67                     // targetFilePath.toString());
68
69                     if (!targetFile.exists()) {
70
71                         final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
72                                 .getEncoders(sourceFileExtension, targetFormat);
73
74                         if (formats.size() == 0)
75                             System.out
76                                     .println("Error: no encoders found to convert file \""
77                                             + sourceFile.getAbsolutePath()
78                                             + "\" into "
79                                             + targetFormat
80                                             + " format.");
81                         else if (formats.size() > 1)
82                             System.out
83                                     .println("Error: Encoders piping not yet supported to convert file \""
84                                             + sourceFile.getAbsolutePath()
85                                             + "\" into "
86                                             + targetFormat
87                                             + " format.");
88                         else {
89                             final AbstractConverter chosenFormat = formats
90                                     .get(0);
91                             final EncodingTask encodingTask = new EncodingTask(
92                                     sourceFile, targetFile, chosenFormat,
93                                     targetFormat);
94
95                             if (chosenFormat.isTerminalMandatory())
96                                 encodingTask.setUseTerminal(true);
97                             encodingPlan.scheduleTask(encodingTask);
98                         }
99                     }
100                 }
101             }
102
103     }
104
105     /**
106      * @param file single file candidate to potentially be encoded
107      * @return <code>true</code> if file shall be encoded.
108      */
109     public boolean fileMatchesInputPattern(final File file) {
110         final String fileName = file.getName().toLowerCase();
111
112         for (final String inputPattern : encodingOptions.getInputPatterns())
113             if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
114                 return true;
115
116         return false;
117     }
118
119     @Override
120     public String getDescription() {
121         return "Convert between various media formats.";
122     }
123
124     @Override
125     public String getModuleCommand() {
126         return "encode";
127     }
128
129     private File getTargetFile(final File sourceFile, final String targetFormat) {
130         String targetFilePath = sourceFile.getParent() +
131                 "/" +
132                 FilePathParser
133                         .getFileNameWithoutExtension(sourceFile) +
134                 "." +
135                 targetFormat;
136
137         return new File(targetFilePath);
138     }
139
140     @Override
141     public void run(final String[] args) {
142
143         // parse incoming commandline arguments
144         encodingOptions = commandlineHandler.parseCommandlineArguments(args);
145
146         if (encodingOptions == null) {
147             showCommandlineHelp();
148             return;
149         }
150
151         encodingPlan = new EncodingPlan();
152
153         compileEncodingPlan(encodingOptions.getWorkingDirectory());
154
155         if (!encodingOptions.isTestOnly())
156             try {
157                 encodingPlan.execute(encodingOptions);
158             } catch (final Exception exception) {
159                 exception.printStackTrace();
160             }
161
162     }
163
164     @Override
165     public void showCommandlineHelp() {
166         commandlineHandler.parser.showHelp();
167         System.out.println("Example commands:");
168         System.out
169                 .println("    Convert all MTS files in the current directory into MP4's.");
170         System.out.println("        meviz encode -o mp4 -i *.MTS");
171     }
172
173 }