[FFmpeg-devel] [PATCH] examples/transcoding: Fix time_base handling

Jack Lau jacklau1222gm at gmail.com
Tue Feb 4 17:13:53 EET 2025


The `dec_ctx->time_base` was incorrectly default set to 0/60, while
`enc_ctx->time_base` was derived from `dec_ctx->framerate`. This mismatch
could cause incorrect video duration in the output.

This patch aligns `enc_ctx->time_base` with `dec_ctx->time_base` to prevent
rescaling issues and ensure correct video duration.

Signed-off-by: Jack Lau <jacklau1222gm at gmail.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..847bdb7e1a 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 = dec_ctx->time_base;
        } 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 = dec_ctx->time_base;
        }

        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
-- 
2.45.2


More information about the ffmpeg-devel mailing list