f078634a863f32936ee19ed5ec21208a3753589f
[meviz.git] / 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.EncodingOptions;
21 import eu.svjatoslav.meviz.encoder.EncodingPlan;
22 import eu.svjatoslav.meviz.encoder.EncodingTask;
23 import eu.svjatoslav.meviz.encoder.converters.Converter;
24 import eu.svjatoslav.meviz.encoder.converters.Mts2Mp4;
25
26 public class Main implements Module {
27
28         @Override
29         public String getDescription() {
30                 return "Convenience helper to retrieve contents of inserted memory card.";
31         }
32
33         public File[] getDiskVideos(final File file) {
34                 final String videosPath = file.getAbsolutePath()
35                                 + "/AVCHD/BDMV/STREAM/";
36                 final File videosDirectory = new File(videosPath);
37
38                 if (!videosDirectory.exists()) {
39                         return new File[0];
40                 }
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                 }
50                 return valueString;
51         };
52
53         @Override
54         public String getModuleCommand() {
55                 return "grabmemcard";
56         }
57
58         @SuppressWarnings("deprecation")
59         public File getTargetDirectory() {
60                 final Date now = new Date();
61                 final GregorianCalendar calendar = new GregorianCalendar();
62
63                 final StringBuffer directoryName = new StringBuffer();
64                 directoryName.append(calendar.get(GregorianCalendar.YEAR) + "."
65                                 + getDoubleDigit(calendar.get(GregorianCalendar.MONTH) + 1)
66                                 + "."
67                                 + getDoubleDigit(calendar.get(GregorianCalendar.DAY_OF_MONTH))
68                                 + " ");
69
70                 directoryName.append(getDoubleDigit(now.getHours()) + ":"
71                                 + getDoubleDigit(now.getMinutes()) + ":"
72                                 + getDoubleDigit(now.getSeconds()));
73
74                 final String targetDirectoryPath = CommonPathResolver
75                                 .getDesktopDirectory().getAbsolutePath()
76                                 + "/"
77                                 + directoryName.toString() + "/";
78
79                 System.out.println("Target directory path: " + targetDirectoryPath);
80
81                 return new File(targetDirectoryPath);
82         }
83
84         @Override
85         public void run(final String[] args) throws IOException {
86
87                 final File targetDirectory = getTargetDirectory();
88                 targetDirectory.mkdirs();
89
90                 final Converter converter = new Mts2Mp4();
91                 final EncodingPlan encodingPlan = new EncodingPlan();
92
93                 final String userName = System.getProperty("user.name");
94
95                 // final File file = new File("/media/" + userName + "/");
96
97                 final File file = new File("/media/");
98
99                 for (final File insertedDisk : file.listFiles()) {
100                         final File[] diskVideos = getDiskVideos(insertedDisk);
101
102                         for (final File sourceFile : diskVideos) {
103
104                                 // compute target file path
105                                 final StringBuffer targetFilePath = new StringBuffer();
106                                 targetFilePath.append(targetDirectory.getAbsolutePath());
107                                 targetFilePath.append("/");
108                                 targetFilePath.append(FilePathParser
109                                                 .getFileNameWithoutExtension(sourceFile));
110                                 targetFilePath.append(".mp4");
111                                 final File targetFile = new File(targetFilePath.toString());
112
113                                 final EncodingTask encodingTask = new EncodingTask(sourceFile,
114                                                 targetFile, converter);
115                                 encodingPlan.scheduleTask(encodingTask);
116                         }
117                 }
118
119                 final EncodingOptions options = new EncodingOptions();
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 }