如何解决FFMPEG:使用实时时间戳信息调整编码数据包的PTS和DTS
我正在尝试使用FFMPEG库中的muxing在桌面上录制活动视频。 我引用了以下链接进行我的实施。 https://ffmpeg.org/doxygen/trunk/muxing_8c_source.html
我观察到编码视频播放得太快。下面是编码框架并将其写入输出文件的代码。
static int write_frame(AVFormatContext *fmt_ctx,AVCodecContext *c,AVStream *st,AVFrame *frame)
{
int ret;
// send the frame to the encoder
ret = avcodec_send_frame(c,frame);
if (ret < 0) {
fprintf(stderr,"Error sending a frame to the encoder: %s\n",av_err2str(ret));
exit(1);
}
while (ret >= 0) {
AVPacket pkt = { 0 };
ret = avcodec_receive_packet(c,&pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0) {
fprintf(stderr,"Error encoding a frame: %s\n",av_err2str(ret));
exit(1);
}
/* rescale output packet timestamp values from codec to stream timebase(from 25 to 90000 this case) */
av_packet_rescale_ts(&pkt,c->time_base,st->time_base);
pkt.stream_index = st->index;
/* Write the compressed frame to the media file. */
log_packet(fmt_ctx,&pkt);
ret = av_interleaved_write_frame(fmt_ctx,&pkt);
av_packet_unref(&pkt);
if (ret < 0) {
fprintf(stderr,"Error while writing output packet: %s\n",av_err2str(ret));
exit(1);
}
}
return ret == AVERROR_EOF ? 1 : 0;
}
->在写之前,我正在将输出数据包时间戳值从编解码器重新缩放到流时基(25-> 90,000)。 ->由于编码视频的播放速度非常快,因此我在av_packet_rescale_ts之前添加了临时修正,以将流时基乘以某个因子(在我的情况下为5-> 5 * st-> time_base.den,变为4,50,000),因此输出视频的持续时间会增加。这可能不是准确的解决方案,因为这不会给出帧之间的实时移位。
/** rescale output packet timestamp values from codec to stream timebase **/
st->time_base.den = 5*st->time_base.den;
av_packet_rescale_ts(pkt,*time_base,st->time_base);
pkt->stream_index = st->index;
/** Write the compressed frame to the media file. **/
return av_interleaved_write_frame(fmt_ctx,pkt);
->我希望输出的视频持续时间应该与输入的相似。我听说过诸如为帧和数据包的PTS和DTS提供实时时间戳,以及以可变或未知帧速率进行编码的事情。 我们可以使用这些,如果是,那么如何实施? 该方法的任何建议或伪代码都将真正有帮助。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。