time_base梳理


目录
  • time_base的定义与使用方法
  • time_base 存在场景

time_base的定义与使用方法

time_base:时间精度

typedef struct AVRational{
    int num; ///< numerator
    int den; ///< denominator
} AVRational;

AVRational相当于分数形式,为自定义结构体,无法参与到数值计算中,通过av_q2d(time_base)转换为double就可以参与计算了

编码过程中会存在由AVCodecContext中time_base转变为AVStream中time_base,执行av_scale_q

具体对应:

Tduration = AVStream.duration * AVStream.time_base

Tpts = AVFrame.pts * AVCodecContext.time_base

Tdts = AVFrame.dts * AVCodecContext.time_base

ffmpeg内部使用精度为

#define AV_TIME_BASE 1000000/*微秒*/

编码前的时间基位于AVCodecContext中,AVFrame使用

编码后的时间基位于AVStream中,AVPacket AVStream石笋

time_base 存在场景

  1. AVCodecContext
  2. AVStream

mux存入文件容器中的time:采用AVStream中time_base

demux出来的time,采用AVStream中的time_base

decode出来的frame采用AVCodecContext中的time_base

AVPacket中time(编码后数据),采用AVStream中time_base

音视频的计量单位不同

AVFrame视频要存储图片,则pts使用codec_ctx->gop_size计量,pts += 1; time_base使用帧率计量,time_base = 1/ codec_ctx->gop_size

AVFrame音频要存储声音,则pts使用codec_ctx->sample_rate计量,pts += 1, time_base = 1 / codec_ctx->sample_rate;

nb_samples == 1/ codec_ctx->sample_rate

参考文章:

ffmpeg time_base详解