使用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); } }