2 * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.meviz.encoder;
9 import eu.svjatoslav.commons.file.FilePathParser;
10 import eu.svjatoslav.commons.string.GlobMatcher;
11 import eu.svjatoslav.meviz.Module;
12 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
15 import java.util.List;
17 public class Encoder implements Module {
19 private final FormatsRegistry encoderRegistry = new FormatsRegistry();
20 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
21 private EncodingOptions encodingOptions;
22 private EncodingPlan encodingPlan;
25 * Generate encoding plan
27 * @param sourceFile Source directory of file
29 private void compileEncodingPlan(final File sourceFile) {
30 if (!sourceFile.exists()) {
31 System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
32 + "\" does not exist.");
36 if (sourceFile.isDirectory()) {
38 for (final File subFile : sourceFile.listFiles())
39 if (subFile.isDirectory()) {
40 if (encodingOptions.isRecursive())
41 compileEncodingPlan(subFile);
43 compileEncodingPlan(subFile);
44 } else if (sourceFile.isFile())
45 if (fileMatchesInputPattern(sourceFile)) {
47 // System.out.println("Processing file: " +
48 // file.getAbsolutePath());
50 final String sourceFileExtension = FilePathParser
51 .getFileExtension(sourceFile);
53 // encode source file into every desired target format
54 for (final String targetFormat : encodingOptions.getOutputFormats()) {
56 // construct target file
57 final File targetFile = getTargetFile(sourceFile,
60 // System.out.println("target path: " +
61 // targetFilePath.toString());
63 if (!targetFile.exists()) {
65 final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
66 .getEncoders(sourceFileExtension, targetFormat);
68 if (formats.size() == 0)
70 .println("Error: no encoders found to convert file \""
71 + sourceFile.getAbsolutePath()
75 else if (formats.size() > 1)
77 .println("Error: Encoders piping not yet supported to convert file \""
78 + sourceFile.getAbsolutePath()
83 final AbstractConverter chosenFormat = formats
85 final EncodingTask encodingTask = new EncodingTask(
86 sourceFile, targetFile, chosenFormat,
89 if (chosenFormat.isTerminalMandatory())
90 encodingTask.setUseTerminal(true);
91 encodingPlan.scheduleTask(encodingTask);
100 * @param file single file candidate to potentially be encoded
101 * @return <code>true</code> if file shall be encoded.
103 private boolean fileMatchesInputPattern(final File file) {
104 final String fileName = file.getName().toLowerCase();
106 for (final String inputPattern : encodingOptions.getInputPatterns())
107 if (GlobMatcher.match(fileName, inputPattern.toLowerCase()))
114 public String getDescription() {
115 return "Convert between various media formats.";
119 public String getModuleCommand() {
123 private File getTargetFile(final File sourceFile, final String targetFormat) {
124 String targetFilePath = sourceFile.getParent() +
127 .getFileNameWithoutExtension(sourceFile) +
131 return new File(targetFilePath);
135 public void run(final String[] args) {
137 // parse incoming commandline arguments
138 encodingOptions = commandlineHandler.parseCommandlineArguments(args);
140 if (encodingOptions == null) {
141 showCommandlineHelp();
145 encodingPlan = new EncodingPlan();
147 compileEncodingPlan(encodingOptions.getWorkingDirectory());
149 if (!encodingOptions.isTestOnly())
151 encodingPlan.execute(encodingOptions);
152 } catch (final Exception exception) {
153 exception.printStackTrace();
159 public void showCommandlineHelp() {
160 commandlineHandler.parser.showHelp();
161 System.out.println("Example commands:");
163 .println(" Convert all MTS files in the current directory into MP4's.");
164 System.out.println(" meviz encode -o mp4 -i *.MTS");