OSDN Git Service

merge.
[coroid/inqubus.git] / frontend / src / saccubus / converter / Converter.java
1 package saccubus.converter;
2
3 import saccubus.converter.profile.Profile;
4 import saccubus.converter.profile.FfmpegOption;
5 import saccubus.converter.filegetter.FileInstanciator;
6 import java.io.File;
7 import java.io.IOException;
8 import saccubus.ConvertStopFlag;
9 import saccubus.net.TextProgressListener;
10
11 /**
12  * <p>\83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·</p>
13  *
14  * <p>\90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91¶</p>
15  *
16  * <p>\92\98\8dì\8c : Copyright (c) 2007 PSI</p>
17  *
18  * <p>\89ï\8eÐ\96¼: </p>
19  *
20  * @author \96¢\93ü\97Í
21  * @version 1.0
22  */
23 public class Converter extends AbstractCommand implements Runnable {
24
25     private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
26     private final Profile Setting;
27     private final String Tag;
28     private final String Time;
29
30     public Converter(String url, String time, Profile setting,
31             TextProgressListener listener, ConvertStopFlag flag) {
32         super(listener, flag);
33         url = url.trim();
34         if (url.startsWith(VIDEO_URL_PARSER)) {
35             int index = url.indexOf('?', VIDEO_URL_PARSER.length());
36             if (index >= 0) {
37                 Tag = url.substring(VIDEO_URL_PARSER.length(), index);
38             } else {
39                 Tag = url.substring(VIDEO_URL_PARSER.length());
40             }
41         } else {
42             Tag = url;
43         }
44         Time = time;
45         Setting = setting;
46     }
47
48     @Override
49     public void run() {
50         try {
51             runConvert();
52         } catch (Exception ex) {
53             String text = (ex.getMessage() != null) ? ex.getMessage() : "\97\\8aú\82µ\82È\82¢\83G\83\89\81[\94­\90\82Ì\82½\82ß\92\86\92f\82µ\82Ü\82µ\82½\81B";
54             sendText(text);
55             ex.printStackTrace();
56         } finally {
57             getStopFlag().finished();
58         }
59     }
60
61     private void runConvert() throws IOException, InterruptedException {
62         if (!Setting.shouldRun()) {
63             sendText("\89½\82à\82·\82é\82±\82Æ\82ª\82 \82è\82Ü\82¹\82ñ");
64             return;
65         }
66
67         validSetting();
68         final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
69
70         sendText("\83\8d\83O\83C\83\93\92\86");
71
72         final FileInstanciator fi = createInstanciator();
73
74         stopFlagReturn();
75
76         final File videoFile = fi.getVideoFile(getListener());
77
78         stopFlagReturn();
79
80         File commentFile = fi.getCommentFile(getListener());
81
82         stopFlagReturn();
83
84         File tcommFile = fi.getTcommFile(getListener());
85
86         if (!Setting.needsConvert()) {
87             sendText("\93®\89æ\81E\83R\83\81\83\93\83g\82ð\95Û\91\82µ\81A\95Ï\8a·\82Í\8ds\82¢\82Ü\82¹\82ñ\82Å\82µ\82½\81B");
88             return;
89         }
90         if (!videoFile.isFile()) {
91             throw new IOException("\93ü\97Í\93®\89æ\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ:" + videoFile.getPath());
92         }
93
94         if (Setting.getOutputFileSetting().isAddComment()) {
95             if (!commentFile.isFile()) {
96                 throw new IOException("\93ü\97Í\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ:" + commentFile.getPath());
97             }
98         } else {
99             commentFile = null;
100         }
101
102         if (Setting.getOutputFileSetting().isAddTcomment()) {
103             if (!tcommFile.isFile()) {
104                 throw new IOException("\93ü\97Í\93\8a\8de\8eÒ\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ:" + tcommFile.getPath());
105             }
106         } else {
107             tcommFile = null;
108         }
109
110         /*\83r\83f\83I\96¼\82Ì\8am\92è*/
111         File convertedVideoFile;
112         if (!Setting.getOutputFileSetting().getFile().isFile()) {
113             if (fi.getVideoTitle() == null) {
114                 sendText("\95Ï\8a·\8cã\82Ì\83r\83f\83I\83t\83@\83C\83\8b\96¼\82ª\8am\92è\82Å\82«\82Ü\82¹\82ñ\81B");
115                 return;
116             }
117             String conv_name = fi.getVideoTitle();
118             if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
119                 conv_name = getVideoIDWithBracket() + conv_name;
120             }
121             convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
122                     conv_name + ov.getExtOption());
123         } else {
124             String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
125             if (!filename.endsWith(ov.getExtOption())) {
126                 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
127                 convertedVideoFile = new File(filename);
128             } else {
129                 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
130             }
131         }
132
133         boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
134                 convertedVideoFile, Setting.getFfmpeg()).execute();
135         if (res) {
136             if (Setting.getCommentSetting().isDelete()) {
137                 commentFile.delete();
138             }
139             if (Setting.getVideoSetting().isDelete()) {
140                 videoFile.delete();
141             }
142             if (Setting.getTcommentSetting().isDelete()) {
143                 tcommFile.delete();
144             }
145         }
146     }
147
148     private FileInstanciator createInstanciator() throws IOException {
149         FileInstanciator fi;
150
151         FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
152
153         FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
154                 getCommentSetting(), Setting.getCommentGetInfo().isselfAdjustCommentNum(), Setting.getCommentGetInfo().
155                 getBackComment());
156
157         FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
158                 Setting.getTcommentSetting());
159
160         fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
161         return fi;
162     }
163
164     /**
165      * (\83l\83b\83g\83\8f\81[\83N\90Ý\92è\88È\8aO\82Ì)\90Ý\92è\82ð\8c\9f\8fØ\82·\82é.
166      * @throws IllegalArgumentException \90Ý\92è\82É\95s\94õ\82ª\82 \82é\8fê\8d\87.
167      */
168     private void validSetting() {
169         if (Setting.needsConvert()) {
170             File a = Setting.getFfmpeg().getFfmpeg();
171             if (!a.canRead()) {
172                 throw new IllegalArgumentException("FFmpeg\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
173             }
174             if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
175                 throw new IllegalArgumentException("\82·\82¢\82Ü\82¹\82ñ\81B\8c»\8dÝvhook\83\89\83C\83u\83\89\83\8a\82É\82Í\94¼\8ap\8bó\94\92\82Í\8eg\82¦\82Ü\82¹\82ñ\81B");
176             }
177             a = Setting.getFfmpeg().getVhook();
178             if (!a.canRead()) {
179                 throw new IllegalArgumentException("Vhook\83\89\83C\83u\83\89\83\8a\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
180             }
181             a = Setting.getFfmpeg().getFont();
182             if (!a.canRead()) {
183                 throw new IllegalArgumentException("\83t\83H\83\93\83g\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
184             }
185         } else {
186             if (Setting.getVideoSetting().isDelete()) {
187                 throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93®\89æ\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
188             }
189             if (Setting.getCommentSetting().isDelete()) {
190                 throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
191             }
192             if (Setting.getTcommentSetting().isDelete()) {
193                 throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93\8a\8de\8eÒ\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
194             }
195         }
196     }
197
198     private String getVideoIDWithBracket() {
199         return "[" + Tag + "]";
200     }
201
202     public boolean isConverted() {
203         return getStopFlag().isFinished();
204     }
205
206     @Override
207     public ConvertStopFlag getStopFlag() {
208         return super.getStopFlag();
209     }
210 }