FFmpeg之avformat_open_input解析

我们知道avformat_open_input()能打开视频容器,那么现在我比较关心视频文件的MetaData怎么查看, 刚好FFMpeg给我们提供了av_dump_format()函数会自动打印MetaData信息,打印有关输入或输出格式的详细信息,例如
  *持续时间,比特率,流,容器,程序,元数据,边数据,
  *编解码器和时基。。

函数说明
        一般使用av_find_stream_info函数探测码流格式,它的作用是为pFormatContext->streams填充上正确的音视频格式信息。可以通过av_dump_format函数将音视频数据格式通过av_log输出到指定的文件或者控制台,方便开发者了解输入的视音频格式,对于程序的调用,删除该函数的调用没有任何的影响
函数原型:/**
 * Print detailed information about the input or output format, such as
 * duration, bitrate, streams, container, programs, metadata, side data,
 * codec and time base.
 *
 * @param ic        the context to analyze   要分析的上下文
 * @param index     index of the stream to dump information about  转储信息的流的索引
 * @param url       the URL to print, such as source or destination file  要打印的URL,例如源文件或目标文件
 * @param is_output Select whether the specified context is an input(0) or output(1)  选择指定的上下文是输入0还是输出1
 */

void av_dump_format(AVFormatContext *ic,
                    int index,
                    const char *url,
                    int is_output) {

    int i;
    uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
    if (ic->nb_streams && !printed)
        return;

    av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
           is_output ? "Output" : "Input",
           index,
           is_output ? ic->oformat->name : ic->iformat->name,
           is_output ? "to" : "from", url);
    dump_metadata(NULL, ic->metadata, "  ");

    if (!is_output) {
        av_log(NULL, AV_LOG_INFO, "  Duration: ");
        if (ic->duration != AV_NOPTS_VALUE) {
            int hours, mins, secs, us;
            int64_t duration = ic->duration + (ic->duration <= INT64_MAX - 5000 ? 5000 : 0);
            secs  = duration / AV_TIME_BASE;
            us    = duration % AV_TIME_BASE;
            mins  = secs / 60;
            secs %= 60;
            hours = mins / 60;
            mins %= 60;
            av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
                   (100 * us) / AV_TIME_BASE);
        } else {
            av_log(NULL, AV_LOG_INFO, "N/A");
        }
        if (ic->start_time != AV_NOPTS_VALUE) {
            int secs, us;
            av_log(NULL, AV_LOG_INFO, ", start: ");
            secs = llabs(ic->start_time / AV_TIME_BASE);
            us   = llabs(ic->start_time % AV_TIME_BASE);
            av_log(NULL, AV_LOG_INFO, "%s%d.%06d",
                   ic->start_time >= 0 ? "" : "-",
                   secs,
                   (int) av_rescale(us, 1000000, AV_TIME_BASE));
        }
        av_log(NULL, AV_LOG_INFO, ", bitrate: ");
        if (ic->bit_rate)
            av_log(NULL, AV_LOG_INFO, "%"PRId64" kb/s", ic->bit_rate / 1000);
        else
            av_log(NULL, AV_LOG_INFO, "N/A");
        av_log(NULL, AV_LOG_INFO, "\n");
    }

    for (i = 0; i < ic->nb_chapters; i++) {
        AVChapter *ch = ic->chapters[i];
        av_log(NULL, AV_LOG_INFO, "    Chapter #%d:%d: ", index, i);
        av_log(NULL, AV_LOG_INFO,
               "start %f, ", ch->start * av_q2d(ch->time_base));
        av_log(NULL, AV_LOG_INFO,
               "end %f\n", ch->end * av_q2d(ch->time_base));

        dump_metadata(NULL, ch->metadata, "    ");
    }

    if (ic->nb_programs) {
        int j, k, total = 0;
        for (j = 0; j < ic->nb_programs; j++) {
            AVDictionaryEntry *name = av_dict_get(ic->programs[j]->metadata,
                                                  "name", NULL, 0);
            av_log(NULL, AV_LOG_INFO, "  Program %d %s\n", ic->programs[j]->id,
                   name ? name->value : "");
            dump_metadata(NULL, ic->programs[j]->metadata, "    ");
            for (k = 0; k < ic->programs[j]->nb_stream_indexes; k++) {
                dump_stream_format(ic, ic->programs[j]->stream_index[k],
                                   index, is_output);
                printed[ic->programs[j]->stream_index[k]] = 1;
            }
            total += ic->programs[j]->nb_stream_indexes;
        }
        if (total < ic->nb_streams)
            av_log(NULL, AV_LOG_INFO, "  No Program\n");
    }

    for (i = 0; i < ic->nb_streams; i++)
        if (!printed[i])
            dump_stream_format(ic, i, index, is_output);

    av_free(printed);
}

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/hardy/Library/Developer/CoreSimulator/Devices/9E375B33-C850-474D-8350-F743031C2926/data/Containers/Bundle/Application/60F40FEA-3FB3-432E-A35E-D4DDE159752F/HTFFMpegPlayer.app/1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42isomavc1
    creation_time   : 2013-10-08 15:58:56
    title           : Popeye for President - http://www.archive.org/details/Popeye_forPresident
    artist          : Seymour Kneitel
    composer        : Seymour Kneitel
    comment         : license:  http://creativecommons.org/licenses/publicdomain/
    date            : 1956
    encoder         : HandBrake 0.9.9 2013051800
  Duration: 00:01:00.03, bitrate: N/A
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none(bt470bg/smpte240m/bt709), 314x240, 377 kb/s, SAR 1:1 DAR 157:120, 29.97 fps, 29.97 tbr, 90k tbn (default)
    Metadata:
      creation_time   : 2013-10-08 15:58:56
      encoder         : JVT/AVC Coding
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, 2 channels, 127 kb/s (default)
    Metadata:
      creation_time   : 2013-10-08 15:58:56

在上面一长串的信息中,发现Duration是描述视频的长度关键字。 但av_dump_format()打印出的信息只能在控制条查看,无法在代码中获取。怎么通过代码获取视频总时间了?

在AVSteam中有关Duration的描述如下:

/** Decoding: duration of the stream, in stream time base. 
 If a source file does not specify a duration, but does specify a bitrate, this value will be estimated from bitrate and file size.
 */ 
// 流长度,是以time base为单位的。 如果源文件没有定义流的持续时间,但是定义了比特率,则duration可以通过比特率和文件大小计算。
int64_t duration;

在AVFormatContext中也有duration的定义

/**  Duration of the stream, in AV_TIME_BASE fractional  seconds. Only set this value if you know none of the individual stream durations and also do not set any of them. This is deduced from the AVStream values if not set.  Demuxing only, set by libavformat. 
*/
// 只设置该值,如果不知道单个流持续时间,也不设置任何一个流持续   时间。这是从流值,如果没有设置。 
 int64_t duration;

我们分别打印下avStrem->duration 和 avFormatContenx ->duration

5402402
60032000

根据经验视频的时间总长计算公式为:

avStream->duration * av_q2d(avStream->time_base)

查阅相关资料显示,视频时间计算FFMpeg源码为

if(pFormatCtx->duration != AV_NOPTS_VALUE){  
        int hours, mins, secs, us;  
        int64_t duration = pFormatCtx->duration + 5000;  
        secs = duration / AV_TIME_BASE;  
        us = duration % AV_TIME_BASE;  
        mins = secs / 60;  
        secs %= 60;  
        hours = mins/ 60;  
        mins %= 60;  
        printf("%02d:%02d:%02d.%02d\n", hours, mins, secs, (100 * us) / AV_TIME_BASE);  
  
    } 

小礼物走一



作者:Hardy_Hu
链接:https://www.jianshu.com/p/300475f2cb43
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/xiaomucgwlmx/article/details/82916695