@@ -870,12 +870,7 @@ void FFmpegWriter::flush_encoders() {
870870 avcodec_flush_buffers (video_codec_ctx);
871871 break ;
872872 }
873- if (pkt.pts != AV_NOPTS_VALUE)
874- pkt.pts = av_rescale_q (pkt.pts , video_codec_ctx->time_base , video_st->time_base );
875- if (pkt.dts != AV_NOPTS_VALUE)
876- pkt.dts = av_rescale_q (pkt.dts , video_codec_ctx->time_base , video_st->time_base );
877- if (pkt.duration > 0 )
878- pkt.duration = av_rescale_q (pkt.duration , video_codec_ctx->time_base , video_st->time_base );
873+ av_packet_rescale_ts (&pkt, video_codec_ctx->time_base , video_st->time_base );
879874 pkt.stream_index = video_st->index ;
880875 error_code = av_interleaved_write_frame (oc, &pkt);
881876 }
@@ -894,12 +889,7 @@ void FFmpegWriter::flush_encoders() {
894889 }
895890
896891 // set the timestamp
897- if (pkt.pts != AV_NOPTS_VALUE)
898- pkt.pts = av_rescale_q (pkt.pts , video_codec_ctx->time_base , video_st->time_base );
899- if (pkt.dts != AV_NOPTS_VALUE)
900- pkt.dts = av_rescale_q (pkt.dts , video_codec_ctx->time_base , video_st->time_base );
901- if (pkt.duration > 0 )
902- pkt.duration = av_rescale_q (pkt.duration , video_codec_ctx->time_base , video_st->time_base );
892+ av_packet_rescale_ts (&pkt, video_codec_ctx->time_base , video_st->time_base );
903893 pkt.stream_index = video_st->index ;
904894
905895 // Write packet
@@ -940,12 +930,7 @@ void FFmpegWriter::flush_encoders() {
940930 pkt.pts = pkt.dts = write_audio_count;
941931
942932 // Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
943- if (pkt.pts != AV_NOPTS_VALUE)
944- pkt.pts = av_rescale_q (pkt.pts , audio_codec_ctx->time_base , audio_st->time_base );
945- if (pkt.dts != AV_NOPTS_VALUE)
946- pkt.dts = av_rescale_q (pkt.dts , audio_codec_ctx->time_base , audio_st->time_base );
947- if (pkt.duration > 0 )
948- pkt.duration = av_rescale_q (pkt.duration , audio_codec_ctx->time_base , audio_st->time_base );
933+ av_packet_rescale_ts (&pkt, audio_codec_ctx->time_base , audio_st->time_base );
949934
950935 // set stream
951936 pkt.stream_index = audio_st->index ;
@@ -1887,12 +1872,7 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
18871872 pkt.pts = pkt.dts = write_audio_count;
18881873
18891874 // Scale the PTS to the audio stream timebase (which is sometimes different than the codec's timebase)
1890- if (pkt.pts != AV_NOPTS_VALUE)
1891- pkt.pts = av_rescale_q (pkt.pts , audio_codec_ctx->time_base , audio_st->time_base );
1892- if (pkt.dts != AV_NOPTS_VALUE)
1893- pkt.dts = av_rescale_q (pkt.dts , audio_codec_ctx->time_base , audio_st->time_base );
1894- if (pkt.duration > 0 )
1895- pkt.duration = av_rescale_q (pkt.duration , audio_codec_ctx->time_base , audio_st->time_base );
1875+ av_packet_rescale_ts (&pkt, audio_codec_ctx->time_base , audio_st->time_base );
18961876
18971877 // set stream
18981878 pkt.stream_index = audio_st->index ;
@@ -2144,12 +2124,7 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
21442124 /* if zero size, it means the image was buffered */
21452125 if (error_code == 0 && got_packet_ptr) {
21462126 // set the timestamp
2147- if (pkt.pts != AV_NOPTS_VALUE)
2148- pkt.pts = av_rescale_q (pkt.pts , video_codec_ctx->time_base , video_st->time_base );
2149- if (pkt.dts != AV_NOPTS_VALUE)
2150- pkt.dts = av_rescale_q (pkt.dts , video_codec_ctx->time_base , video_st->time_base );
2151- if (pkt.duration > 0 )
2152- pkt.duration = av_rescale_q (pkt.duration , video_codec_ctx->time_base , video_st->time_base );
2127+ av_packet_rescale_ts (&pkt, video_codec_ctx->time_base , video_st->time_base );
21532128 pkt.stream_index = video_st->index ;
21542129
21552130 /* write the compressed frame in the media file */
0 commit comments