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;
33 * @param url 対象となる動画のURL.
39 public Converter(String url, String time, Profile setting,
40 TextProgressListener listener, ConvertStopFlag flag) {
41 super(listener, flag);
43 if (url.startsWith(VIDEO_URL_PARSER)) {
44 int index = url.indexOf('?', VIDEO_URL_PARSER.length());
46 Tag = url.substring(VIDEO_URL_PARSER.length(), index);
48 Tag = url.substring(VIDEO_URL_PARSER.length());
57 public Boolean call() throws Exception {
58 boolean result = false;
60 result = runConvert();
62 getStopFlag().finished();
64 return Boolean.valueOf(result);
70 } catch (Exception ex) {
71 String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
77 private boolean runConvert() throws IOException, InterruptedException {
78 if (!Setting.shouldRun()) {
79 sendText("何もすることがありません");
84 final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
88 final FileInstanciator fi = createInstanciator();
92 final File videoFile = fi.getVideoFile(getListener());
96 File commentFile = fi.getCommentFile(getListener());
100 File tcommFile = fi.getTcommFile(getListener());
102 if (!Setting.needsConvert()) {
103 sendText("動画・コメントを保存し、変換は行いませんでした。");
107 if (!videoFile.isFile()) {
108 throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
111 if (Setting.getOutputFileSetting().isAddComment()) {
112 if (!commentFile.isFile()) {
113 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
119 if (Setting.getOutputFileSetting().isAddTcomment()) {
120 if (!tcommFile.isFile()) {
121 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
128 File convertedVideoFile;
129 if (!Setting.getOutputFileSetting().getFile().isFile()) {
130 String conv_name = fi.getVideoTitle();
131 if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
132 conv_name = getVideoIDWithBracket() + conv_name;
134 convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
135 conv_name + ov.getExtOption());
137 String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
138 if (!filename.endsWith(ov.getExtOption())) {
139 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
140 convertedVideoFile = new File(filename);
142 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
146 boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
147 convertedVideoFile, Setting.getFfmpeg(), Setting.getGeneralSetting()).execute();
149 if (Setting.getCommentSetting().isDelete()) {
150 commentFile.delete();
152 if (Setting.getVideoSetting().isDelete()) {
155 if (Setting.getTcommentSetting().isDelete()) {
162 private FileInstanciator createInstanciator() throws IOException {
165 FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
167 FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
168 getCommentSetting(), Setting.getCommentGetInfo().isselfAdjustCommentNum(), Setting.getCommentGetInfo().
171 FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
172 Setting.getTcommentSetting());
174 fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
179 * (ネットワーク設定以外の)設定を検証する.
180 * @throws IllegalArgumentException 設定に不備がある場合.
182 private void validSetting() {
183 if (Setting.needsConvert()) {
184 File a = Setting.getFfmpeg().getFfmpeg();
186 throw new IllegalArgumentException("FFmpegが見つかりません。");
188 if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
189 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
191 a = Setting.getFfmpeg().getVhook();
193 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
195 a = Setting.getFfmpeg().getFont();
197 throw new IllegalArgumentException("フォントが見つかりません。");
200 if (Setting.getVideoSetting().isDelete()) {
201 throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
203 if (Setting.getCommentSetting().isDelete()) {
204 throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
206 if (Setting.getTcommentSetting().isDelete()) {
207 throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
212 private String getVideoIDWithBracket() {
213 return "[" + Tag + "]";
216 public boolean isConverted() {
217 return getStopFlag().isFinished();
221 public ConvertStopFlag getStopFlag() {
222 return super.getStopFlag();