zoukankan      html  css  js  c++  java
  • FFmpeg中的时间基(time_base), AV_TIME_BASE

    一. AV_TIME_BASE

    经常在FFmpeg的代码中看到一个奇怪的单位 AV_TIME_BASE ,比如 AVFormatContext 结构体中就有这样一个字段: duration ,它在FFmpeg中的解释如下:

    /**
         * 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;

    以一段时长为60s的视频为例,用FFmpeg将其读入到内存,并打印出它的 duration 后发现:

       AVFormatContext *ic = NULL;
        int re = avformat_open_input(&ic, path, 0, 0);
        if (re != 0) {
            LOGE("avformat_open_input failed: %s", av_err2str(re));
            return;
        }
        LOGI("avformat_open_input %s success", path);
        re = avformat_find_stream_info(ic, 0);
        if (re != 0) {
            LOGE("avformat_find_stream_info failed: %s", av_err2str(re));
        }
        LOGI("duration is: %lld, nb_streams = %d, input filename is: %s, bitrate = %lld", ic->duration, ic->nb_streams, ic->filename, ic->bit_rate);

     duration 此时为60000000,而它的注释也说得很清楚,是以 AV_TIME_BASE 为单位,找到 AV_TIME_BASE  :

    /**
     * Internal time base represented as integer
     */
    
    #define AV_TIME_BASE            1000000
    
    /**
     * Internal time base represented as fractional value
     */
    
    #define AV_TIME_BASE_Q          (AVRational){1, AV_TIME_BASE}

    发现该值为1000000,60000000/1000000刚好为60,而1s = 1000000μs 由此可见,FFmpeg内部的时间单位其实是微秒(μs),而 AV_TIME_BASE_Q 其实是一种分数的表示形式,其中的1表示分子, AV_TIME_BASE 也就是1000000,表示的是分母,所以它其实就是1微秒,也就是 1/1000000 秒。

    事实上,除了 AVFormatContext  中的 duration ,FFmpeg中的pts,dts也是基于timebase来换算的,时间基(time_base)是FFmpeg中作为时间单位的概念,比如上面的 AV_TIME_BASE_Q ,它就相当于是 1/1000000 秒,也就是把1s分成1000000份,可以理解成是一把尺,那么每一格就是 1/1000000 秒,此时的time_base就是{1, 1000000}。所谓的时间基就是指每个刻度是多少秒。那么它的作用是什么呢?其实就是为了更精确的度量时间。

    二. pts/dts的时间戳究竟代表什么

    之前解释过PTS和DTS的基本概念:

    DTS(Decoding Time Stamp):即解码时间戳,这个时间戳的意义在于告诉播放器该在什么时候解码这一帧的数据。
    PTS(Presentation Time Stamp):即显示时间戳,这个时间戳用来告诉播放器该在什么时候显示这一帧的数据。

    光看字面意思,它也是一种时间戳,那它这种时间戳到底是什么样子的?是传统意义上 yyyy-MM-dd HH:mm:ss ?首先可以肯定的是绝不是 yyyy-MM-dd HH:mm:ss,因为这种时间戳的控制精度不够,上面甚至都用μs来表示了,那这个PTS和DTS的值到底是个什么东西?

    pts和dts的值指的是占多少个时间刻度(占多少个格子)。它的单位不是秒,而是时间刻度。只有pts与time_base两者结合在一起,才能表达出具体的时间是多少。好比我只告诉你,某个物体的长度占某一把尺上的20个刻度。但是我不告诉你,每个刻度是多少厘米,你仍然无法知道物体的长度。pts 就是这样的东西,pts(占了多少个时间刻度) , time_base(每个时间刻度是多少秒) ,而帧的显示时间戳 =  pts(占了多少个时间刻度)  * time_base(每个时间刻度是多少秒)。

    三. 一些时间基转换的场景

    【计算视频总时长】

    AVFormatContext *ifmt_ctx = NULL;
    avformat_open_input(&ifmt_ctx, filename, NULL, NULL);
    double totle_seconds = ifmt_ctx->duration * av_q2d(AV_TIME_BASE_Q);

    【根据PTS求出一帧在视频中对应的秒数位置】

    double sec = enc_pkt.pts * av_q2d(ofmt_ctx->streams[stream_index]->time_base);

    【ffmpeg内部的时间戳与标准的时间转换方法】

    timestamp(ffmpeg内部时间戳) = AV_TIME_BASE * time(秒)
    time(秒) = AV_TIME_BASE_Q * timestamp(ffmpeg内部时间戳)

    【当需要把视频Seek到N秒的时候】

     // 指定流索引
    int pos = 20 * r2d(ic->streams[videoStream]->time_base);
    av_seek_frame(ic,videoStream, pos, AVSEEK_FLAG_BACKWARD|AVSEEK_FLAG_FRAME );
    // 未指定指定流索引
    int64_t timestamp = N * AV_TIME_BASE; 
    av_seek_frame(fmtctx, -1, timestamp, AVSEEK_FLAG_BACKWARD);

    参考链接:

    1.理解ffmpeg中的pts,dts,time_base

    2.FFmpeg时间戳整理

    3.C++编程音视频库ffmpeg的pts时间怎么换算

  • 相关阅读:
    数论-剩余类、完全剩余系、缩系、欧拉函数
    数论-同余式
    计算机科研项目中的重点项目、重大项目、重大研究计划项目,重点研发计划有什么区别和联系?
    VFS虚拟文件系统
    git中文名转义带来的麻烦;git配置之core.quotepath;git中文乱码
    nodejs 与 npm 配置
    mongodb 部署 安装 使用 记录
    GPG error: https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 656408E390CFB1F5
    slurm 网路监控软件使用
    Win10 64位,北航研究生教务系统文件打印办法(旧版GSMIS),只要三步就能解决;
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/10684953.html
Copyright © 2011-2022 走看看