在从avi转码到mp4(x264)时,我遇到了fps问题。最终发现问题出在PTS和DTS值上,因此在调用av_interleaved_write_frame函数之前添加了12-15行代码:
1. AVFormatContext* outContainer = NULL;
2. avformat_alloc_output_context2(&outContainer, NULL, "mp4", "c:\\test.mp4";
3. AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
4. AVStream *outStream = avformat_new_stream(outContainer, encoder);
5. // outStream->codec initiation
6. // ...
7. avformat_write_header(outContainer, NULL);
8. // reading and decoding packet
9. // ...
10. avcodec_encode_video2(outStream->codec, &encodedPacket, decodedFrame, &got_frame)
11.
12. if (encodedPacket.pts != AV_NOPTS_VALUE)
13. encodedPacket.pts = av_rescale_q(encodedPacket.pts, outStream->codec->time_base, outStream->time_base);
14. if (encodedPacket.dts != AV_NOPTS_VALUE)
15. encodedPacket.dts = av_rescale_q(encodedPacket.dts, outStream->codec->time_base, outStream->time_base);
16.
17. av_interleaved_write_frame(outContainer, &encodedPacket)
阅读了很多帖子后,我仍然不理解:
outStream->codec->time_base
= 1/25 而outStream->time_base
= 1/12800。第一个是我设置的,但我无法弄清楚为什么以及谁设置了12800?我注意到在第七行之前,outStream->time_base
= 1/90000,在它之后它改变为1/12800,为什么? 当我从avi转码到avi时,也就是将第2行更改为avformat_alloc_output_context2(&outContainer, NULL, "avi", "c:\\test.avi";
,因此在第七行之前和之后,outStream->time_base
始终保持为1/25,而不像mp4的情况,为什么?outStream->codec
和outStream
的time_base有什么区别?- 为了计算pts,
av_rescale_q
会:取两个time_base,将它们的分数交叉相乘,然后计算pts。为什么要这样做?当我进行调试时,encodedPacket.pts
的值递增1,所以如果它有值,为什么要更改它? - 在开始时,dts值为-2,每次重新缩放后仍然有负数,但尽管如此视频播放正常!它不应该是正数吗?