1 package saccubus.converter;
3 import saccubus.converter.profile.Profile;
4 import saccubus.converter.profile.FfmpegOption;
5 import saccubus.converter.filegetter.FileInstanciator;
7 import java.io.IOException;
8 import java.util.concurrent.Callable;
9 import saccubus.ConvertStopFlag;
10 import saccubus.net.TextProgressListener;
15 * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
17 * <p>著作権: Copyright (c) 2007 PSI</p>
24 public class Converter extends AbstractCommand implements Runnable, Callable<Boolean> {
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;
31 public Converter(String url, String time, Profile setting,
32 TextProgressListener listener, ConvertStopFlag flag) {
33 super(listener, flag);
35 if (url.startsWith(VIDEO_URL_PARSER)) {
36 int index = url.indexOf('?', VIDEO_URL_PARSER.length());
38 Tag = url.substring(VIDEO_URL_PARSER.length(), index);
40 Tag = url.substring(VIDEO_URL_PARSER.length());
49 public Boolean call() throws Exception {
50 boolean result = false;
52 result = runConvert();
54 getStopFlag().finished();
56 return Boolean.valueOf(result);
62 } catch (Exception ex) {
63 String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
69 private boolean runConvert() throws IOException, InterruptedException {
70 if (!Setting.shouldRun()) {
71 sendText("何もすることがありません");
76 final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
80 final FileInstanciator fi = createInstanciator();
84 final File videoFile = fi.getVideoFile(getListener());
88 File commentFile = fi.getCommentFile(getListener());
92 File tcommFile = fi.getTcommFile(getListener());
94 if (!Setting.needsConvert()) {
95 sendText("動画・コメントを保存し、変換は行いませんでした。");
99 if (!videoFile.isFile()) {
100 throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
103 if (Setting.getOutputFileSetting().isAddComment()) {
104 if (!commentFile.isFile()) {
105 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
111 if (Setting.getOutputFileSetting().isAddTcomment()) {
112 if (!tcommFile.isFile()) {
113 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
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;
126 convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
127 conv_name + ov.getExtOption());
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);
134 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
138 boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
139 convertedVideoFile, Setting.getFfmpeg(), Setting.getGeneralSetting()).execute();
141 if (Setting.getCommentSetting().isDelete()) {
142 commentFile.delete();
144 if (Setting.getVideoSetting().isDelete()) {
147 if (Setting.getTcommentSetting().isDelete()) {
154 private FileInstanciator createInstanciator() throws IOException {
157 FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
159 FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
160 getCommentSetting(), Setting.getCommentGetInfo().isSelfAdjustCommentNum(), Setting.getCommentGetInfo().
161 getBackComment(), Setting.getCommentGetInfo().isReduceComment());
163 FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
164 Setting.getTcommentSetting());
166 fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
171 * (ネットワーク設定以外の)設定を検証する.
172 * @throws IllegalArgumentException 設定に不備がある場合.
174 private void validSetting() {
175 if (Setting.needsConvert()) {
176 File a = Setting.getFfmpeg().getFfmpeg();
178 throw new IllegalArgumentException("FFmpegが見つかりません。");
180 if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
181 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
183 a = Setting.getFfmpeg().getVhook();
185 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
187 a = Setting.getFfmpeg().getFont();
189 throw new IllegalArgumentException("フォントが見つかりません。");
192 if (Setting.getVideoSetting().isDelete()) {
193 throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
195 if (Setting.getCommentSetting().isDelete()) {
196 throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
198 if (Setting.getTcommentSetting().isDelete()) {
199 throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
204 private String getVideoIDWithBracket() {
205 return "[" + Tag + "]";
208 public boolean isConverted() {
209 return getStopFlag().isFinished();
213 public ConvertStopFlag getStopFlag() {
214 return super.getStopFlag();