--- /dev/null
+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());
+ }
+}