这个要链接我之前的文章:ffmpeg的pts之解析_leek5533的博客-CSDN博客
梳理一下时间戳,
首先要有一定的I B P帧概念,和timebase 及timescale的认识,
3. FFmpeg 中的时间基与时间戳
3.1 时间基与时间戳的概念
在 FFmpeg 中,时间基(time_base)是时间戳(timestamp)的单位,时间戳值乘以时间基,可以得到实际的时刻值(以秒等为单位)。例如,如果一个视频帧的 dts 是 40,pts 是 160,其 time_base 是 1/1000 秒,那么可以计算出此视频帧的解码时刻是 40 毫秒(40/1000),显示时刻是 160 毫秒(160/1000)。FFmpeg 中时间戳(pts/dts)的类型是 int64_t 类型,把一个 time_base 看作一个时钟脉冲,则可把 dts/pts 看作时钟脉冲的计数。
3.2 三种时间基 tbr、tbn 和 tbc
不同的封装格式具有不同的时间基。在 FFmpeg 处理音视频过程中的不同阶段,也会采用不同的时间基。
FFmepg 中有三种时间基,命令行中 tbr、tbn 和 tbc 的打印值就是这三种时间基的倒数:
tbn:对应容器中的时间基。值是 AVStream.time_base 的倒数
tbc:对应编解码器中的时间基。值是 AVCodecContext.time_base 的倒数
tbr:从视频流中猜算得到,可能是帧率或场率(帧率的 2 倍)//这个不一定
使用 ffprobe 探测媒体文件格式,如下:
1 2 3 4 5 6 7 8
think@opensuse> ffprobe tnmil3.flv ffprobe version 4.1 Copyright (c) 2007-2018 the FFmpeg developers Input #0, flv, from 'tnmil3.flv': Metadata: encoder : Lavf58.20.100 Duration: 00:00:03.60, start: 0.017000, bitrate: 513 kb/s Stream #0:0: Video: h264 (High), yuv420p(progressive), 784x480, 25 fps, 25 tbr, 1k tbn, 50 tbc Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s
关于 tbr、tbn 和 tbc 的说明,原文如下,来自 FFmpeg 邮件列表:
There are three different time bases for time stamps in FFmpeg. The
values printed are actually reciprocals of these, i.e. 1/tbr, 1/tbn and
1/tbc.tbn is the time base in AVStream that has come from the container, I
think. It is used for all AVStream time stamps.tbc is the time base in AVCodecContext for the codec used for a
particular stream. It is used for all AVCodecContext and related time
stamps.tbr is guessed from the video stream and is the value users want to see
when they look for the video frame rate, except sometimes it is twice
what one would expect because of field rate versus frame rate.
3.3 内部时间基 AV_TIME_BASE
除以上三种时间基外,FFmpeg 还有一个内部时间基 AV_TIME_BASE(以及分数形式的 AV_TIME_BASE_Q)
1 2 3 4 5
// 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}
AV_TIME_BASE 及 AV_TIME_BASE_Q 用于 FFmpeg 内部函数处理,使用此时间基计算得到时间值表示的是微秒。
3.4 时间值形式转换
av_q2d()将时间从 AVRational 形式转换为 double 形式。AVRational 是分数类型,double 是双精度浮点数类型,转换的结果单位是秒。转换前后的值基于同一时间基,仅仅是数值的表现形式不同而已。
av_q2d()实现如下:
1
2
3
4
5
6
7
8
9
/**
* Convert an AVRational to a `double`.
* @param a AVRational to convert
* @return `a` in floating-point form
* @see av_d2q()
*/
static inline double av_q2d(AVRationa