more elegant commandline handling
[meviz.git] / src / main / java / eu / svjatoslav / meviz / grabmemcard / Main.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.meviz.grabmemcard;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Date;
15 import java.util.GregorianCalendar;
16
17 import eu.svjatoslav.commons.file.CommonPathResolver;
18 import eu.svjatoslav.commons.file.FilePathParser;
19 import eu.svjatoslav.meviz.Module;
20 import eu.svjatoslav.meviz.encoder.BitrateParameter;
21 import eu.svjatoslav.meviz.encoder.EncodingOptions;
22 import eu.svjatoslav.meviz.encoder.EncodingPlan;
23 import eu.svjatoslav.meviz.encoder.EncodingTask;
24 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
25 import eu.svjatoslav.meviz.encoder.converters.Ffmpeg;
26
27 public class Main implements Module {
28
29         @Override
30         public String getDescription() {
31                 return "Convenience helper to retrieve contents of inserted memory card.";
32         }
33
34         public File[] getDiskVideos(final File file) {
35                 final String videosPath = file.getAbsolutePath()
36                                 + "/AVCHD/BDMV/STREAM/";
37                 final File videosDirectory = new File(videosPath);
38
39                 if (!videosDirectory.exists())
40                         return new File[0];
41
42                 return videosDirectory.listFiles();
43         }
44
45         public String getDoubleDigit(final int value) {
46                 String valueString = Integer.toString(value);
47                 if (valueString.length() == 1)
48                         valueString = "0" + valueString;
49                 return valueString;
50         };
51
52         @Override
53         public String getModuleCommand() {
54                 return "grabmemcard";
55         }
56
57         @SuppressWarnings("deprecation")
58         public File getTargetDirectory() {
59                 final Date now = new Date();
60                 final GregorianCalendar calendar = new GregorianCalendar();
61
62                 final StringBuffer directoryName = new StringBuffer();
63                 directoryName.append(calendar.get(GregorianCalendar.YEAR) + "."
64                                 + getDoubleDigit(calendar.get(GregorianCalendar.MONTH) + 1)
65                                 + "."
66                                 + getDoubleDigit(calendar.get(GregorianCalendar.DAY_OF_MONTH))
67                                 + " ");
68
69                 directoryName.append(getDoubleDigit(now.getHours()) + ":"
70                                 + getDoubleDigit(now.getMinutes()) + ":"
71                                 + getDoubleDigit(now.getSeconds()));
72
73                 final String targetDirectoryPath = CommonPathResolver
74                                 .getDesktopDirectory().getAbsolutePath()
75                                 + "/"
76                                 + directoryName.toString() + "/";
77
78                 System.out.println("Target directory path: " + targetDirectoryPath);
79
80                 return new File(targetDirectoryPath);
81         }
82
83         @Override
84         public void run(final String[] args) throws IOException {
85
86                 final File targetDirectory = getTargetDirectory();
87                 targetDirectory.mkdirs();
88
89                 final AbstractConverter converter = new Ffmpeg();
90                 final EncodingPlan encodingPlan = new EncodingPlan();
91
92                 final String userName = System.getProperty("user.name");
93
94                 // final File file = new File("/media/" + userName + "/");
95
96                 final File file = new File("/media/");
97
98                 for (final File insertedDisk : file.listFiles()) {
99                         final File[] diskVideos = getDiskVideos(insertedDisk);
100
101                         for (final File sourceFile : diskVideos) {
102
103                                 // compute target file path
104                                 final StringBuffer targetFilePath = new StringBuffer();
105                                 targetFilePath.append(targetDirectory.getAbsolutePath());
106                                 targetFilePath.append("/");
107                                 targetFilePath.append(FilePathParser
108                                                 .getFileNameWithoutExtension(sourceFile));
109                                 targetFilePath.append(".mp4");
110                                 final File targetFile = new File(targetFilePath.toString());
111
112                                 final EncodingTask encodingTask = new EncodingTask(sourceFile,
113                                                 targetFile, converter);
114                                 encodingPlan.scheduleTask(encodingTask);
115                         }
116                 }
117
118                 final EncodingOptions options = new EncodingOptions();
119                 options.setVideoBitrate(BitrateParameter.bitrate.COPY);
120
121                 encodingPlan.execute(options);
122         }
123
124         @Override
125         public void showCommandlineHelp() {
126                 System.out.println("memory card grabber has no commandline options");
127         }
128
129 }