OSDN Git Service

ffmpegを利用して情報を取得するユーティリティクラス
authoryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 02:24:45 +0000 (11:24 +0900)
committeryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 02:25:44 +0000 (11:25 +0900)
frontend/src/saccubus/util/FfmpegUtil.java [new file with mode: 0644]

diff --git a/frontend/src/saccubus/util/FfmpegUtil.java b/frontend/src/saccubus/util/FfmpegUtil.java
new file mode 100644 (file)
index 0000000..4e441ff
--- /dev/null
@@ -0,0 +1,64 @@
+package saccubus.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
+ * @author yuki
+ */
+public final class FfmpegUtil {
+
+    private static final Pattern PATTERN_DURATION = Pattern.compile("Duration: (\\d+):(\\d+):(\\d+)");
+    private final File ffmpegPath;
+    private final File targetPath;
+    private String execResultCache;
+
+    public FfmpegUtil(File ffmpeg, File target) {
+        this.ffmpegPath = ffmpeg;
+        this.targetPath = target;
+    }
+
+    public int getDuration() throws IOException {
+        final String res = getExecResult();
+        final Matcher m = PATTERN_DURATION.matcher(res);
+        if (m.find()) {
+            final int hour = Integer.parseInt(m.group(1));
+            final int min = Integer.parseInt(m.group(2));
+            final int sec = Integer.parseInt(m.group(3));
+            return ((hour * 60) + min) * 60 + sec;
+        } else {
+            throw new IOException("Can Not Get Duration");
+        }
+    }
+
+    private String getExecResult() throws IOException {
+        if (execResultCache != null) {
+            return execResultCache;
+        } else {
+            final ProcessBuilder pb = new ProcessBuilder(ffmpegPath.getPath(), "-i", targetPath.getPath());
+            final Process process = pb.start();
+            final InputStream es = process.getErrorStream();
+            final BufferedReader reader = new BufferedReader(new InputStreamReader(es));
+            final StringBuilder sb = new StringBuilder();
+            String str;
+            while ((str = reader.readLine()) != null) {
+                sb.append(str);
+            }
+            final String res = sb.toString();
+            System.out.println(res);
+            return res;
+        }
+    }
+
+    public static void main(String[] args) throws IOException {
+        final FfmpegUtil util = new FfmpegUtil(new File("bin/ffmpeg.exe"), new File(
+                "_video/[zb4128320]ラジオCandy boy~かなちゃんのいぬ間に~第2回.mp4"));
+        System.out.println(util.getDuration());
+    }
+}