OSDN Git Service

6f4a4fe9d5e41cafeb24b18af96c74876b4086a7
[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 java.util.concurrent.Callable;
9 import saccubus.ConvertStopFlag;
10 import saccubus.net.TextProgressListener;
11
12 /**
13  * <p>タイトル: さきゅばす</p>
14  *
15  * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
16  *
17  * <p>著作権: Copyright (c) 2007 PSI</p>
18  *
19  * <p>会社名: </p>
20  *
21  * @author 未入力
22  * @version 1.0
23  */
24 public class Converter extends AbstractCommand implements Runnable, Callable<Boolean> {
25
26     private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
27     private final Profile Setting;
28     private final String Tag;
29     private final String Time;
30
31     public Converter(String url, String time, Profile setting,
32             TextProgressListener listener, ConvertStopFlag flag) {
33         super(listener, flag);
34         url = url.trim();
35         if (url.startsWith(VIDEO_URL_PARSER)) {
36             int index = url.indexOf('?', VIDEO_URL_PARSER.length());
37             if (index >= 0) {
38                 Tag = url.substring(VIDEO_URL_PARSER.length(), index);
39             } else {
40                 Tag = url.substring(VIDEO_URL_PARSER.length());
41             }
42         } else {
43             Tag = url;
44         }
45         Time = time;
46         Setting = setting;
47     }
48
49     public Boolean call() throws Exception {
50         boolean result = false;
51         try {
52             result = runConvert();
53         } finally {
54             getStopFlag().finished();
55         }
56         return Boolean.valueOf(result);
57     }
58
59     public void run() {
60         try {
61             call();
62         } catch (Exception ex) {
63             String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
64             sendText(text);
65             ex.printStackTrace();
66         }
67     }
68
69     private boolean runConvert() throws IOException, InterruptedException {
70         if (!Setting.shouldRun()) {
71             sendText("何もすることがありません");
72             return true;
73         }
74
75         validSetting();
76         final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
77
78         sendText("ログイン中");
79
80         final FileInstanciator fi = createInstanciator();
81
82         stopFlagReturn();
83
84         final File videoFile = fi.getVideoFile(getListener());
85
86         stopFlagReturn();
87
88         File commentFile = fi.getCommentFile(getListener());
89
90         stopFlagReturn();
91
92         File tcommFile = fi.getTcommFile(getListener());
93
94         if (!Setting.needsConvert()) {
95             sendText("動画・コメントを保存し、変換は行いませんでした。");
96             return true;
97         }
98
99         if (!videoFile.isFile()) {
100             throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
101         }
102
103         if (Setting.getOutputFileSetting().isAddComment()) {
104             if (!commentFile.isFile()) {
105                 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
106             }
107         } else {
108             commentFile = null;
109         }
110
111         if (Setting.getOutputFileSetting().isAddTcomment()) {
112             if (!tcommFile.isFile()) {
113                 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
114             }
115         } else {
116             tcommFile = null;
117         }
118
119         /*ビデオ名の確定*/
120         File convertedVideoFile;
121         if (!Setting.getOutputFileSetting().getFile().isFile()) {
122             String conv_name = fi.getVideoTitle();
123             if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
124                 conv_name = getVideoIDWithBracket() + conv_name;
125             }
126             convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
127                     conv_name + ov.getExtOption());
128         } else {
129             String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
130             if (!filename.endsWith(ov.getExtOption())) {
131                 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
132                 convertedVideoFile = new File(filename);
133             } else {
134                 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
135             }
136         }
137
138         boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
139                 convertedVideoFile, Setting.getFfmpeg(), Setting.getGeneralSetting()).execute();
140         if (res) {
141             if (Setting.getCommentSetting().isDelete()) {
142                 commentFile.delete();
143             }
144             if (Setting.getVideoSetting().isDelete()) {
145                 videoFile.delete();
146             }
147             if (Setting.getTcommentSetting().isDelete()) {
148                 tcommFile.delete();
149             }
150         }
151         return res;
152     }
153
154     private FileInstanciator createInstanciator() throws IOException {
155         FileInstanciator fi;
156
157         FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
158
159         FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
160                 getCommentSetting(), Setting.getCommentGetInfo().isselfAdjustCommentNum(), Setting.getCommentGetInfo().
161                 getBackComment());
162
163         FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
164                 Setting.getTcommentSetting());
165
166         fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
167         return fi;
168     }
169
170     /**
171      * (ネットワーク設定以外の)設定を検証する.
172      * @throws IllegalArgumentException 設定に不備がある場合.
173      */
174     private void validSetting() {
175         if (Setting.needsConvert()) {
176             File a = Setting.getFfmpeg().getFfmpeg();
177             if (!a.canRead()) {
178                 throw new IllegalArgumentException("FFmpegが見つかりません。");
179             }
180             if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
181                 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
182             }
183             a = Setting.getFfmpeg().getVhook();
184             if (!a.canRead()) {
185                 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
186             }
187             a = Setting.getFfmpeg().getFont();
188             if (!a.canRead()) {
189                 throw new IllegalArgumentException("フォントが見つかりません。");
190             }
191         } else {
192             if (Setting.getVideoSetting().isDelete()) {
193                 throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
194             }
195             if (Setting.getCommentSetting().isDelete()) {
196                 throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
197             }
198             if (Setting.getTcommentSetting().isDelete()) {
199                 throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
200             }
201         }
202     }
203
204     private String getVideoIDWithBracket() {
205         return "[" + Tag + "]";
206     }
207
208     public boolean isConverted() {
209         return getStopFlag().isFinished();
210     }
211
212     @Override
213     public ConvertStopFlag getStopFlag() {
214         return super.getStopFlag();
215     }
216 }