Libavformat 记录 RTP 流太快(FPS 太高)
我正在尝试使用 libavformat 录制 RTP 流,但是录制视频的 FPS 非常高,我得到了数千 FPS,如果不是一万的话。我尝试将 FPS 设置为 30,并进行以下修改:
AVFormatContext *src; // Assume src is set here to demuxer's context
AVStream *source_stream = src->streams[i];
AVStream *dest_stream = avformat_new_stream(dest, NULL); // dest is muxer's context here
const AVCodec *pCodec = avcodec_find_decoder(source_stream->codecpar->codec_id);
AVCodecContext *avctx = avcodec_alloc_context3(pCodec);
avctx->time_base = av_make_q(1, 30000);
avcodec_parameters_to_context(avctx, source_stream->codecpar);
avcodec_parameters_from_context(dest_stream->codecpar, avctx);
dest_stream->sample_aspect_ratio = source_stream->sample_aspect_ratio;
dest_stream->time_base = avctx->time_base;
dest_stream->avg_frame_rate = av_make_q(30, 1);
dest_stream->r_frame_rate = source_stream->r_frame_rate;
dest_stream->disposition = source_stream->disposition;
然后,在记录阶段,我在读取新数据包时执行以下操作:
packet->pts = av_rescale_q(packet->pts, src_stream->time_base, dest_stream->time_base);
packet->dts = av_rescale_q(packet->dts, src_stream->time_base, dest_stream->time_base);
packet->duration = dest_stream->time_base.den / dest_stream->time_base.num / dest_stream->avg_frame_rate.num * dest_stream->avg_frame_rate.den;
av_interleaved_write_frame(dest, packet);
我得到的错误日志以重复的方式如下:
[mp4 @ 0x7fc514001d00] Application provided duration: ... / timestamp: ... is out of range for mov/mp4 format
[mp4 @ 0x7fc514001d00] pts has no value
libav我使用的 版本如下:
ffmpeg version 4.3.3 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.2.0-8ubuntu3)
configuration:
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
PS:升级 ffmpeg
版本不起作用。
I am trying to record an RTP stream using libavformat
, however the FPS of recorded videos are ridiculously high, I get thousands of FPS, if not ten thousands. I tried to set the FPS to 30 making the following modifications:
AVFormatContext *src; // Assume src is set here to demuxer's context
AVStream *source_stream = src->streams[i];
AVStream *dest_stream = avformat_new_stream(dest, NULL); // dest is muxer's context here
const AVCodec *pCodec = avcodec_find_decoder(source_stream->codecpar->codec_id);
AVCodecContext *avctx = avcodec_alloc_context3(pCodec);
avctx->time_base = av_make_q(1, 30000);
avcodec_parameters_to_context(avctx, source_stream->codecpar);
avcodec_parameters_from_context(dest_stream->codecpar, avctx);
dest_stream->sample_aspect_ratio = source_stream->sample_aspect_ratio;
dest_stream->time_base = avctx->time_base;
dest_stream->avg_frame_rate = av_make_q(30, 1);
dest_stream->r_frame_rate = source_stream->r_frame_rate;
dest_stream->disposition = source_stream->disposition;
Then, during the recording phase, I do the following as I read a new packet:
packet->pts = av_rescale_q(packet->pts, src_stream->time_base, dest_stream->time_base);
packet->dts = av_rescale_q(packet->dts, src_stream->time_base, dest_stream->time_base);
packet->duration = dest_stream->time_base.den / dest_stream->time_base.num / dest_stream->avg_frame_rate.num * dest_stream->avg_frame_rate.den;
av_interleaved_write_frame(dest, packet);
The error log I get is the following, in a repeated manner:
[mp4 @ 0x7fc514001d00] Application provided duration: ... / timestamp: ... is out of range for mov/mp4 format
[mp4 @ 0x7fc514001d00] pts has no value
The libav
version I am using is the following:
ffmpeg version 4.3.3 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.2.0-8ubuntu3)
configuration:
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
P.S: Upgrading ffmpeg
version won't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我按照此处所述进行了以下修改它工作正常:
I made the following modification as mentioned here and now it works fine: