Ecco un esempio completo per voi che dovrebbe iniziare
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("(?<=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("(?<=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();
}
}
uscita:
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%
Si può anche considerare l'utilizzo di un qualche tipo di binding Java per ffmpeg come jjmpeg che può fornire quello che ti serve in un modo più robusto.
EDIT
Con ffmpeg 2.0, uscita tempo è HH:mm:ss.S
così la timePattern
bisogno di una incorporare un :
Pattern timePattern = Pattern.compile("(?<=time=)[\\d:.]*");
Inoltre, dovranno essere diviso in :
e sommati il dur
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;
//...
ottengo uscita come 'frame = 2.822 fps = 493 q = 19.1 tempo lSize = 4082kB = 117.66 bitrate = 284.2kbits/s' esattamente come si prevede di estrarre il registrare progressi%? – aioobe
vedere prima di tutto che mostra la Durata all'avvio ... Voglio che sia letto per primo e lo convertirà in secondi e memorizziamo tutte le righe come 'frame = 2822 fps = 493 q = 19.1 Lsize = 4082kB time = 117,66 bitrate = 284,2kbits/s' e da questa riga estratto tempo = valore e attraverso questo troverò progresso con la formula '(tempo/durata) * 100' – shalki
che vedo. Lasciatemi fare un tentativo :-) – aioobe