/*
* Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
*
* This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+*/
package eu.svjatoslav.meviz.encoder;
-import java.io.File;
-import java.util.List;
-
import eu.svjatoslav.commons.file.FilePathParser;
import eu.svjatoslav.commons.string.WildCardMatcher;
import eu.svjatoslav.meviz.Module;
import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
+import java.io.File;
+import java.util.List;
+
public class Encoder implements Module {
- public FormatsRegistry encoderRegistry = new FormatsRegistry();
-
- public EncodingOptions encodingOptions;
-
- public EncodingPlan encodingPlan;
-
- CommandlineHandler commandlineHandler = new CommandlineHandler();
-
- /**
- * Generate encoding plan
- *
- * @param sourceFile
- * Source directory of file
- */
- public void compileEncodingPlan(final File sourceFile) {
- if (!sourceFile.exists()) {
- System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
- + "\" does not exist.");
- return;
- }
-
- if (sourceFile.isDirectory()) {
- // handle directory
- for (final File subFile : sourceFile.listFiles())
- if (subFile.isDirectory()) {
- if (encodingOptions.recursive)
- compileEncodingPlan(subFile);
- } else
- compileEncodingPlan(subFile);
- } else if (sourceFile.isFile())
- if (fileMatchesInputPattern(sourceFile)) {
-
- // System.out.println("Processing file: " +
- // file.getAbsolutePath());
-
- final String sourceFileExtension = FilePathParser
- .getFileExtension(sourceFile);
-
- // encode source file into every desired target format
- for (final String targetFormat : encodingOptions.outputFormats) {
-
- // construct target file
- final File targetFile = getTargetFile(sourceFile,
- targetFormat);
-
- // System.out.println("target path: " +
- // targetFilePath.toString());
-
- if (!targetFile.exists()) {
-
- final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
- .getEncoders(sourceFileExtension, targetFormat);
-
- if (formats.size() == 0)
- System.out
- .println("Error: no encoders found to convert file \""
- + sourceFile.getAbsolutePath()
- + "\" into "
- + targetFormat
- + " format.");
- else if (formats.size() > 1)
- System.out
- .println("Error: Encoders piping not yet supported to convert file \""
- + sourceFile.getAbsolutePath()
- + "\" into "
- + targetFormat
- + " format.");
- else {
- final AbstractConverter chosenFormat = formats
- .get(0);
- final EncodingTask encodingTask = new EncodingTask(
- sourceFile, targetFile, chosenFormat,
- targetFormat);
-
- if (chosenFormat.isTerminalMandatory())
- encodingTask.setUseTerminal(true);
- encodingPlan.scheduleTask(encodingTask);
- }
- }
- }
- }
-
- }
-
- /**
- * @param file
- * single file candidate to potentially be encoded
- *
- * @return <code>true</code> if file shall be encoded.
- */
- public boolean fileMatchesInputPattern(final File file) {
- final String fileName = file.getName().toLowerCase();
-
- for (final String inputPattern : encodingOptions.inputPatterns)
- if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
- return true;
-
- return false;
- }
-
- @Override
- public String getDescription() {
- return "Convert between various media formats.";
- }
-
- @Override
- public String getModuleCommand() {
- return "encode";
- }
-
- private File getTargetFile(final File sourceFile, final String targetFormat) {
- final StringBuffer targetFilePath = new StringBuffer();
- targetFilePath.append(sourceFile.getParent());
- targetFilePath.append("/");
- targetFilePath.append(FilePathParser
- .getFileNameWithoutExtension(sourceFile));
- targetFilePath.append(".");
- targetFilePath.append(targetFormat);
-
- return new File(targetFilePath.toString());
- }
-
- @Override
- public void run(final String[] args) {
-
- // parse incoming commandline arguments
- encodingOptions = commandlineHandler.parseCommandlineArguments(args);
-
- if (encodingOptions == null) {
- showCommandlineHelp();
- return;
- }
-
- encodingPlan = new EncodingPlan();
-
- compileEncodingPlan(encodingOptions.workingDirectory);
-
- if (!encodingOptions.testOnly)
- try {
- encodingPlan.execute(encodingOptions);
- } catch (final Exception exception) {
- exception.printStackTrace();
- }
-
- }
-
- @Override
- public void showCommandlineHelp() {
- commandlineHandler.parser.showHelp();
- System.out.println("Example commands:");
- System.out
- .println(" Convert all MTS files in the current directory into MP4's.");
- System.out.println(" meviz encode -o mp4 -i *.MTS");
- }
+ private final FormatsRegistry encoderRegistry = new FormatsRegistry();
+ private final CommandlineHandler commandlineHandler = new CommandlineHandler();
+ private EncodingOptions encodingOptions;
+ private EncodingPlan encodingPlan;
+
+ /**
+ * Generate encoding plan
+ *
+ * @param sourceFile Source directory of file
+ */
+ private void compileEncodingPlan(final File sourceFile) {
+ if (!sourceFile.exists()) {
+ System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
+ + "\" does not exist.");
+ return;
+ }
+
+ if (sourceFile.isDirectory()) {
+ // handle directory
+ for (final File subFile : sourceFile.listFiles())
+ if (subFile.isDirectory()) {
+ if (encodingOptions.isRecursive())
+ compileEncodingPlan(subFile);
+ } else
+ compileEncodingPlan(subFile);
+ } else if (sourceFile.isFile())
+ if (fileMatchesInputPattern(sourceFile)) {
+
+ // System.out.println("Processing file: " +
+ // file.getAbsolutePath());
+
+ final String sourceFileExtension = FilePathParser
+ .getFileExtension(sourceFile);
+
+ // encode source file into every desired target format
+ for (final String targetFormat : encodingOptions.getOutputFormats()) {
+
+ // construct target file
+ final File targetFile = getTargetFile(sourceFile,
+ targetFormat);
+
+ // System.out.println("target path: " +
+ // targetFilePath.toString());
+
+ if (!targetFile.exists()) {
+
+ final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
+ .getEncoders(sourceFileExtension, targetFormat);
+
+ if (formats.size() == 0)
+ System.out
+ .println("Error: no encoders found to convert file \""
+ + sourceFile.getAbsolutePath()
+ + "\" into "
+ + targetFormat
+ + " format.");
+ else if (formats.size() > 1)
+ System.out
+ .println("Error: Encoders piping not yet supported to convert file \""
+ + sourceFile.getAbsolutePath()
+ + "\" into "
+ + targetFormat
+ + " format.");
+ else {
+ final AbstractConverter chosenFormat = formats
+ .get(0);
+ final EncodingTask encodingTask = new EncodingTask(
+ sourceFile, targetFile, chosenFormat,
+ targetFormat);
+
+ if (chosenFormat.isTerminalMandatory())
+ encodingTask.setUseTerminal(true);
+ encodingPlan.scheduleTask(encodingTask);
+ }
+ }
+ }
+ }
+
+ }
+
+ /**
+ * @param file single file candidate to potentially be encoded
+ * @return <code>true</code> if file shall be encoded.
+ */
+ private boolean fileMatchesInputPattern(final File file) {
+ final String fileName = file.getName().toLowerCase();
+
+ for (final String inputPattern : encodingOptions.getInputPatterns())
+ if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
+ return true;
+
+ return false;
+ }
+
+ @Override
+ public String getDescription() {
+ return "Convert between various media formats.";
+ }
+
+ @Override
+ public String getModuleCommand() {
+ return "encode";
+ }
+
+ private File getTargetFile(final File sourceFile, final String targetFormat) {
+ String targetFilePath = sourceFile.getParent() +
+ "/" +
+ FilePathParser
+ .getFileNameWithoutExtension(sourceFile) +
+ "." +
+ targetFormat;
+
+ return new File(targetFilePath);
+ }
+
+ @Override
+ public void run(final String[] args) {
+
+ // parse incoming commandline arguments
+ encodingOptions = commandlineHandler.parseCommandlineArguments(args);
+
+ if (encodingOptions == null) {
+ showCommandlineHelp();
+ return;
+ }
+
+ encodingPlan = new EncodingPlan();
+
+ compileEncodingPlan(encodingOptions.getWorkingDirectory());
+
+ if (!encodingOptions.isTestOnly())
+ try {
+ encodingPlan.execute(encodingOptions);
+ } catch (final Exception exception) {
+ exception.printStackTrace();
+ }
+
+ }
+
+ @Override
+ public void showCommandlineHelp() {
+ commandlineHandler.parser.showHelp();
+ System.out.println("Example commands:");
+ System.out
+ .println(" Convert all MTS files in the current directory into MP4's.");
+ System.out.println(" meviz encode -o mp4 -i *.MTS");
+ }
}