FFmpeg - 计算剩余转换时间

时间:2017-04-27 12:23:14

标签: video ffmpeg converter

我使用ffmpeg将剪辑video.mp4转换为video.mp3。 有没有办法计算剩余转换时间?

1 个答案:

答案 0 :(得分:1)

这是我从其他页面复制来的一种工作逻辑,并对其进行了修改,以使其在匹配器尝试查找速度模式时不会引发异常,还删除了不必要的分配,并使toTimer转换更加动态。当然,我已经进行了自我测试,因此一切都很好。

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);
}

在FFmpeg onProgress回调中使用它:

@Override
public void onProgress(String message) {
    String remainingTime = getRemainingTime(message,yourVideoDurationInMilliseconds);
    Log.d("onProgress", "Remaining Time: "+remainingTime);
}