package saccubus.converter;
-import saccubus.converter.filegetter.LoginInfo;
+import yukihane.saccubus.converter.profile.Profile;
import saccubus.converter.filegetter.FileInstanciator;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.Properties;
+import java.util.concurrent.Callable;
import saccubus.ConvertStopFlag;
-import saccubus.conv.ConvertToVideoHook;
+import yukihane.saccubus.converter.profile.FfmpegOption;
import saccubus.net.TextProgressListener;
-import yukihane.swf.Cws2Fws;
/**
- * <p>\83^\83C\83g\83\8b: \82³\82«\82ã\82Î\82·</p>
+ * <p>タイトル: さきゅばす</p>
*
- * <p>\90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91¶</p>
+ * <p>説明: ニコニコ動画の動画をコメントつきで保存</p>
*
- * <p>\92\98\8dì\8c : Copyright (c) 2007 PSI</p>
+ * <p>著作権: Copyright (c) 2007 PSI</p>
*
- * <p>\89ï\8eÐ\96¼: </p>
+ * <p>会社名: </p>
*
- * @author \96¢\93ü\97Í
+ * @author 未入力
* @version 1.0
*/
-public class Converter extends Thread {
+public class Converter extends AbstractCommand implements Runnable, Callable<Boolean> {
private static final String VIDEO_URL_PARSER = "http://www.nicovideo.jp/watch/";
- private static final String TMP_CWS = "fws_tmp.swf";
- private final Profile Setting;
- private final String Tag;
- private final String Time;
- private final TextProgressListener listener;
- private final ConvertStopFlag StopFlag;
- private final File commentMiddleFile = new File("./vhook.tmp");
- private final File tcommMiddleFile = new File("./tcomment.tmp");
-
- public Converter(String url, String time, Profile setting,
+ private final Profile profile;
+ private final String movieId;
+ private final String time;
+
+ /**
+ * コンバータを構築します.
+ * @param url 対象となる動画のURL.
+ * @param time
+ * @param profile
+ * @param listener
+ * @param flag
+ */
+ public Converter(String url, String time, Profile profile,
TextProgressListener listener, ConvertStopFlag flag) {
+ super(listener, flag);
url = url.trim();
if (url.startsWith(VIDEO_URL_PARSER)) {
int index = url.indexOf('?', VIDEO_URL_PARSER.length());
if (index >= 0) {
- Tag = url.substring(VIDEO_URL_PARSER.length(), index);
+ movieId = url.substring(VIDEO_URL_PARSER.length(), index);
} else {
- Tag = url.substring(VIDEO_URL_PARSER.length());
+ movieId = url.substring(VIDEO_URL_PARSER.length());
}
} else {
- Tag = url;
+ movieId = url;
}
- Time = time;
- Setting = setting;
- this.listener = listener;
- StopFlag = flag;
+ this.time = time;
+ this.profile = profile;
+ }
+
+ public Boolean call() throws Exception {
+ boolean result = false;
+ try {
+ result = runConvert();
+ } finally {
+ getStopFlag().finished();
+ }
+ return Boolean.valueOf(result);
}
- @Override
public void run() {
try {
- runConvert();
+ call();
} catch (Exception ex) {
- sendText(ex.getMessage());
+ String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
+ sendText(text);
ex.printStackTrace();
- } finally {
- if (commentMiddleFile.exists()) {
- commentMiddleFile.delete();
- }
- if (tcommMiddleFile.exists()) {
- tcommMiddleFile.delete();
- }
- StopFlag.finished();
}
}
- private void runConvert() throws IOException, InterruptedException {
- if (!Setting.shouldRun()) {
- sendText("\89½\82à\82·\82é\82±\82Æ\82ª\82 \82è\82Ü\82¹\82ñ");
- return;
+ private boolean runConvert() throws IOException, InterruptedException {
+ if (!shouldRun(profile)) {
+ sendText("何もすることがありません");
+ return true;
}
validSetting();
- final FfmpegOption ov = Setting.getFfmpegOption();
+ final FfmpegOption ov = profile.getFfmpeg().getFfmpegOption();
- sendText("\83\8d\83O\83C\83\93\92\86");
+ sendText("ログイン中");
final FileInstanciator fi = createInstanciator();
stopFlagReturn();
- final File videoFile = fi.getVideoFile(listener);
+ final File videoFile = fi.getVideoFile(getListener());
stopFlagReturn();
- final File commentFile = fi.getCommentFile(listener);
+ File commentFile = fi.getCommentFile(getListener());
stopFlagReturn();
- final File tcommFile = fi.getTcommFile(listener);
+ File tcommFile = fi.getTcommFile(getListener());
- if (!Setting.needsConvert()) {
- sendText("\93®\89æ\81E\83R\83\81\83\93\83g\82ð\95Û\91¶\82µ\81A\95Ï\8a·\82Í\8ds\82¢\82Ü\82¹\82ñ\82Å\82µ\82½\81B");
- return;
+ if (!profile.needsConvert()) {
+ sendText("動画・コメントを保存し、変換は行いませんでした。");
+ return true;
}
- if (Setting.getAddComment()) {
- sendText("\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
+ if (!videoFile.isFile()) {
+ throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath());
+ }
- boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile,
- Setting.getNgId(), Setting.getNgWord());
- if (!conv) {
- sendText("\83R\83\81\83\93\83g\95Ï\8a·\82É\8e¸\94s\81B\82¨\82»\82ç\82\90³\8bK\95\\8c»\82Ì\8aÔ\88á\82¢\81H");
- return;
+ if (profile.getOutputFileSetting().isAddComment()) {
+ if (!commentFile.isFile()) {
+ throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath());
}
+ } else {
+ commentFile = null;
}
- stopFlagReturn();
-
- if (Setting.getAddTcomment()) {
- sendText("\93\8a\8de\8eÒ\83R\83\81\83\93\83g\82Ì\92\86\8aÔ\83t\83@\83C\83\8b\82Ö\82Ì\95Ï\8a·\92\86");
-
- boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile,
- Setting.getNgId(), Setting.getNgWord());
- if (!conv) {
- sendText("\83R\83\81\83\93\83g\95Ï\8a·\82É\8e¸\94s\81B\82¨\82»\82ç\82\90³\8bK\95\\8c»\82Ì\8aÔ\88á\82¢\81H");
- return;
+ if (profile.getOutputFileSetting().isAddTcomment()) {
+ if (!tcommFile.isFile()) {
+ throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath());
}
+ } else {
+ tcommFile = null;
}
- stopFlagReturn();
-
- sendText("\93®\89æ\82Ì\95Ï\8a·\82ð\8aJ\8en");
- /*\83r\83f\83I\96¼\82Ì\8am\92è*/
+ /*ビデオ名の確定*/
File convertedVideoFile;
- if (!Setting.getConvertFile().isFile()) {
- if (fi.getVideoTitle() == null) {
- sendText("\95Ï\8a·\8cã\82Ì\83r\83f\83I\83t\83@\83C\83\8b\96¼\82ª\8am\92è\82Å\82«\82Ü\82¹\82ñ\81B");
- return;
- }
+ if (!profile.getOutputFileSetting().getFile().isFile()) {
String conv_name = fi.getVideoTitle();
- if (Setting.needsAppendPrefixVideoId()) {
+ if (profile.getOutputFileSetting().isAppendPrefixVideoId()) {
conv_name = getVideoIDWithBracket() + conv_name;
}
- convertedVideoFile = new File(Setting.getConvertFile().getFile(), conv_name + ov.getExtOption());
+ convertedVideoFile = new File(profile.getOutputFileSetting().getFile().getFile(),
+ conv_name + ov.getExtOption());
} else {
- String filename = Setting.getConvertFile().getFile().getPath();
+ String filename = profile.getOutputFileSetting().getFile().getFile().getPath();
if (!filename.endsWith(ov.getExtOption())) {
- filename = filename.substring(0, filename.lastIndexOf('.'))
- + ov.getExtOption();
+ filename = filename.substring(0, filename.lastIndexOf('.')) + ov.getExtOption();
convertedVideoFile = new File(filename);
} else {
- convertedVideoFile = Setting.getConvertFile().getFile();
+ convertedVideoFile = profile.getOutputFileSetting().getFile().getFile();
}
}
- int code;
- if ((code = converting_video(videoFile, convertedVideoFile, Setting.getAddComment(), commentMiddleFile.getPath(),
- Setting.getAddTcomment(), tcommMiddleFile.getPath(), ov)) == 0) {
- sendText("\95Ï\8a·\82ª\90³\8fí\82É\8fI\97¹\82µ\82Ü\82µ\82½\81B");
- if (Setting.getCommentSetting().isDelete()) {
- commentFile.delete();
- }
- if (Setting.getVideoSetting().isDelete()) {
- videoFile.delete();
- }
- if (Setting.getTcommentSetting().isDelete()) {
- tcommFile.delete();
- }
- } else {
- sendText("\95Ï\8a·\83G\83\89\81[:" + convertedVideoFile.getPath());
- }
+
+ boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, tcommFile, videoFile,
+ convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
+ return res;
}
private FileInstanciator createInstanciator() throws IOException {
FileInstanciator fi;
- LoginInfo li = new LoginInfo(Setting.getMailAddress(), Setting.getPassword(), Setting.getProxy());
- FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(Setting.getVideoSetting());
+ FileInstanciator.InstanciationType videoType = new FileInstanciator.InstanciationType(profile.getVideoSetting());
- FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(Setting.
- getCommentSetting(), Setting.isselfAdjustCommentNum(), Setting.getBackComment());
+ FileInstanciator.CommentInstanciationType commentType = new FileInstanciator.CommentInstanciationType(profile.
+ getCommentSetting(), profile.getCommentGetInfo().isselfAdjustCommentNum(), profile.getCommentGetInfo().
+ getBackComment());
FileInstanciator.InstanciationType tcommType = new FileInstanciator.InstanciationType(
- Setting.getTcommentSetting());
+ profile.getTcommentSetting());
- fi = FileInstanciator.create(StopFlag, videoType, commentType, tcommType, li, Tag, Time);
+ fi = FileInstanciator.create(getStopFlag(), videoType, commentType, tcommType, profile.getLoginInfo(), profile.
+ getProxySetting(), movieId, time);
return fi;
}
/**
- * (\83l\83b\83g\83\8f\81[\83N\90Ý\92è\88È\8aO\82Ì)\90Ý\92è\82ð\8c\9f\8fØ\82·\82é.
- * @throws IllegalArgumentException \90Ý\92è\82É\95s\94õ\82ª\82 \82é\8fê\8d\87.
+ * (ネットワーク設定以外の)設定を検証する.
+ * @throws IllegalArgumentException 設定に不備がある場合.
*/
private void validSetting() {
- if (Setting.needsConvert()) {
- File a = new File(Setting.getFfmpegPath());
+ if (profile.needsConvert()) {
+ File a = profile.getFfmpeg().getFfmpeg();
if (!a.canRead()) {
- throw new IllegalArgumentException("FFmpeg\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
+ throw new IllegalArgumentException("FFmpegが見つかりません。");
}
- if (Setting.getVhookPath().indexOf(' ') >= 0) {
- throw new IllegalArgumentException("\82·\82¢\82Ü\82¹\82ñ\81B\8c»\8dÝvhook\83\89\83C\83u\83\89\83\8a\82É\82Í\94¼\8ap\8bó\94\92\82Í\8eg\82¦\82Ü\82¹\82ñ\81B");
+ if (profile.getFfmpeg().getVhook().getPath().indexOf(' ') >= 0) {
+ throw new IllegalArgumentException("すいません。現在vhookライブラリには半角空白は使えません。");
}
- a = new File(Setting.getVhookPath());
+ a = profile.getFfmpeg().getVhook();
if (!a.canRead()) {
- throw new IllegalArgumentException("Vhook\83\89\83C\83u\83\89\83\8a\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
+ throw new IllegalArgumentException("Vhookライブラリが見つかりません。");
}
- a = new File(Setting.getFontPath());
+ a = profile.getFfmpeg().getFont();
if (!a.canRead()) {
- throw new IllegalArgumentException("\83t\83H\83\93\83g\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ\81B");
- }
- } else {
- if (Setting.getVideoSetting().isDelete()) {
- throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93®\89æ\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
- }
- if (Setting.getCommentSetting().isDelete()) {
- throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
- }
- if (Setting.getTcommentSetting().isDelete()) {
- throw new IllegalArgumentException("\95Ï\8a·\82µ\82È\82¢\82Ì\82É\81A\93\8a\8de\8eÒ\83R\83\81\83\93\83g\8dí\8f\9c\82µ\82¿\82á\82Á\82Ä\97Ç\82¢\82ñ\82Å\82·\82©\81H");
- }
- }
- }
-
- private void sendText(String text) {
- listener.setText(text);
- }
-
- private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String vhook_path,
- boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
- File fwsFile = Cws2Fws.createFws(videoFile, new File(TMP_CWS));
-
- StringBuffer sb = new StringBuffer();
- sb.append("\"");
- sb.append(Setting.getFfmpegPath().replace("\\", "\\\\"));
- sb.append("\"");
- sb.append(" -y ");
- sb.append(ov.getMainOption());
- sb.append(" ");
- sb.append(ov.getInOption());
- sb.append(" -i ");
- if (fwsFile == null) {
- sb.append("\"");
- sb.append(videoFile.getPath().replace("\\", "\\\\"));
- sb.append("\"");
- } else {
- sb.append(fwsFile.getPath().replace("\\", "\\\\"));
- }
- sb.append(" ");
- sb.append(ov.getOutOption());
- sb.append(" \"");
- sb.append(convertedVideoFile.getPath().replace("\\", "\\\\"));
- sb.append("\"");
- if (!Setting.isVhookDisabled()) {
- if (!addVhookSetting(sb, addComment, vhook_path, addTcomment, tcommPath)) {
- return -1;
- }
- }
- String cmd = sb.substring(0);
- System.out.println("arg:" + cmd);
- try {
- System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
- Process process = Runtime.getRuntime().exec(cmd);
- BufferedReader ebr = new BufferedReader(new InputStreamReader(
- process.getErrorStream()));
- String e;
- while ((e = ebr.readLine()) != null) {
- String state = e;
- if (state.startsWith("frame=")) {
- sendText(state);
- } else if (!state.endsWith("No accelerated colorspace conversion found")) {
- System.out.println(e);
- }
-
- try {
- stopFlagReturn();
- } catch (InterruptedException ex) {
- process.destroy();
- throw ex;
- }
-
- }
- process.waitFor();
- return process.exitValue();
- } finally {
- if (fwsFile != null) {
- fwsFile.delete();
+ throw new IllegalArgumentException("フォントが見つかりません。");
}
}
}
- private boolean addVhookSetting(StringBuffer sb, boolean addComment, String vhook_path, boolean addTcomment,
- String tcommPath) {
- try {
- sb.append(" -vfilters \"vhext=");
- sb.append(Setting.getVhookPath().replace("\\", "/"));
- if (addComment) {
- sb.append("|");
- sb.append("--data-user:");
- sb.append(URLEncoder.encode(vhook_path.replace("\\", "/"), "Shift_JIS"));
- }
- if (addTcomment) {
- sb.append("|");
- sb.append("--data-owner:");
- sb.append(URLEncoder.encode(tcommPath.replace("\\", "/"), "Shift_JIS"));
- }
- sb.append("|");
- sb.append("--font:");
- sb.append(URLEncoder.encode(
- Setting.getFontPath().replace("\\", "/"), "Shift_JIS"));
- sb.append("|");
- sb.append("--font-index:");
- sb.append(Setting.getFontIndex());
- sb.append("|");
- sb.append("--show-user:");
- sb.append(Setting.getVideoShowNum());
- sb.append("|");
- sb.append("--shadow:");
- sb.append(Setting.getShadowIndex());
- sb.append("|");
- if (Setting.isShowConvertingVideo()) {
- sb.append("--enable-show-video");
- sb.append("|");
- }
- if (Setting.isSelfAdjustFontSize()) {
- sb.append("--enable-fix-font-size");
- sb.append("|");
- }
- if (Setting.isOpaqueComment()) {
- sb.append("--enable-opaque-comment");
- }
- sb.append("\"");
- return true;
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return false;
- }
+ private String getVideoIDWithBracket() {
+ return "[" + movieId + "]";
}
public boolean isConverted() {
- return StopFlag.isFinished();
+ return getStopFlag().isFinished();
}
- private void stopFlagReturn() throws InterruptedException {
- if (StopFlag.needStop()) {
- throw new InterruptedException("\92\86\8e~\82µ\82Ü\82µ\82½\81B");
- }
+ @Override
+ public ConvertStopFlag getStopFlag() {
+ return super.getStopFlag();
}
- public ConvertStopFlag getStopFlag() {
- return this.StopFlag;
+ /** @return 何か実行すべき処理があればtrue. */
+ private static boolean shouldRun(Profile profile) {
+ return profile.getOutputFileSetting().isConvert() || needsDownload(profile);
}
- private String getVideoIDWithBracket() {
- return "[" + Tag + "]";
+ /** @return 何かダウンロードするものがあればtrue. */
+ private static boolean needsDownload(Profile profile) {
+ return (profile.getVideoSetting().isDownload() || profile.getCommentSetting().isDownload() || profile.
+ getTcommentSetting().isDownload());
}
}