从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?我注意到在第(7)行outStream->time_base
= 1/90000之前和之后它变为1/12800,为什么?当我从avi转换为avi时,意味着将第(2)行更改为
avformat_alloc_output_context2(&outContainer, NULL, "avi", "c:\\test.avi";
,因此在第(7)行之前和之后,outStream->time_base
始终始终为1/25,而不是像mp4一样,为什么? outStream->codec
的time_base和outStream
之间有什么区别? av_rescale_q
进行以下操作:花费2个time_base,将它们的分数相乘,然后计算pts。为什么这样做呢?正如我调试的那样,encodedPacket.pts
的值增加了1,那么为什么要更改它的值呢? 最佳答案
a/b
为单位的时间t可以转换为c/d
单位t*(a*d)/(b*c)
。