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.file.FilePathParser;
13 import eu.svjatoslav.commons.string.WildCardMatcher;
14 import eu.svjatoslav.meviz.Module;
15 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
18 import java.util.List;
20 public class Encoder implements Module {
22 private final FormatsRegistry encoderRegistry = new FormatsRegistry();
23 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
24 private EncodingOptions encodingOptions;
25 private EncodingPlan encodingPlan;
28 * Generate encoding plan
30 * @param sourceFile Source directory of file
32 private void compileEncodingPlan(final File sourceFile) {
33 if (!sourceFile.exists()) {
34 System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
35 + "\" does not exist.");
39 if (sourceFile.isDirectory()) {
41 for (final File subFile : sourceFile.listFiles())
42 if (subFile.isDirectory()) {
43 if (encodingOptions.isRecursive())
44 compileEncodingPlan(subFile);
46 compileEncodingPlan(subFile);
47 } else if (sourceFile.isFile())
48 if (fileMatchesInputPattern(sourceFile)) {
50 // System.out.println("Processing file: " +
51 // file.getAbsolutePath());
53 final String sourceFileExtension = FilePathParser
54 .getFileExtension(sourceFile);
56 // encode source file into every desired target format
57 for (final String targetFormat : encodingOptions.getOutputFormats()) {
59 // construct target file
60 final File targetFile = getTargetFile(sourceFile,
63 // System.out.println("target path: " +
64 // targetFilePath.toString());
66 if (!targetFile.exists()) {
68 final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
69 .getEncoders(sourceFileExtension, targetFormat);
71 if (formats.size() == 0)
73 .println("Error: no encoders found to convert file \""
74 + sourceFile.getAbsolutePath()
78 else if (formats.size() > 1)
80 .println("Error: Encoders piping not yet supported to convert file \""
81 + sourceFile.getAbsolutePath()
86 final AbstractConverter chosenFormat = formats
88 final EncodingTask encodingTask = new EncodingTask(
89 sourceFile, targetFile, chosenFormat,
92 if (chosenFormat.isTerminalMandatory())
93 encodingTask.setUseTerminal(true);
94 encodingPlan.scheduleTask(encodingTask);
103 * @param file single file candidate to potentially be encoded
104 * @return <code>true</code> if file shall be encoded.
106 private boolean fileMatchesInputPattern(final File file) {
107 final String fileName = file.getName().toLowerCase();
109 for (final String inputPattern : encodingOptions.getInputPatterns())
110 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
117 public String getDescription() {
118 return "Convert between various media formats.";
122 public String getModuleCommand() {
126 private File getTargetFile(final File sourceFile, final String targetFormat) {
127 String targetFilePath = sourceFile.getParent() +
130 .getFileNameWithoutExtension(sourceFile) +
134 return new File(targetFilePath);
138 public void run(final String[] args) {
140 // parse incoming commandline arguments
141 encodingOptions = commandlineHandler.parseCommandlineArguments(args);
143 if (encodingOptions == null) {
144 showCommandlineHelp();
148 encodingPlan = new EncodingPlan();
150 compileEncodingPlan(encodingOptions.getWorkingDirectory());
152 if (!encodingOptions.isTestOnly())
154 encodingPlan.execute(encodingOptions);
155 } catch (final Exception exception) {
156 exception.printStackTrace();
162 public void showCommandlineHelp() {
163 commandlineHandler.parser.showHelp();
164 System.out.println("Example commands:");
166 .println(" Convert all MTS files in the current directory into MP4's.");
167 System.out.println(" meviz encode -o mp4 -i *.MTS");