The `dec_ctx->time_base` was incorrectly default set by avcodec_open2(), while 
`enc_ctx->time_base` was derived from `dec_ctx->framerate`. This mismatch could 
cause incorrect video duration in the output.

This patch corrects the issue by adjusting the `enc_ctx->time_base` calculation 
to account for `ticks_per_frame`, ensuring that the time base is consistent 
between the decoder and encoder contexts.

--- Begin Message ---
From 6a02fbaf6c6068040640ff105ad70115fb81b5d2 Mon Sep 17 00:00:00 2001
From: Jack Lau <jacklau122...@gmail.com>
Date: Tue, 4 Feb 2025 21:39:20 +0800
Subject: [PATCH] examples/transcoding: Fix time_base handling
X-Unsent: 1
To: ffmpeg-devel@ffmpeg.org

The `dec_ctx->time_base` was incorrectly default set by avcodec_open2(), while 
`enc_ctx->time_base` was derived from `dec_ctx->framerate`. This mismatch could 
cause incorrect video duration in the output.

This patch corrects the issue by adjusting the `enc_ctx->time_base` calculation 
to account for `ticks_per_frame`, ensuring that the time base is consistent 
between the decoder and encoder contexts.

Signed-off-by: Jack Lau <jacklau1...@qq.com>
---
 doc/examples/transcoding.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/doc/examples/transcoding.c b/doc/examples/transcoding.c
index 013f89fc7d..8cc1991267 100644
--- a/doc/examples/transcoding.c
+++ b/doc/examples/transcoding.c
@@ -172,7 +172,7 @@ static int open_output_file(const char *filename)
                 else
                     enc_ctx->pix_fmt = dec_ctx->pix_fmt;
                 /* video time_base can be set to whatever is handy and 
supported by encoder */
-                enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
+                enc_ctx->time_base = av_inv_q(av_mul_q(dec_ctx->framerate, 
(AVRational){dec_ctx->ticks_per_frame, 1}));
             } else {
                 enc_ctx->sample_rate = dec_ctx->sample_rate;
                 ret = av_channel_layout_copy(&enc_ctx->ch_layout, 
&dec_ctx->ch_layout);
@@ -180,7 +180,7 @@ static int open_output_file(const char *filename)
                     return ret;
                 /* take first format from list of supported formats */
                 enc_ctx->sample_fmt = encoder->sample_fmts[0];
-                enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
+                enc_ctx->time_base = (AVRational){1,dec_ctx->sample_rate};
             }
 
             if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
-- 
2.48.1


--- End Message ---
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to