Had fight with maven. It decided to block HTTP repositories.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / Encoder.java
1 /*
2  * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5
6
7 package eu.svjatoslav.meviz.encoder;
8
9 import eu.svjatoslav.commons.file.FilePathParser;
10 import eu.svjatoslav.commons.string.GlobMatcher;
11 import eu.svjatoslav.meviz.Module;
12 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
13
14 import java.io.File;
15 import java.util.List;
16
17 public class Encoder implements Module {
18
19     private final FormatsRegistry encoderRegistry = new FormatsRegistry();
20     private final CommandlineHandler commandlineHandler = new CommandlineHandler();
21     private EncodingOptions encodingOptions;
22     private EncodingPlan encodingPlan;
23
24     /**
25      * Generate encoding plan
26      *
27      * @param sourceFile Source directory of file
28      */
29     private void compileEncodingPlan(final File sourceFile) {
30         if (!sourceFile.exists()) {
31             System.out.println("Error: file \"" + sourceFile.getAbsolutePath()
32                     + "\" does not exist.");
33             return;
34         }
35
36         if (sourceFile.isDirectory()) {
37             // handle directory
38             for (final File subFile : sourceFile.listFiles())
39                 if (subFile.isDirectory()) {
40                     if (encodingOptions.isRecursive())
41                         compileEncodingPlan(subFile);
42                 } else
43                     compileEncodingPlan(subFile);
44         } else if (sourceFile.isFile())
45             if (fileMatchesInputPattern(sourceFile)) {
46
47                 // System.out.println("Processing file: " +
48                 // file.getAbsolutePath());
49
50                 final String sourceFileExtension = FilePathParser
51                         .getFileExtension(sourceFile);
52
53                 // encode source file into every desired target format
54                 for (final String targetFormat : encodingOptions.getOutputFormats()) {
55
56                     // construct target file
57                     final File targetFile = getTargetFile(sourceFile,
58                             targetFormat);
59
60                     // System.out.println("target path: " +
61                     // targetFilePath.toString());
62
63                     if (!targetFile.exists()) {
64
65                         final List<eu.svjatoslav.meviz.encoder.converters.AbstractConverter> formats = encoderRegistry
66                                 .getEncoders(sourceFileExtension, targetFormat);
67
68                         if (formats.size() == 0)
69                             System.out
70                                     .println("Error: no encoders found to convert file \""
71                                             + sourceFile.getAbsolutePath()
72                                             + "\" into "
73                                             + targetFormat
74                                             + " format.");
75                         else if (formats.size() > 1)
76                             System.out
77                                     .println("Error: Encoders piping not yet supported to convert file \""
78                                             + sourceFile.getAbsolutePath()
79                                             + "\" into "
80                                             + targetFormat
81                                             + " format.");
82                         else {
83                             final AbstractConverter chosenFormat = formats
84                                     .get(0);
85                             final EncodingTask encodingTask = new EncodingTask(
86                                     sourceFile, targetFile, chosenFormat,
87                                     targetFormat);
88
89                             if (chosenFormat.isTerminalMandatory())
90                                 encodingTask.setUseTerminal(true);
91                             encodingPlan.scheduleTask(encodingTask);
92                         }
93                     }
94                 }
95             }
96
97     }
98
99     /**
100      * @param file single file candidate to potentially be encoded
101      * @return <code>true</code> if file shall be encoded.
102      */
103     private boolean fileMatchesInputPattern(final File file) {
104         final String fileName = file.getName().toLowerCase();
105
106         for (final String inputPattern : encodingOptions.getInputPatterns())
107             if (GlobMatcher.match(fileName, inputPattern.toLowerCase()))
108                 return true;
109
110         return false;
111     }
112
113     @Override
114     public String getDescription() {
115         return "Convert between various media formats.";
116     }
117
118     @Override
119     public String getModuleCommand() {
120         return "encode";
121     }
122
123     private File getTargetFile(final File sourceFile, final String targetFormat) {
124         String targetFilePath = sourceFile.getParent() +
125                 "/" +
126                 FilePathParser
127                         .getFileNameWithoutExtension(sourceFile) +
128                 "." +
129                 targetFormat;
130
131         return new File(targetFilePath);
132     }
133
134     @Override
135     public void run(final String[] args) {
136
137         // parse incoming commandline arguments
138         encodingOptions = commandlineHandler.parseCommandlineArguments(args);
139
140         if (encodingOptions == null) {
141             showCommandlineHelp();
142             return;
143         }
144
145         encodingPlan = new EncodingPlan();
146
147         compileEncodingPlan(encodingOptions.getWorkingDirectory());
148
149         if (!encodingOptions.isTestOnly())
150             try {
151                 encodingPlan.execute(encodingOptions);
152             } catch (final Exception exception) {
153                 exception.printStackTrace();
154             }
155
156     }
157
158     @Override
159     public void showCommandlineHelp() {
160         commandlineHandler.parser.showHelp();
161         System.out.println("Example commands:");
162         System.out
163                 .println("    Convert all MTS files in the current directory into MP4's.");
164         System.out.println("        meviz encode -o mp4 -i *.MTS");
165     }
166
167 }