首页 > 解决方案 > FFmpeg - 如何获得转码进度?

问题描述

我正在使用 fffmpeg 做一些视频工作,现在我遇到了一些麻烦。我不知道如何获得转码的进度。我检查了ffmpeg.c,发现大部分时间成本是'transcode',这里是ffmpeg.c#transcode的源代码:

static int transcode(void)
    {
    XLOGD("==========transcode==========");
   ...
    XLOGD("start transcode");
    while (!received_sigterm) {
        int64_t cur_time= av_gettime_relative();

        /* if 'q' pressed, exits */
        if (stdin_interaction)
            if (check_keyboard_interaction(cur_time) < 0)
                break;

        /* check if there's any stream where output is still needed */
        if (!need_output()) {
            av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write 
            to, finishing.\n");
            break;
        }

        ret = transcode_step();
        if (ret < 0 && ret != AVERROR_EOF) {
            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", 
            av_err2str(ret));
            break;
        }

        /* dump report by using the output first video and audio streams */
        print_report(0, timer_start, cur_time);
    }

    return ret;
   }

我这样调用ffmpeg:

int 执行(int argc,char **argv){

如果 CONFIG_AVDEVICE

/* parse options and open all input/output files */
ret = ffmpeg_parse_options(argc, argv);
if (ret < 0){
    return exit_program(1);
}

if (nb_output_files <= 0 && nb_input_files == 0) {
    show_usage();
    av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
    return exit_program(1);
}

/* file converter / grab */
if (nb_output_files <= 0) {
    av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
    return exit_program(1);
}

if (nb_input_files == 0) {
    av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
    return exit_program(1);
}

for (i = 0; i < nb_output_files; i++) {
    if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
        want_sdp = 0;
}

current_time = ti = getutime();
if (transcode() < 0){
    return exit_program(1);
}

返回 main_return_code;}

有什么想法吗?

提前谢谢了。

#

非常感谢,现在我想通了。在 ffmpeg.c 中,函数

print_report(int is_last_report, int64_t timer_start, int64_t cur_time),

我有一些代码块:

secs = FFABS(pts) / AV_TIME_BASE;
    us = FFABS(pts) % AV_TIME_BASE;
    mins = secs / 60;
    secs %= 60;
    hours = mins / 60;
    mins %= 60;

由此我可以知道已转码的持续时间。

标签: ffmpeg

解决方案


首先获取电影的时长:int64_t duration = output_files[0]->ctx->duration;

循环中的第二个:您可以使用以下公式计算百分比: int percent = (int)(((double)(cur_time - timer_start) / (double)(duration )) * 100);


推荐阅读