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