OSDN Git Service

エラー時出力メッセージを修正。
[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         // TODO \96{\97\88videoFile\82Ì\8eæ\93¾\8e¸\94s\8e\9e\82Ínull\82Å\82Í\82È\82­IOException\82É\82µ\82½\82¢\82ª...
91         if (videoFile == null || !videoFile.isFile()) {
92             throw new IOException("\93ü\97Í\93®\89æ\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
93         }
94
95         if (Setting.getOutputFileSetting().isAddComment()) {
96             if (commentFile == null || !commentFile.isFile()) {
97                 throw new IOException("\93ü\97Í\83R\83\81\83\93\83g\83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ü\82¹\82ñ\81B");
98             }
99         } else {
100             commentFile = null;
101         }
102
103         if (Setting.getOutputFileSetting().isAddTcomment()) {
104             if (tcommFile == null || !tcommFile.isFile()) {
105                 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ñ\81B");
106             }
107         } else {
108             tcommFile = null;
109         }
110
111         /*\83r\83f\83I\96¼\82Ì\8am\92è*/
112         File convertedVideoFile;
113         if (!Setting.getOutputFileSetting().getFile().isFile()) {
114             if (fi.getVideoTitle() == null) {
115                 sendText("\95Ï\8a·\8cã\82Ì\83r\83f\83I\83t\83@\83C\83\8b\96¼\82ª\8am\92è\82Å\82«\82Ü\82¹\82ñ\81B");
116                 return;
117             }
118             String conv_name = fi.getVideoTitle();
119             if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
120                 conv_name = getVideoIDWithBracket() + conv_name;
121             }
122             convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
123                     conv_name + ov.getExtOption());
124         } else {
125             String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
126             if (!filename.endsWith(ov.getExtOption())) {
127                 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
128                 convertedVideoFile = new File(filename);
129             } else {
130                 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
131             }
132         }
133
134         boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
135                 convertedVideoFile, Setting.getFfmpeg()).execute();
136         if (res) {
137             if (Setting.getCommentSetting().isDelete()) {
138                 commentFile.delete();
139             }
140             if (Setting.getVideoSetting().isDelete()) {
141                 videoFile.delete();
142             }
143             if (Setting.getTcommentSetting().isDelete()) {
144                 tcommFile.delete();
145             }
146         }
147     }
148
149     private FileInstanciator createInstanciator() throws IOException {
150         FileInstanciator fi;
151
152         FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
153
154         FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
155                 getCommentSetting(), Setting.getCommentGetInfo().isselfAdjustCommentNum(), Setting.getCommentGetInfo().
156                 getBackComment());
157
158         FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
159                 Setting.getTcommentSetting());
160
161         fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
162         return fi;
163     }
164
165     /**
166      * (\83l\83b\83g\83\8f\81[\83N\90Ý\92è\88È\8aO\82Ì)\90Ý\92è\82ð\8c\9f\8fØ\82·\82é.
167      * @throws IllegalArgumentException \90Ý\92è\82É\95s\94õ\82ª\82 \82é\8fê\8d\87.
168      */
169     private void validSetting() {
170         if (Setting.needsConvert()) {
171             File a = Setting.getFfmpeg().getFfmpeg();
172             if (!a.canRead()) {
173                 throw new IllegalArgumentException("FFmpeg\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
174             }
175             if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
176                 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");
177             }
178             a = Setting.getFfmpeg().getVhook();
179             if (!a.canRead()) {
180                 throw new IllegalArgumentException("Vhook\83\89\83C\83u\83\89\83\8a\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
181             }
182             a = Setting.getFfmpeg().getFont();
183             if (!a.canRead()) {
184                 throw new IllegalArgumentException("\83t\83H\83\93\83g\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
185             }
186         } else {
187             if (Setting.getVideoSetting().isDelete()) {
188                 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");
189             }
190             if (Setting.getCommentSetting().isDelete()) {
191                 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");
192             }
193             if (Setting.getTcommentSetting().isDelete()) {
194                 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");
195             }
196         }
197     }
198
199     private String getVideoIDWithBracket() {
200         return "[" + Tag + "]";
201     }
202
203     public boolean isConverted() {
204         return getStopFlag().isFinished();
205     }
206
207     @Override
208     public ConvertStopFlag getStopFlag() {
209         return super.getStopFlag();
210     }
211 }