X-Git-Url: http://git.sourceforge.jp/view?p=coroid%2Finqubus.git;a=blobdiff_plain;f=frontend%2Fsrc%2Fsaccubus%2Fconverter%2FConverter.java;h=e78ac1cd7e33cf171a68c12513b635ca67a53862;hp=499221a421b5220c186d9dba0c1d8740bd296ebb;hb=e570086dbeb579551393d7373009f98d3f4266ab;hpb=2514ecfc2bc1464d42117c8e5804bd778bb29656 diff --git a/frontend/src/saccubus/converter/Converter.java b/frontend/src/saccubus/converter/Converter.java index 499221a..e78ac1c 100644 --- a/frontend/src/saccubus/converter/Converter.java +++ b/frontend/src/saccubus/converter/Converter.java @@ -1,358 +1,215 @@ 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; /** - *

ƒ^ƒCƒgƒ‹: ‚³‚«‚ã‚΂·

+ *

タイトル: さきゅばす

* - *

à–¾: ƒjƒRƒjƒR“®‰æ‚Ì“®‰æ‚ðƒRƒƒ“ƒg‚‚«‚Å•Û‘¶

+ *

説明: ニコニコ動画の動画をコメントつきで保存

* - *

’˜ìŒ : Copyright (c) 2007 PSI

+ *

著作権: Copyright (c) 2007 PSI

* - *

‰ïŽÐ–¼:

+ *

会社名:

* - * @author –¢“ü—Í + * @author 未入力 * @version 1.0 */ -public class Converter extends Thread { +public class Converter extends AbstractCommand implements Runnable, Callable { 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("‰½‚à‚·‚邱‚Æ‚ª‚ ‚è‚Ü‚¹‚ñ"); - 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("ƒƒOƒCƒ“’†"); + 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("“®‰æEƒRƒƒ“ƒg‚ð•Û‘¶‚µA•ÏŠ·‚͍s‚¢‚Ü‚¹‚ñ‚Å‚µ‚½B"); - return; + if (!profile.needsConvert()) { + sendText("動画・コメントを保存し、変換は行いませんでした。"); + return true; } - if (Setting.getAddComment()) { - sendText("ƒRƒƒ“ƒg‚Ì’†ŠÔƒtƒ@ƒCƒ‹‚Ö‚Ì•ÏŠ·’†"); + if (!videoFile.isFile()) { + throw new IOException("入力動画ファイルが存在しません:" + videoFile.getPath()); + } - boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, - Setting.getNgId(), Setting.getNgWord()); - if (!conv) { - sendText("ƒRƒƒ“ƒg•ÏŠ·‚ÉŽ¸”sB‚¨‚»‚ç‚­³‹K•\Œ»‚̊ԈႢH"); - return; + if (profile.getOutputFileSetting().isAddComment()) { + if (!commentFile.isFile()) { + throw new IOException("入力コメントファイルが存在しません:" + commentFile.getPath()); } + } else { + commentFile = null; } - stopFlagReturn(); - - if (Setting.getAddTcomment()) { - sendText("“ŠeŽÒƒRƒƒ“ƒg‚Ì’†ŠÔƒtƒ@ƒCƒ‹‚Ö‚Ì•ÏŠ·’†"); - - boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, - Setting.getNgId(), Setting.getNgWord()); - if (!conv) { - sendText("ƒRƒƒ“ƒg•ÏŠ·‚ÉŽ¸”sB‚¨‚»‚ç‚­³‹K•\Œ»‚̊ԈႢH"); - return; + if (profile.getOutputFileSetting().isAddTcomment()) { + if (!tcommFile.isFile()) { + throw new IOException("入力投稿者コメントファイルが存在しません" + tcommFile.getPath()); } + } else { + tcommFile = null; } - stopFlagReturn(); - - sendText("“®‰æ‚Ì•ÏŠ·‚ðŠJŽn"); - /*ƒrƒfƒI–¼‚ÌŠm’è*/ + /*ビデオ名の確定*/ File convertedVideoFile; - if (!Setting.getConvertFile().isFile()) { - if (fi.getVideoTitle() == null) { - sendText("•ÏŠ·Œã‚̃rƒfƒIƒtƒ@ƒCƒ‹–¼‚ªŠm’è‚Å‚«‚Ü‚¹‚ñB"); - 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("•ÏŠ·‚ª³í‚ɏI—¹‚µ‚Ü‚µ‚½B"); - if (Setting.getCommentSetting().isDelete()) { - commentFile.delete(); - } - if (Setting.getVideoSetting().isDelete()) { - videoFile.delete(); - } - if (Setting.getTcommentSetting().isDelete()) { - tcommFile.delete(); - } - } else { - sendText("•ÏŠ·ƒGƒ‰[:" + 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; } /** - * (ƒlƒbƒgƒ[ƒNÝ’èˆÈŠO‚Ì)Ý’è‚ðŒŸØ‚·‚é. - * @throws IllegalArgumentException Ý’è‚É•s”õ‚ª‚ ‚éê‡. + * (ネットワーク設定以外の)設定を検証する. + * @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‚ªŒ©‚‚©‚è‚Ü‚¹‚ñB"); + throw new IllegalArgumentException("FFmpegが見つかりません。"); } - if (Setting.getVhookPath().indexOf(' ') >= 0) { - throw new IllegalArgumentException("‚·‚¢‚Ü‚¹‚ñBŒ»Ývhookƒ‰ƒCƒuƒ‰ƒŠ‚É‚Í”¼Šp‹ó”’‚ÍŽg‚¦‚Ü‚¹‚ñB"); + 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ƒ‰ƒCƒuƒ‰ƒŠ‚ªŒ©‚‚©‚è‚Ü‚¹‚ñB"); + throw new IllegalArgumentException("Vhookライブラリが見つかりません。"); } - a = new File(Setting.getFontPath()); + a = profile.getFfmpeg().getFont(); if (!a.canRead()) { - throw new IllegalArgumentException("ƒtƒHƒ“ƒg‚ªŒ©‚‚©‚è‚Ü‚¹‚ñB"); - } - } else { - if (Setting.getVideoSetting().isDelete()) { - throw new IllegalArgumentException("•ÏŠ·‚µ‚È‚¢‚̂ɁA“®‰æíœ‚µ‚¿‚á‚Á‚Ä—Ç‚¢‚ñ‚Å‚·‚©H"); - } - if (Setting.getCommentSetting().isDelete()) { - throw new IllegalArgumentException("•ÏŠ·‚µ‚È‚¢‚̂ɁAƒRƒƒ“ƒgíœ‚µ‚¿‚á‚Á‚Ä—Ç‚¢‚ñ‚Å‚·‚©H"); - } - if (Setting.getTcommentSetting().isDelete()) { - throw new IllegalArgumentException("•ÏŠ·‚µ‚È‚¢‚̂ɁA“ŠeŽÒƒRƒƒ“ƒgíœ‚µ‚¿‚á‚Á‚Ä—Ç‚¢‚ñ‚Å‚·‚©H"); - } - } - } - - 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("’†Ž~‚µ‚Ü‚µ‚½B"); - } + @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()); } }