1 package saccubus.converter;
3 import java.util.regex.Matcher;
4 import saccubus.converter.profile.Profile;
5 import saccubus.converter.profile.FfmpegOption;
6 import saccubus.converter.filegetter.FileInstanciator;
8 import java.io.IOException;
9 import java.util.concurrent.Callable;
10 import java.util.regex.Pattern;
11 import saccubus.ConvertStopFlag;
12 import saccubus.net.TextProgressListener;
17 * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
19 * <p>著作権: Copyright (c) 2007 PSI</p>
26 public class Converter extends AbstractCommand implements Runnable, Callable<Boolean> {
28 private final Profile Setting;
29 private final String Tag;
30 private final String Time;
32 public Converter(String url, String time, Profile setting,
33 TextProgressListener listener, ConvertStopFlag flag) {
34 super(listener, flag);
36 // TODO 入力欄の値から動画IDの切り出しはGUI側でやるべきだろう
37 final int startIdIdx = url.lastIndexOf("/") + 1;
38 final String altId = url.substring(startIdIdx);
39 final Pattern idPattern = Pattern.compile("([a-z]*\\d+)");
40 final Matcher idMatcher = idPattern.matcher(altId);
41 if (idMatcher.find()) {
42 Tag = idMatcher.group(1);
44 throw new IllegalArgumentException("URL/IDの指定が不正です: " + url);
51 public Boolean call() throws Exception {
52 boolean result = false;
54 result = runConvert();
56 getStopFlag().finished();
58 return Boolean.valueOf(result);
64 } catch (Exception ex) {
65 String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
71 private boolean runConvert() throws IOException, InterruptedException {
72 if (!Setting.shouldRun()) {
73 sendText("何もすることがありません");
78 final FfmpegOption ov = Setting.getFfmpeg().getFfmpegOption();
82 final FileInstanciator fi = createInstanciator();
86 final File videoFile = fi.getVideoFile(getListener());
90 File commentFile = fi.getCommentFile(getListener());
94 File tcommFile = fi.getTcommFile(getListener());
96 if (!Setting.needsConvert()) {
97 sendText("動画・コメントを保存し、変換は行いませんでした。");
101 if (!videoFile.isFile()) {
102 throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
105 if (Setting.getOutputFileSetting().isAddComment()) {
106 if (!commentFile.isFile()) {
107 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
113 if (Setting.getOutputFileSetting().isAddTcomment()) {
114 if (!tcommFile.isFile()) {
115 throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
122 File convertedVideoFile;
123 if (!Setting.getOutputFileSetting().getFile().isFile()) {
124 String conv_name = fi.getVideoTitle();
125 if (Setting.getOutputFileSetting().isAppendPrefixVideoId()) {
126 conv_name = getVideoIDWithBracket() + conv_name;
128 convertedVideoFile = new File(Setting.getOutputFileSetting().getFile().getFile(),
129 conv_name + ov.getExtOption());
131 String filename = Setting.getOutputFileSetting().getFile().getFile().getPath();
132 if (!filename.endsWith(ov.getExtOption())) {
133 filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
134 convertedVideoFile = new File(filename);
136 convertedVideoFile = Setting.getOutputFileSetting().getFile().getFile();
140 boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
141 convertedVideoFile, Setting.getFfmpeg(), Setting.getGeneralSetting()).execute();
143 if (Setting.getCommentSetting().isDelete()) {
144 commentFile.delete();
146 if (Setting.getVideoSetting().isDelete()) {
149 if (Setting.getTcommentSetting().isDelete()) {
156 private FileInstanciator createInstanciator() throws IOException {
159 FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
161 FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
162 getCommentSetting(), Setting.getCommentGetInfo().isSelfAdjustCommentNum(), Setting.getCommentGetInfo().
163 getBackComment(), Setting.getCommentGetInfo().isReduceComment());
165 FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
166 Setting.getTcommentSetting());
168 fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, Setting.getLoginInfo(), Tag, Time);
173 * (ネットワーク設定以外の)設定を検証する.
174 * @throws IllegalArgumentException 設定に不備がある場合.
176 private void validSetting() {
177 if (Setting.needsConvert()) {
178 File a = Setting.getFfmpeg().getFfmpeg();
180 throw new IllegalArgumentException("FFmpegが見つかりません。");
182 if (Setting.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
183 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
185 a = Setting.getFfmpeg().getVhook();
187 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
189 a = Setting.getFfmpeg().getFont();
191 throw new IllegalArgumentException("フォントが見つかりません。");
194 if (Setting.getVideoSetting().isDelete()) {
195 throw new IllegalArgumentException("変換しないのに、動画削除しちゃって良いんですか?");
197 if (Setting.getCommentSetting().isDelete()) {
198 throw new IllegalArgumentException("変換しないのに、コメント削除しちゃって良いんですか?");
200 if (Setting.getTcommentSetting().isDelete()) {
201 throw new IllegalArgumentException("変換しないのに、投稿者コメント削除しちゃって良いんですか?");
206 private String getVideoIDWithBracket() {
207 return "[" + Tag + "]";
210 public boolean isConverted() {
211 return getStopFlag().isFinished();
215 public ConvertStopFlag getStopFlag() {
216 return super.getStopFlag();