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;
13 import java.util.List;
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.Converter;
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
34 * Source directory of file
36 public void compileEncodingPlan(final File sourceFile) {
37 if (!sourceFile.exists()) {
38 System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
39 + "\" does not exist.");
43 if (sourceFile.isDirectory()) {
45 for (final File subFile : sourceFile.listFiles())
46 if (subFile.isDirectory()) {
47 if (encodingOptions.recursive)
48 compileEncodingPlan(subFile);
50 compileEncodingPlan(subFile);
51 } else if (sourceFile.isFile())
52 if (fileMatchesInputPattern(sourceFile)) {
54 // System.out.println("Processing file: " +
55 // file.getAbsolutePath());
57 final String sourceFileExtension = FilePathParser
58 .getFileExtension(sourceFile);
60 // encode source file into every desired target format
61 for (final String targetFormat : encodingOptions.outputFormats) {
63 // construct target file
64 final File targetFile = getTargetFile(sourceFile,
67 // System.out.println("target path: " +
68 // targetFilePath.toString());
70 if (!targetFile.exists()) {
72 final List<eu.svjatoslav.meviz.encoder.converters.Converter> formats = encoderRegistry
73 .getEncoders(sourceFileExtension, targetFormat);
75 if (formats.size() == 0)
77 .println("Error: no encoders found to convert file \""
78 + sourceFile.getAbsolutePath()
82 else if (formats.size() > 1)
84 .println("Error: Encoders piping not yet supported to convert file \""
85 + sourceFile.getAbsolutePath()
90 final Converter chosenFormat = formats.get(0);
91 final EncodingTask encodingTask = new EncodingTask(
92 sourceFile, targetFile, chosenFormat);
94 if (encodingOptions.terminal
95 || chosenFormat.isTerminalMandatory())
96 encodingTask.setUseTerminal(true);
97 encodingPlan.scheduleTask(encodingTask);
107 * single file candidate to potentially be encoded
109 * @return <code>true</code> if file shall be encoded.
111 public boolean fileMatchesInputPattern(final File file) {
112 final String fileName = file.getName().toLowerCase();
114 for (final String inputPattern : encodingOptions.inputPatterns)
115 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
122 public String getDescription() {
123 return "Convert between various media formats.";
127 public String getModuleCommand() {
131 private File getTargetFile(final File sourceFile, final String targetFormat) {
132 final StringBuffer targetFilePath = new StringBuffer();
133 targetFilePath.append(sourceFile.getParent());
134 targetFilePath.append("/");
135 targetFilePath.append(FilePathParser
136 .getFileNameWithoutExtension(sourceFile));
137 targetFilePath.append(".");
138 targetFilePath.append(targetFormat);
140 return new File(targetFilePath.toString());
144 public void run(final String[] args) {
146 // parse incoming commandline arguments
147 encodingOptions = commandlineHandler.parseCommandlineArguments(args);
149 if (encodingOptions == null) {
150 showCommandlineHelp();
154 encodingPlan = new EncodingPlan();
156 compileEncodingPlan(encodingOptions.workingDirectory);
158 if (!encodingOptions.testOnly)
160 encodingPlan.execute(encodingOptions);
161 } catch (final Exception exception) {
162 exception.printStackTrace();
168 public void showCommandlineHelp() {
169 commandlineHandler.initParser().showHelp();
170 System.out.println("Example commands:");
172 .println(" Convert all MTS files in the current directory into MP4's.");
173 System.out.println(" meviz encode -o mp4 -i *.MTS");