OSDN Git Service

ConvertStopFlag, TextProgressListenerクラス削除
[coroid/inqubus.git] / frontend / src / saccubus / converter / FfmpegCommand.java
index ede933c..57e16f6 100644 (file)
@@ -9,39 +9,38 @@ import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.SwingWorker;
 import org.apache.commons.lang.StringUtils;
-import saccubus.ConvertStopFlag;
 import saccubus.conv.ConvertToVideoHook;
-import yukihane.saccubus.converter.profile.NgSetting;
-import yukihane.saccubus.converter.profile.Ffmpeg;
-import yukihane.saccubus.converter.profile.FfmpegOption;
-import yukihane.saccubus.converter.profile.GeneralSetting;
-import saccubus.net.TextProgressListener;
+import saccubus.converter.profile.ConvertProfile;
+import saccubus.converter.profile.FfmpegProfile;
+import saccubus.converter.profile.GeneralProfile;
 import yukihane.mediainfowrapper.Info;
 import yukihane.mediainfowrapper.MediaInfo;
 import yukihane.mediainfowrapper.Size;
+import saccubus.converter.profile.ConvertProfile.HideCondition;
 import yukihane.swf.Cws2Fws;
 
 /**
  *
  * @author yuki
  */
-public class FfmpegCommand extends AbstractCommand {
+public class FfmpegCommand extends SwingWorker<FfmpegCommandResult, FfmpegCommandProgress> {
 
+    private static final Logger logger = Logger.getLogger(FfmpegCommand.class.getName());
     private final File commentMiddleFile;
     private final File tcommMiddleFile;
     private final File TMP_CWS;
     private final File commentFile;
-    private final File tcommFile;
     private final File videoFile;
     private final File convertedVideoFile;
-    private final Ffmpeg ffmpeg;
+    private final ConvertProfile ffmpeg;
 
-    FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile, File tcommFile,
-            File videoFile, File convertedVideoFile, Ffmpeg ffmpeg, GeneralSetting general) throws IOException {
-        super(listener, flag);
+    FfmpegCommand(File commentFile, File videoFile, File convertedVideoFile, ConvertProfile ffmpeg,
+            GeneralProfile general) throws IOException {
         this.commentFile = commentFile;
-        this.tcommFile = tcommFile;
         this.videoFile = videoFile;
         this.convertedVideoFile = convertedVideoFile;
         this.ffmpeg = ffmpeg;
@@ -52,7 +51,8 @@ public class FfmpegCommand extends AbstractCommand {
         TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
     }
 
-    public boolean execute() throws InterruptedException, IOException {
+    @Override
+    protected FfmpegCommandResult doInBackground() throws Exception {
         try {
             return exec();
         } finally {
@@ -68,42 +68,41 @@ public class FfmpegCommand extends AbstractCommand {
         }
     }
 
-    private boolean exec() throws InterruptedException, IOException {
-        final NgSetting ngSetting = getFfmpeg().getNgSetting();
+    private FfmpegCommandResult exec() throws InterruptedException, IOException {
+        final HideCondition ngSetting = getFfmpeg().getNgSetting();
         if (commentFile != null) {
-            sendText("コメントの中間ファイルへの変換中");
+            publish(new FfmpegCommandProgress("コメントの中間ファイルへの変換中"));
             boolean conv = ConvertToVideoHook.convert(commentFile, commentMiddleFile, ngSetting.getId(), ngSetting.
                     getWord());
             if (!conv) {
-                sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
-                return false;
+                publish(new FfmpegCommandProgress("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?"));
+                return new FfmpegCommandResult(false);
             }
         }
-        stopFlagReturn();
-        if (tcommFile != null) {
-            sendText("投稿者コメントの中間ファイルへの変換中");
-            boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
-            if (!conv) {
-                sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
-                return false;
-            }
-        }
-        stopFlagReturn();
-        sendText("動画の変換を開始");
+        checkStop();
+//        if (tcommFile != null) {
+//            sendText("投稿者コメントの中間ファイルへの変換中");
+//            boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
+//            if (!conv) {
+//                sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
+//                return false;
+//            }
+//        }
+//        stopFlagReturn();
+        publish(new FfmpegCommandProgress("動画の変換を開始"));
         int code;
         if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
-                (tcommFile
-                != null), tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
-            sendText("変換が正常に終了しました。");
-            return true;
+                false, tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
+            publish(new FfmpegCommandProgress("変換が正常に終了しました。"));
+            return new FfmpegCommandResult(true);
         } else {
-            sendText("変換エラー:" + convertedVideoFile.getPath());
+            publish(new FfmpegCommandProgress("変換エラー:" + convertedVideoFile.getPath()));
         }
-        return false;
+        return new FfmpegCommandResult(false);
     }
 
     private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
-            boolean addTcomment, String tcommPath, FfmpegOption ov) throws InterruptedException, IOException {
+            boolean addTcomment, String tcommPath, FfmpegProfile ov) throws InterruptedException, IOException {
         File fwsFile = Cws2Fws.createFws(videoFile, TMP_CWS);
 
         List<String> cmdList = new ArrayList<String>();
@@ -137,7 +136,7 @@ public class FfmpegCommand extends AbstractCommand {
 
         final Info info = MediaInfo.getInfo(new File("bin", "MediaInfo"), videoFile);
         // 4:3 なら1.33, 16:9 なら1.76
-        boolean isHD = ((double)info.getWidth()/(double)info.getHeight() > 1.5);
+        boolean isHD = ((double) info.getWidth() / (double) info.getHeight() > 1.5);
         if (ov.isResize()) {
             final Size scaled = (ov.isAdjustRatio()) ? MediaInfo.adjustSize(info, ov.getResizeWidth(), ov.
                     getResizeHeight()) : new Size(info.getWidth(), info.getHeight());
@@ -148,7 +147,7 @@ public class FfmpegCommand extends AbstractCommand {
         List<String> avfilterArgs = getAvfilterOptions(ov.getAvfilterOption());
         if (!getFfmpeg().isVhookDisabled()) {
             String vhookArg = getVhookArg(addComment, commPath, addTcomment, tcommPath, isHD);
-            if(StringUtils.isNotBlank(vhookArg)){
+            if (StringUtils.isNotBlank(vhookArg)) {
                 avfilterArgs.add(vhookArg);
             }
         }
@@ -161,30 +160,31 @@ public class FfmpegCommand extends AbstractCommand {
 
         cmdList.add(convertedVideoFile.getPath());
 
-        System.out.print("arg:");
+        final StringBuilder argMsg = new StringBuilder();
+        argMsg.append("arg:");
         for (String s : cmdList) {
-            System.out.print(" " + s);
+            argMsg.append(" ").append(s);
         }
-        System.out.println();
+        logger.log(Level.INFO, argMsg.toString());
 
+        Process process = null;
         try {
-            System.out.println("\n\n----\nProcessing FFmpeg...\n----\n\n");
-            Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
+            logger.log(Level.INFO, "\n\n----\nProcessing FFmpeg...\n----\n\n");
+            process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
             BufferedReader ebr = new BufferedReader(new InputStreamReader(
                     process.getErrorStream()));
             String e;
             while ((e = ebr.readLine()) != null) {
                 String state = e;
                 if (state.startsWith("frame=")) {
-                    sendText(state);
+                    publish(new FfmpegCommandProgress(state));
                 } else if (!state.endsWith("No accelerated colorspace conversion found")) {
-                    System.out.println(e);
+                    logger.log(Level.INFO, e);
                 }
 
                 try {
-                    stopFlagReturn();
+                    checkStop();
                 } catch (InterruptedException ex) {
-                    process.destroy();
                     throw ex;
                 }
 
@@ -192,6 +192,10 @@ public class FfmpegCommand extends AbstractCommand {
             process.waitFor();
             return process.exitValue();
         } finally {
+            // TODO 正常終了した場合もdestroyしていいのか?
+            if (process != null) {
+                process.destroy();
+            }
             if (fwsFile != null) {
                 fwsFile.delete();
             }
@@ -239,7 +243,7 @@ public class FfmpegCommand extends AbstractCommand {
             sb.append("--enable-show-video");
             sb.append("|");
         }
-        if (getFfmpeg().isSelfAdjustFontSize()) {
+        if (!getFfmpeg().isDisableFontSizeArrange()) {
             sb.append("--enable-fix-font-size");
             sb.append("|");
         }
@@ -247,14 +251,20 @@ public class FfmpegCommand extends AbstractCommand {
             sb.append("--enable-opaque-comment");
             sb.append("|");
         }
-        if(isHD){
+        if (isHD) {
             sb.append("--aspect-mode:1");
             sb.append("|");
         }
         return sb.toString();
     }
 
-    private Ffmpeg getFfmpeg() {
+    private ConvertProfile getFfmpeg() {
         return ffmpeg;
     }
+
+    protected void checkStop() throws InterruptedException {
+        if (Thread.interrupted()) {
+            throw new InterruptedException("中止要求を受け付けました");
+        }
+    }
 }