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;
/**
- *
^Cg: ³«ãη
+ * ã¿ã¤ãã«: ããã
ã°ã
*
- * à¾: jRjR®æÌ®æðRg«ÅÛ¶
+ * 説æ: ãã³ãã³åç»ã®åç»ãã³ã¡ã³ãã¤ãã§ä¿å
*
- * ì : 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("OC");
+ 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("®æERgðÛ¶µAÏ·Ís¢Ü¹ñŵ½B");
- return;
+ if (!profile.needsConvert()) {
+ sendText("åç»ã»ã³ã¡ã³ããä¿åããå¤æã¯è¡ãã¾ããã§ããã");
+ return true;
}
- if (Setting.getAddComment()) {
- sendText("RgÌÔt@CÖÌÏ·");
+ if (!videoFile.isFile()) {
+ throw new IOException("å
¥ååç»ãã¡ã¤ã«ãåå¨ãã¾ãã:" + videoFile.getPath());
+ }
- boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile,
- Setting.getNgId(), Setting.getNgWord());
- if (!conv) {
- sendText("RgϷɸsB¨»ç³K\»ÌÔá¢H");
- return;
+ if (profile.getOutputFileSetting().isAddComment()) {
+ if (!commentFile.isFile()) {
+ throw new IOException("å
¥åã³ã¡ã³ããã¡ã¤ã«ãåå¨ãã¾ãã:" + commentFile.getPath());
}
+ } else {
+ commentFile = null;
}
- stopFlagReturn();
-
- if (Setting.getAddTcomment()) {
- sendText("eÒRgÌÔt@CÖÌÏ·");
-
- boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile,
- Setting.getNgId(), Setting.getNgWord());
- if (!conv) {
- sendText("RgϷɸsB¨»ç³K\»ÌÔá¢H");
- return;
+ if (profile.getOutputFileSetting().isAddTcomment()) {
+ if (!tcommFile.isFile()) {
+ throw new IOException("å
¥åæ稿è
ã³ã¡ã³ããã¡ã¤ã«ãåå¨ãã¾ãã" + tcommFile.getPath());
}
+ } else {
+ tcommFile = null;
}
- stopFlagReturn();
-
- sendText("®æÌÏ·ðJn");
- /*rfI¼Ìmè*/
+ /*ãããªåã®ç¢ºå®*/
File convertedVideoFile;
- if (!Setting.getConvertFile().isFile()) {
- if (fi.getVideoTitle() == null) {
- sendText("Ï·ãÌrfIt@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;
}
/**
- * (lbg[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»ÝvhookCuÉͼ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("VhookCuª©Â©èܹñB");
+ throw new IllegalArgumentException("Vhookã©ã¤ãã©ãªãè¦ã¤ããã¾ããã");
}
- a = new File(Setting.getFontPath());
+ a = profile.getFfmpeg().getFont();
if (!a.canRead()) {
- throw new IllegalArgumentException("tHgª©Â©èܹñB");
- }
- } else {
- if (Setting.getVideoSetting().isDelete()) {
- throw new IllegalArgumentException("Ï·µÈ¢ÌÉA®æíµ¿áÁÄÇ¢ñÅ·©H");
- }
- if (Setting.getCommentSetting().isDelete()) {
- throw new IllegalArgumentException("Ï·µÈ¢ÌÉARgíµ¿áÁÄÇ¢ñÅ·©H");
- }
- if (Setting.getTcommentSetting().isDelete()) {
- throw new IllegalArgumentException("Ï·µÈ¢ÌÉAeÒRgíµ¿áÁÄÇ¢ñÅ·©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());
}
}