X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Fencoder%2FEncoder.java;h=073f47ae1a78ce4440cb85a7cf82ec6d15ca2118;hb=cf6e4ace4972f24f40f88ea12fcf99c763e4e40a;hp=420fa493d0fb693528a305edca8b92080ebbd268;hpb=0a7c6bdea0eb481cab530d6168c5c1a8d942c012;p=meviz.git diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/Encoder.java b/src/main/java/eu/svjatoslav/meviz/encoder/Encoder.java index 420fa49..073f47a 100755 --- a/src/main/java/eu/svjatoslav/meviz/encoder/Encoder.java +++ b/src/main/java/eu/svjatoslav/meviz/encoder/Encoder.java @@ -1,7 +1,7 @@ /* * Meviz - Various tools collection to work with multimedia. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2012 -- 2018, 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. @@ -9,168 +9,162 @@ 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.Converter; +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 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 Converter chosenFormat = formats.get(0); - final EncodingTask encodingTask = new EncodingTask( - sourceFile, targetFile, chosenFormat); - - if (encodingOptions.terminal - || chosenFormat.isTerminalMandatory()) - encodingTask.setUseTerminal(true); - encodingPlan.scheduleTask(encodingTask); - } - } - } - } - - } - - /** - * @param file - * single file candidate to potentially be encoded - * - * @return true 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 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 true 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"); + } }