OSDN Git Service

変換時の進捗をプログレスバーで表示する
authoryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 02:47:02 +0000 (11:47 +0900)
committeryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 02:47:02 +0000 (11:47 +0900)
frontend/src/saccubus/worker/impl/convert/Convert.java

index bcc40f2..8e05774 100644 (file)
@@ -15,7 +15,10 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import saccubus.conv.ConvertToVideoHook;
+import saccubus.util.FfmpegUtil;
 import saccubus.worker.Worker;
 import saccubus.worker.WorkerListener;
 import saccubus.worker.profile.ConvertProfile;
@@ -115,7 +118,8 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
             final File target = (fwsFile != null) ? fwsFile : videoFile;
 
             final List<String> arguments = createArguments(target, transformedComment, outputFile);
-            return executeFfmpeg(arguments);
+            final int duration = new FfmpegUtil(profile.getFfmpeg(), target).getDuration();
+            return executeFfmpeg(arguments, duration);
         } finally {
             if (fwsFile != null && fwsFile.exists()) {
                 fwsFile.delete();
@@ -183,7 +187,9 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
         return cmdList;
     }
 
-    private int executeFfmpeg(final List<String> cmdList) throws InterruptedException, IOException {
+    private static final Pattern PATTERN_TIME = Pattern.compile("time=(\\d+)");
+
+    private int executeFfmpeg(final List<String> cmdList, int duration) throws InterruptedException, IOException {
         Process process = null;
         try {
             logger.log(Level.INFO, "Processing FFmpeg...");
@@ -193,8 +199,15 @@ public class Convert extends Worker<ConvertResult, ConvertProgress> {
             String msg;
             while ((msg = ebr.readLine()) != null) {
                 if (msg.startsWith("frame=")) {
-                    // TODO パーセンテージ計算、出力
-                    publish(new ConvertProgress(PROCESS, 0.0, msg));
+                    final Matcher m = PATTERN_TIME.matcher(msg);
+                    double per = -1.0;
+                    if (m.find()) {
+                        final String strTime = m.group(1);
+                        final int time = Integer.parseInt(strTime);
+                        per = 100.0 * time / duration;
+                        logger.log(Level.FINEST, "time:{0}, duration:{1}", new Object[]{time, duration});
+                    }
+                    publish(new ConvertProgress(PROCESS, per, msg));
                 } else if (!msg.endsWith("No accelerated colorspace conversion found")) {
                     logger.log(Level.INFO, msg);
                 }