FFmpeg : 时间基函数

604 阅读1分钟

主要函数

  • double av_q2d()
    • 将时间从 AVRational 形式转换为 double 形式。
  • av_packet_rescale_ts():
    • 用于将 AVPacket 中各种时间值从一种时间基转换为另一种时间基。
  • av_rescale_q()
    • 用于不同时间基的转换,用于将时间值从一种时间基转换为另一种时间基。
  • av_rescale_q_rnd()
    • 不同封装格式具有不同的时间基,在转封装(将一种封装格式转换为另一种封装格式)过程中
  • av_inv_q()
    • 取倒数

USE

  • av_q2d()
AVStream stream;
AVPacket packet;
packet 播放时刻值:timestamp(单位秒) = packet.pts × av_q2d(stream.time_base);
packet 播放时长值:duration(单位秒) = packet.duration × av_q2d(stream.time_base);
  • av_rescale_q_rnd()
av_read_frame(ifmt_ctx, &pkt);
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);

tbr, tbn,tbc

  • tbr :从视频流中猜算得到,可能是帧率或场率(帧率的 2 倍).
  • tbc :对应编解码器中的时间基。值是 AVCodecContext.time_base 的倒数;
  • tbn :对应容器中的时间基。值是 AVStream.time_base 的倒数;

转码过程中,时间基转换 建议

  • 在视频解码过程中: 我们不使用 AVCodecContext.time_base,而用帧率倒数作时间基,
  • 在视频编码过程中: 我们将 AVCodecContext.time_base 设置为帧率的倒数。

文章来源