Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a complete example for you which should get you started</p> <pre><code>import java.io.*; import java.util.Scanner; import java.util.regex.Pattern; class Test { public static void main(String[] args) throws IOException { ProcessBuilder pb = new ProcessBuilder("ffmpeg","-i","in.webm","out.mp4"); final Process p = pb.start(); new Thread() { public void run() { Scanner sc = new Scanner(p.getErrorStream()); // Find duration Pattern durPattern = Pattern.compile("(?&lt;=Duration: )[^,]*"); String dur = sc.findWithinHorizon(durPattern, 0); if (dur == null) throw new RuntimeException("Could not parse duration."); String[] hms = dur.split(":"); double totalSecs = Integer.parseInt(hms[0]) * 3600 + Integer.parseInt(hms[1]) * 60 + Double.parseDouble(hms[2]); System.out.println("Total duration: " + totalSecs + " seconds."); // Find time as long as possible. Pattern timePattern = Pattern.compile("(?&lt;=time=)[\\d.]*"); String match; while (null != (match = sc.findWithinHorizon(timePattern, 0))) { double progress = Double.parseDouble(match) / totalSecs; System.out.printf("Progress: %.2f%%%n", progress * 100); } } }.start(); } } </code></pre> <p><strong>Output:</strong></p> <pre><code>Total duration: 117.7 seconds. Progress: 7.71% Progress: 16.40% Progress: 25.00% Progress: 33.16% Progress: 42.67% Progress: 51.35% Progress: 60.57% Progress: 69.07% Progress: 78.02% Progress: 86.49% Progress: 95.94% Progress: 99.97% </code></pre> <p>You may also consider using some kind of Java bindings for ffmpeg such as <a href="http://code.google.com/p/jjmpeg/">jjmpeg</a> which may provide what you need in a more robust way.</p> <p><em>EDIT</em></p> <p>With ffmpeg 2.0, time output is <code>HH:mm:ss.S</code> so the <code>timePattern</code> needs a to incorporate a <code>:</code></p> <pre><code>Pattern timePattern = Pattern.compile("(?&lt;=time=)[\\d:.]*"); </code></pre> <p>In addition, the <code>dur</code> will need to be split on <code>:</code> and summed together</p> <pre><code>String[] matchSplit; while (null != (match = sc.findWithinHorizon(timePattern, 0))) { matchSplit = match.split(":") double progress = Integer.parseInt(matchSplit[0]) * 3600 + Integer.parseInt(matchSplit[1]) * 60 + Double.parseDouble(matchSplit[2]) / totalSecs; //... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload