dac58fd09230e36eac7e2f426aeac956b051e9b6
[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.AvconvVideo;
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 AvconvVideo();
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                 for (final File insertedDisk : file.listFiles()) {
97                         final File[] diskVideos = getDiskVideos(insertedDisk);
98
99                         for (final File sourceFile : diskVideos) {
100
101                                 // compute target file path
102                                 final StringBuffer targetFilePath = new StringBuffer();
103                                 targetFilePath.append(targetDirectory.getAbsolutePath());
104                                 targetFilePath.append("/");
105                                 targetFilePath.append(FilePathParser
106                                                 .getFileNameWithoutExtension(sourceFile));
107                                 targetFilePath.append(".mp4");
108                                 final File targetFile = new File(targetFilePath.toString());
109
110                                 final EncodingTask encodingTask = new EncodingTask(sourceFile,
111                                                 targetFile, converter, "mp4");
112                                 encodingPlan.scheduleTask(encodingTask);
113                         }
114                 }
115
116                 final EncodingOptions options = new EncodingOptions();
117                 options.setVideoBitrate(BitrateParameter.bitrate.COPY);
118
119                 encodingPlan.execute(options);
120         }
121
122         @Override
123         public void showCommandlineHelp() {
124                 System.out.println("memory card grabber has no commandline options");
125         }
126
127 }