* <p>
* タイトル: さきゅばす
* </p>
- *
+ *
* <p>
* 説明: ニコニコ動画の動画をコメントつきで保存
* </p>
- *
+ *
* <p>
* 著作権: Copyright (c) 2007 PSI
* </p>
- *
+ *
* <p>
* 会社名:
* </p>
- *
+ *
* @author 未入力
* @version 1.0
*/
+// TODO 不要になる予定
public class ConvertStopFlag {
public interface StateChangeListener {
/* $Id$ */
package saccubus.converter;
+import javax.swing.SwingWorker;
import static org.apache.commons.lang.Validate.*;
import saccubus.ConvertStopFlag;
import saccubus.net.TextProgressListener;
*
* @author yuki
*/
-public abstract class AbstractCommand {
+public abstract class AbstractCommand<T, V> extends SwingWorker<T, V> {
private final TextProgressListener listener;
- private final ConvertStopFlag StopFlag;
- public AbstractCommand(TextProgressListener listener, ConvertStopFlag stopFlag) {
+ public AbstractCommand(TextProgressListener listener) {
notNull(listener);
- notNull(stopFlag);
this.listener = listener;
- this.StopFlag = stopFlag;
}
- protected void stopFlagReturn() throws InterruptedException {
- if (getStopFlag().needStop()) {
- throw new InterruptedException("中止しました。");
+ protected void checkStop() throws InterruptedException {
+ if (Thread.interrupted()) {
+ throw new InterruptedException("中止要求を受け付けました");
}
}
protected TextProgressListener getListener() {
return listener;
}
-
- /**
- * @return the StopFlag
- */
- protected ConvertStopFlag getStopFlag() {
- return StopFlag;
- }
}
--- /dev/null
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class ConvertProgress {
+
+}
--- /dev/null
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class ConvertResult {
+
+ private final boolean resultValue;
+
+ ConvertResult(boolean b) {
+ this.resultValue = b;
+ }
+
+ public boolean getResultValue() {
+ return resultValue;
+ }
+}
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.Callable;
-import java.util.logging.Level;
import java.util.logging.Logger;
import nicobrowser.GetFlvResult;
import nicobrowser.NamePattern;
* @author 未入力
* @version 1.0
*/
-public class Converter extends AbstractCommand implements Callable<Boolean> {
+public abstract class Converter extends AbstractCommand<ConvertResult, ConvertProgress> implements Callable<Boolean> {
private static final Logger logger = Logger.getLogger(Converter.class.getName());
private final Profile profile;
*/
public Converter(String id, Profile profile,
TextProgressListener listener, ConvertStopFlag flag) {
- super(listener, flag);
+ super(listener);
this.movieId = id;
this.profile = profile;
}
@Override
public Boolean call() throws Exception {
- boolean result = false;
try {
- result = runConvert();
+ final ConvertResult result = doInBackground();
+ return Boolean.valueOf(result.getResultValue());
} finally {
- getStopFlag().finished();
+ // TODO 何か処理が必要?
+// getStopFlag().finished();
}
- return Boolean.valueOf(result);
}
- // TODO Runnableを実装しなくなったので削除する
- public void run() {
- try {
- call();
- } catch (Exception ex) {
- String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
- sendText(text);
- logger.log(Level.SEVERE, null, ex);
- }
- }
+// // TODO Runnableを実装しなくなったので削除する
+// public void run() {
+// try {
+// call();
+// } catch (Exception ex) {
+// String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
+// sendText(text);
+// logger.log(Level.SEVERE, null, ex);
+// }
+// }
- private boolean runConvert() throws IOException, InterruptedException, Exception {
+ @Override
+ protected ConvertResult doInBackground() throws Exception {
if (!shouldRun(profile)) {
sendText("何もすることがありません");
- return true;
+ return new ConvertResult(true);
}
validSetting();
}
}
- stopFlagReturn();
-
+ checkStop();
File commentFile;
if (profile.getCommentSetting().isDownload()) {
commentFile = profile.getCommentSetting().getLocalFile();
}
- stopFlagReturn();
+ checkStop();
File videoFile;
GetFlvResult vf = null;
if (!profile.getOutputFileSetting().isConvert()) {
sendText("動画・コメントを保存し、変換は行いませんでした。");
- return true;
+ return new ConvertResult(true);
}
if (!videoFile.isFile()) {
final boolean isNotLow = (vf == null) ? true : (vf.getStatus() != Status.GET_LOW);
File convertedVideoFile = getOutputFileName(vi.getTitleInWatchPage(), isNotLow);
- boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
- convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
- return res;
- }
-
- public boolean isConverted() {
- return getStopFlag().isFinished();
- }
+ return new ConvertResult(true);
- @Override
- public ConvertStopFlag getStopFlag() {
- return super.getStopFlag();
+ // TODO FFMPEG 実行開始は別タスクとして実装する.
+// boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
+// convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
+// return res;
}
/** @return 何か実行すべき処理があればtrue. */
*
* @author yuki
*/
-public class FfmpegCommand extends AbstractCommand {
+public class FfmpegCommand extends AbstractCommand<FfmpegCommandResult, FfmpegCommandProgress> {
private static final Logger logger = Logger.getLogger(FfmpegCommand.class.getName());
private final File commentMiddleFile;
private final File tcommMiddleFile;
FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile,
File videoFile, File convertedVideoFile, ConvertProfile ffmpeg, GeneralProfile general) throws IOException {
- super(listener, flag);
+ super(listener);
this.commentFile = commentFile;
this.videoFile = videoFile;
this.convertedVideoFile = convertedVideoFile;
TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
}
- public boolean execute() throws InterruptedException, IOException {
+
+ @Override
+ protected FfmpegCommandResult doInBackground() throws Exception {
try {
return exec();
} finally {
}
}
- private boolean exec() throws InterruptedException, IOException {
+ private FfmpegCommandResult exec() throws InterruptedException, IOException {
final HideCondition ngSetting = getFfmpeg().getNgSetting();
if (commentFile != null) {
sendText("コメントの中間ファイルへの変換中");
getWord());
if (!conv) {
sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
- return false;
+ return new FfmpegCommandResult(false);
}
}
- stopFlagReturn();
+ checkStop();
// if (tcommFile != null) {
// sendText("投稿者コメントの中間ファイルへの変換中");
// boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
// return false;
// }
// }
- stopFlagReturn();
+// stopFlagReturn();
sendText("動画の変換を開始");
int code;
if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
false, tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
sendText("変換が正常に終了しました。");
- return true;
+ return new FfmpegCommandResult(true);
} else {
sendText("変換エラー:" + convertedVideoFile.getPath());
}
- return false;
+ return new FfmpegCommandResult(false);
}
private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
}
logger.log(Level.INFO, argMsg.toString());
+ Process process = null;
try {
logger.log(Level.INFO, "\n\n----\nProcessing FFmpeg...\n----\n\n");
- Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
+ process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
BufferedReader ebr = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String e;
}
try {
- stopFlagReturn();
+ checkStop();
} catch (InterruptedException ex) {
- process.destroy();
throw ex;
}
process.waitFor();
return process.exitValue();
} finally {
+ // TODO 正常終了した場合もdestroyしていいのか?
+ if (process != null) {
+ process.destroy();
+ }
if (fwsFile != null) {
fwsFile.delete();
}
--- /dev/null
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class FfmpegCommandProgress {
+
+}
--- /dev/null
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class FfmpegCommandResult {
+
+ private final boolean resultValue;
+
+ FfmpegCommandResult(boolean b) {
+ this.resultValue = b;
+ }
+
+ public boolean isResultValue() {
+ return resultValue;
+ }
+}