1 package saccubus.converter;
4 import java.io.IOException;
5 import java.net.URISyntaxException;
6 import java.util.concurrent.Callable;
7 import java.util.logging.Logger;
8 import javax.swing.SwingWorker;
9 import nicobrowser.GetFlvResult;
10 import nicobrowser.NamePattern;
11 import nicobrowser.NicoHttpClient;
12 import nicobrowser.ProgressListener;
13 import nicobrowser.WayBackInfo;
14 import nicobrowser.entity.NicoContent.Status;
15 import org.apache.http.HttpException;
16 import saccubus.converter.profile.CommentProfile;
17 import saccubus.converter.profile.FfmpegProfile;
18 import saccubus.converter.profile.GeneralProfile;
19 import saccubus.converter.profile.OutputProfile;
20 import saccubus.converter.profile.Profile;
21 import saccubus.converter.profile.ProxyProfile;
26 * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
28 * <p>著作権: Copyright (c) 2007 PSI</p>
35 public class Converter extends SwingWorker<ConvertResult, ConvertProgress> implements Callable<Boolean> {
37 private static final Logger logger = Logger.getLogger(Converter.class.getName());
38 private final Profile profile;
39 private final String movieId;
43 * @param id 対象となる動画のID.
49 public Converter(String id, Profile profile) {
51 this.profile = profile;
55 public Boolean call() throws Exception {
57 final ConvertResult result = doInBackground();
58 return Boolean.valueOf(result.getResultValue());
61 // getStopFlag().finished();
65 // // TODO Runnableを実装しなくなったので削除する
66 // public void run() {
69 // } catch (Exception ex) {
70 // String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
72 // logger.log(Level.SEVERE, null, ex);
76 protected ConvertResult doInBackground() throws Exception {
77 if (!shouldRun(profile)) {
78 publish(new ConvertProgress("何もすることがありません"));
79 return new ConvertResult(true);
83 final FfmpegProfile ov = profile.getFfmpeg().getFfmpegOption();
85 // TODO ログインしないで良い場合もある.
86 publish(new ConvertProgress("ログイン中"));
90 NicoHttpClient client = null;
91 nicobrowser.VideoInfo vi = null;
92 NamePattern videoNamePattern = null;
93 WayBackInfo wbi = null;
95 client = createClientAndLogin();
96 vi = client.getVideoInfo(movieId);
98 final String name = profile.getVideoSetting().getFileName();
99 final String replaceFrom = profile.getGeneralSetting().getReplaceFrom();
100 final String replaceTo = profile.getGeneralSetting().getReplaceTo();
101 videoNamePattern = new NamePattern(name, replaceFrom, replaceTo, vi.getTitleInWatchPage());
103 if (needsBackLog()) {
104 final String key = client.getWayBackKey(vi);
105 wbi = new WayBackInfo(key, profile.getCommentSetting().getBackLogPoint());
112 if (profile.getCommentSetting().isDownload()) {
113 final CommentProfile prof = profile.getCommentSetting();
114 final GeneralProfile gene = profile.getGeneralSetting();
116 final NamePattern pattern = new NamePattern(prof.getFileName(), gene.getReplaceFrom(), gene.getReplaceTo(),
117 vi.getTitleInWatchPage());
118 // TODO コメントファイルに{low}は使えないことをどこかに書くべきか
119 final String name = pattern.createFileName(movieId, true);
120 final File file = new File(profile.getCommentSetting().getDir(), name);
122 commentFile = client.getCommentFile(vi, file.getPath(), wbi, profile.getCommentSetting().
123 getLengthRelatedCommentSize(),
124 profile.getCommentSetting().isDisablePerMinComment());
126 commentFile = profile.getCommentSetting().getLocalFile();
132 GetFlvResult vf = null;
133 if (profile.getVideoSetting().isDownload()) {
134 vf = client.getFlvFile(vi, profile.getVideoSetting().getDir(), videoNamePattern,
135 Status.GET_INFO, true, new ProgressListener() {
138 public void progress(long fileSize, long downloadSize) {
139 throw new UnsupportedOperationException("Not supported yet.");
143 public boolean getCancel() {
144 throw new UnsupportedOperationException("Not supported yet.");
148 videoFile = vf.getFile();
150 videoFile = profile.getVideoSetting().getLocalFile();
153 if (!profile.getOutputFileSetting().isConvert()) {
154 publish(new ConvertProgress("動画・コメントを保存し、変換は行いませんでした。"));
155 return new ConvertResult(true);
158 if (!videoFile.isFile()) {
159 throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
162 if (profile.getOutputFileSetting().isAddComment()) {
163 if (!commentFile.isFile()) {
164 throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
171 final boolean isNotLow = (vf == null) ? true : (vf.getStatus() != Status.GET_LOW);
172 File convertedVideoFile = getOutputFileName(vi.getTitleInWatchPage(), isNotLow);
174 return new ConvertResult(true);
176 // TODO FFMPEG 実行開始は別タスクとして実装する.
177 // boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
178 // convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
182 /** @return 何か実行すべき処理があればtrue. */
183 private static boolean shouldRun(Profile profile) {
184 return profile.getOutputFileSetting().isConvert() || needsDownload(profile);
187 /** @return 何かダウンロードするものがあればtrue. */
188 private static boolean needsDownload(Profile profile) {
189 return (profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload());
193 * (ネットワーク設定以外の)設定を検証する.
194 * @throws IllegalArgumentException 設定に不備がある場合.
196 private void validSetting() {
197 if (profile.getOutputFileSetting().isConvert()) {
198 File a = profile.getFfmpeg().getFfmpeg();
200 throw new IllegalArgumentException("FFmpegが見つかりません。");
202 if (profile.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
203 throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
205 a = profile.getFfmpeg().getVhook();
207 throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
209 a = profile.getFfmpeg().getFont();
211 throw new IllegalArgumentException("フォントが見つかりません。");
217 * HttpClientを生成し, ニコニコ動画サーバへログインします.
218 * @return 生成したHttpClientインスタンス.
219 * @throws IOException ログイン失敗.
220 * @throws InterruptedException ログイン失敗.
222 // TODO HttpException を投げるのをやめたい. コンパイル時にHttpComponentが必要になるので.
223 private NicoHttpClient createClientAndLogin() throws IOException, InterruptedException, HttpException {
224 final NicoHttpClient client = createClient(profile.getProxySetting());
225 final boolean hasLogin;
227 hasLogin = client.login(profile.getLoginInfo().getMail(), profile.getLoginInfo().getPassword());
229 throw new IOException("login fail");
231 } catch (URISyntaxException ex) {
232 throw new IOException("login fail", ex);
237 private NicoHttpClient createClient(ProxyProfile proxy) {
239 return new NicoHttpClient(proxy.getHost(), proxy.getPort());
241 return new NicoHttpClient();
245 /** @return ログインする必要があればtrue. */
246 private boolean needsLogin() {
247 return profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload();
250 /** @return 過去ログ取得の必要があればtrue. */
251 private boolean needsBackLog() {
252 return profile.getCommentSetting().getBackLogPoint() >= 0L;
257 * @param title 動画タイトル.
258 * @param isNotLow 入力動画ファイルがlowでないかどうか.
259 * @return 出力ファイルの保存先.
261 private File getOutputFileName(String title, boolean isNotLow) {
262 final OutputProfile prof = profile.getOutputFileSetting();
263 final GeneralProfile gene = profile.getGeneralSetting();
264 final File dir = prof.getDir();
265 final String replaceFrom = gene.getReplaceFrom();
266 final String replaceTo = gene.getReplaceTo();
267 final NamePattern pattern = new NamePattern(prof.getFileName(), replaceFrom, replaceTo, title);
268 final String name = pattern.createFileName(movieId, isNotLow);
269 return new File(dir, name);
272 private void checkStop() throws InterruptedException {
273 if (Thread.interrupted()) {
274 throw new InterruptedException("中止要求を受け付けました");