1 package saccubus.converter;
4 import java.io.IOException;
5 import java.util.concurrent.Callable;
6 import saccubus.ConvertStopFlag;
7 import saccubus.converter.filegetter.FileInstanciator;
8 import saccubus.net.TextProgressListener;
9 import yukihane.saccubus.converter.profile.FfmpegOption;
10 import yukihane.saccubus.converter.profile.Profile;
15 * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
17 * <p>著作権: Copyright (c) 2007 PSI</p>
24 public class Converter extends AbstractCommand implements Callable<Boolean> {
26 private final Profile profile;
27 private final String movieId;
31 * @param id 対象となる動画のID.
37 public Converter(String id, Profile profile,
38 TextProgressListener listener, ConvertStopFlag flag) {
39 super(listener, flag);
41 this.profile = profile;
45 public Boolean call() throws Exception {
46 boolean result = false;
48 result = runConvert();
50 getStopFlag().finished();
52 return Boolean.valueOf(result);
55 // TODO Runnableを実装しなくなったので削除する
59 } catch (Exception ex) {
60 String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
66 private boolean runConvert() throws IOException, InterruptedException {
67 if (!shouldRun(profile)) {
68 sendText("何もすることがありません");
73 final FfmpegOption ov = profile.getFfmpeg().getFfmpegOption();
77 final FileInstanciator fi = createInstanciator();
81 final File videoFile = fi.getVideoFile(getListener());
85 File commentFile = fi.getCommentFile(getListener());
89 File tcommFile = fi.getTcommFile(getListener());
91 if (!profile.getOutputFileSetting().isConvert()) {
92 sendText("動画・コメントを保存し、変換は行いませんでした。");
96 if (!videoFile.isFile()) {
97 throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
100 if (profile.getOutputFileSetting().isAddComment()) {
101 if (!commentFile.isFile()) {
102 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
108 if (profile.getOutputFileSetting().isAddTcomment()) {
109 if (!tcommFile.isFile()) {
110 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
117 File convertedVideoFile;
118 if (!profile.getOutputFileSetting().getFile().isFile()) {
119 String conv_name = fi.getVideoTitle();
120 if (profile.getOutputFileSetting().isAppendPrefixVideoId()) {
121 conv_name = getVideoIDWithBracket() + conv_name;
123 convertedVideoFile = new File(profile.getOutputFileSetting().getFile().getFile(),
124 conv_name + ov.getExtOption());
126 String filename = profile.getOutputFileSetting().getFile().getFile().getPath();
127 if (!filename.endsWith(ov.getExtOption())) {
128 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
129 convertedVideoFile = new File(filename);
131 convertedVideoFile = profile.getOutputFileSetting().getFile().getFile();
135 boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
136 convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
140 private FileInstanciator createInstanciator() throws IOException {
143 FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(profile.getVideoSetting());
145 FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(profile.
146 getCommentSetting(), profile.getCommentSetting().isSelfAdjustCommentNum(), profile.getCommentSetting().
147 getBackComment(), profile.getCommentSetting().isReduceComment());
149 FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
150 profile.getCommentSetting());
152 fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, profile.getLoginInfo(), profile.
153 getProxySetting(), movieId, profile.getCommentSetting().getBackLogPoint());
158 * (ネットワーク設定以外の)設定を検証する.
159 * @throws IllegalArgumentException 設定に不備がある場合.
161 private void validSetting() {
162 if (profile.getOutputFileSetting().isConvert()) {
163 File a = profile.getFfmpeg().getFfmpeg();
165 throw new IllegalArgumentException("FFmpegが見つかりません。");
167 if (profile.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
168 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
170 a = profile.getFfmpeg().getVhook();
172 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
174 a = profile.getFfmpeg().getFont();
176 throw new IllegalArgumentException("フォントが見つかりません。");
181 private String getVideoIDWithBracket() {
182 return "[" + movieId + "]";
185 public boolean isConverted() {
186 return getStopFlag().isFinished();
190 public ConvertStopFlag getStopFlag() {
191 return super.getStopFlag();
194 /** @return 何か実行すべき処理があればtrue. */
195 private static boolean shouldRun(Profile profile) {
196 return profile.getOutputFileSetting().isConvert() || needsDownload(profile);
199 /** @return 何かダウンロードするものがあればtrue. */
200 private static boolean needsDownload(Profile profile) {
201 return (profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload());