FFMpeg视频MetaData函数--av_dump_format()

在上篇FFMpeg 播放视频中,我们简单的实现了使用FFMpeg来播放视频,虽然还有很多问题,如没有声音,没有字幕,YUV转换RGB效率比较低,部分视频资源不能播放成功等,需要我们进一步完善。

通过FFMpeg播放视频一文我们知道avformat_open_input()能打开视频容器,那么现在我比较关心视频文件的MetaData怎么查看, 刚好FFMpeg给我们提供了av_dump_format()函数会自动打印MetaData信息

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);  
  
    } 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文转自:FFmpeg 入门(1):截取视频帧 | www.samirchen.com 背景 在 Mac OS 上...
    SamirChen阅读 13,139评论 6 15
  • 教程一:视频截图(Tutorial 01: Making Screencaps) 首先我们需要了解视频文件的一些基...
    90后的思维阅读 10,256评论 0 3
  • 现状:现在视频直播非常的火,所以在视频直播开发中,使用的对视频进行遍解码的框架显得尤为重要了,其实,这种框架蛮多的...
    ZHANG_GO阅读 8,375评论 0 2
  • 期末了,諸事煩亂。越改越洩氣的試卷,填不完的表格,已經壓到頭上的新學期教學,沈甸甸的科研任務,還有不時突如其來的會...
    囈語者阅读 1,228评论 0 0
  • 要说人生孤独的时刻,真是如天上星星一样数也数不完,好似命中注定一般。 从记事起就觉得自己很孤单。那时不懂孤独。从小...
    巴蜀宝贝阅读 3,751评论 0 1