原出处: https://www.cnblogs.com/lihaiping/p/12771234.html

命令输出格式的解析

当对文件命指定%d.jpg这种形式的时候,ffmpeg.c中是如何处理的?我阅读了ffmpeg.c中open_files()和open_output_file(),以及write_packet()相关函数,均未找到以计数命名相关线索,硬生生读了几遍,找了几遍,都没找到,当时好奇怪。

按道理,如果我们来写这种场景处理的话,可能大家都会以计数形式,打开文件,写完,然后关闭这样的流程走。于是我继续跟代码,看看ffmpeg.c中的open_out_file()函数干了些啥,结果发现ffmpeg是直接将%d.jpg这个输出文件直接传入底层了,我们来看下流程:

err = avformat_alloc_output_context2(&oc, NULL, o->format, filename);

(原)FFmpeg.c源码学习笔记1

其中在av_guess_format()函数中,有这样一段代码:

 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
                                const char *mime_type)
{
    const AVOutputFormat *fmt = NULL;
    AVOutputFormat *fmt_found = NULL;
    void *i = 0;
    int score_max, score;

    /* specific test for image sequences */
#if CONFIG_IMAGE2_MUXER
    //这里是第一步,当我们使用-f image2 %xxd.jpg这种的时候,会进入这种选项
    if (!short_name && filename &&
        av_filename_number_test(filename) &&
        ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
        return av_guess_format("image2", NULL, NULL);
    }
#endif
    /* Find the proper file type. */
    score_max = 0;
    while ((fmt = av_muxer_iterate(&i))) {//这里会对muxer_list进行遍历
        score = 0;
        if (fmt->name && short_name && av_match_name(short_name, fmt->name))
            score += 100;
        if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
            score += 10;
        if (filename && fmt->extensions &&
            av_match_ext(filename, fmt->extensions)) {
            score += 5;
        }
        if (score > score_max) {
            score_max = score;
            fmt_found = (AVOutputFormat*)fmt;
        }
    }
    return fmt_found;
}
View Code

相关文章: