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 public FormatsRegistry encoderRegistry = new FormatsRegistry();
24 public EncodingOptions encodingOptions;
26 public EncodingPlan encodingPlan;
28 CommandlineHandler commandlineHandler = new CommandlineHandler();
31 * Generate encoding plan
33 * @param sourceFile Source directory of file
35 public void compileEncodingPlan(final File sourceFile) {
36 if (!sourceFile.exists()) {
37 System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
38 + "\" does not exist.");
42 if (sourceFile.isDirectory()) {
44 for (final File subFile : sourceFile.listFiles())
45 if (subFile.isDirectory()) {
46 if (encodingOptions.isRecursive())
47 compileEncodingPlan(subFile);
49 compileEncodingPlan(subFile);
50 } else if (sourceFile.isFile())
51 if (fileMatchesInputPattern(sourceFile)) {
53 // System.out.println("Processing file: " +
54 // file.getAbsolutePath());
56 final String sourceFileExtension = FilePathParser
57 .getFileExtension(sourceFile);
59 // encode source file into every desired target format
60 for (final String targetFormat : encodingOptions.getOutputFormats()) {
62 // construct target file
63 final File targetFile = getTargetFile(sourceFile,
66 // System.out.println("target path: " +
67 // targetFilePath.toString());
69 if (!targetFile.exists()) {
71 final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
72 .getEncoders(sourceFileExtension, targetFormat);
74 if (formats.size() == 0)
76 .println("Error: no encoders found to convert file \""
77 + sourceFile.getAbsolutePath()
81 else if (formats.size() > 1)
83 .println("Error: Encoders piping not yet supported to convert file \""
84 + sourceFile.getAbsolutePath()
89 final AbstractConverter chosenFormat = formats
91 final EncodingTask encodingTask = new EncodingTask(
92 sourceFile, targetFile, chosenFormat,
95 if (chosenFormat.isTerminalMandatory())
96 encodingTask.setUseTerminal(true);
97 encodingPlan.scheduleTask(encodingTask);
106 * @param file single file candidate to potentially be encoded
107 * @return <code>true</code> if file shall be encoded.
109 public boolean fileMatchesInputPattern(final File file) {
110 final String fileName = file.getName().toLowerCase();
112 for (final String inputPattern : encodingOptions.getInputPatterns())
113 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
120 public String getDescription() {
121 return "Convert between various media formats.";
125 public String getModuleCommand() {
129 private File getTargetFile(final File sourceFile, final String targetFormat) {
130 String targetFilePath = sourceFile.getParent() +
133 .getFileNameWithoutExtension(sourceFile) +
137 return new File(targetFilePath);
141 public void run(final String[] args) {
143 // parse incoming commandline arguments
144 encodingOptions = commandlineHandler.parseCommandlineArguments(args);
146 if (encodingOptions == null) {
147 showCommandlineHelp();
151 encodingPlan = new EncodingPlan();
153 compileEncodingPlan(encodingOptions.getWorkingDirectory());
155 if (!encodingOptions.isTestOnly())
157 encodingPlan.execute(encodingOptions);
158 } catch (final Exception exception) {
159 exception.printStackTrace();
165 public void showCommandlineHelp() {
166 commandlineHandler.parser.showHelp();
167 System.out.println("Example commands:");
169 .println(" Convert all MTS files in the current directory into MP4's.");
170 System.out.println(" meviz encode -o mp4 -i *.MTS");