使用libav转码视频时发现一个问题:使用下面这段代码解码视频时,视频尾巴上会丢掉几帧。
while(av_read_frame(ifmt_ctx,&packet) >= 0){ ret = avcodec_decode_video2(video_dec_ctx, vframe, &got_frame, &packet); if (got_frame) { packet.pts = av_rescale_q(packet.pts,video_dec_st->time_base,video_enc_st->time_base); write_video_frame(ofmt_ctx,video_enc_st,vframe); }}
int skipped_frame = 0;while(av_read_frame(ifmt_ctx,&packet) >= 0){ ret = avcodec_decode_video2(video_dec_ctx, vframe, &got_frame, &packet); if (got_frame) { packet.pts = av_rescale_q(packet.pts,video_dec_st->time_base,video_enc_st->time_base); write_video_frame(ofmt_ctx,video_enc_st,vframe); } else { skipped_frame++; }}for(int i=skipped_frame; i>0; i--){ ret = avcodec_decode_video2(video_dec_ctx, vframe, &got_frame, &packet); if (got_frame) { packet.pts = av_rescale_q(packet.pts,video_dec_st->time_base,video_enc_st->time_base); write_video_frame(ofmt_ctx,video_enc_st,vframe); }}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow