Reputation: 81
I use ffmpeg to convert a clip video.mp4 to video.mp3 for example. Is there a way to calculate the remaining conversion time ?
Upvotes: 1
Views: 3004
Reputation: 2829
Here is a working logic I copied from other pages and modified it to not throw exception while matcher is trying to find speed pattern, also removed unnecessary assignments and made a more dynamic toTimer conversion. And of course I've tested myself, so everything are good to go.
public String getRemainingTime(String message, long videoLengthInMillis) {
Pattern pattern = Pattern.compile("time=([\\d\\w:]{8}[\\w.][\\d]+)");
if (message.contains("speed")) {
Matcher matcher = pattern.matcher(message);
@SuppressWarnings("UnusedAssignment")
String tempTime = "";
if (matcher.find()) {
tempTime = String.valueOf(matcher.group(1));
String[] arrayTime = tempTime.split("[:|.]");
long time =
TimeUnit.HOURS.toMillis(Long.parseLong(arrayTime[0])) +
TimeUnit.MINUTES.toMillis(Long.parseLong(arrayTime[1])) +
TimeUnit.SECONDS.toMillis(Long.parseLong(arrayTime[2])) +
Math.round(Long.parseLong(arrayTime[3]));
String speed = message.substring(message.indexOf("speed=") + 1, message.indexOf("x")).split("=")[1];
long eta = Math.round((Math.round(videoLengthInMillis) - time) / Float.valueOf(speed));
String estimatedTime = toTimer(eta);
Log.d("getRemainingTime", "EstimateTime -> " + estimatedTime);
return estimatedTime;
}
}
return "";
}
private String toTimer(long millis) {
long hours = TimeUnit.MILLISECONDS.toHours(millis);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));
if (hours!= 0 && hours>0)
return String.format(Locale.getDefault(), "%02d:%02d:%02d",hours,minutes,seconds);
else
return String.format(Locale.getDefault(), "%02d:%02d",minutes,seconds);
}
Use it inside FFmpeg onProgress
callback:
@Override
public void onProgress(String message) {
String remainingTime = getRemainingTime(message,yourVideoDurationInMilliseconds);
Log.d("onProgress", "Remaining Time: "+remainingTime);
}
Upvotes: 2