From 57021a68d94b3f48cda7f567d645ae9425d45101 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 28 May 2023 10:43:36 +0200 Subject: [PATCH] fftools/ffmpeg_mux: set stream duration after the timebase is certainly known Stop assuming the encoder knows the muxing timebase, which does not always have to hold (e.g. due to bitstream filters). --- fftools/ffmpeg_enc.c | 4 ---- fftools/ffmpeg_mux.c | 5 +++++ fftools/ffmpeg_mux.h | 3 +++ fftools/ffmpeg_mux_init.c | 10 ++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index f3e291a9e4..fab71ca0c0 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -445,10 +445,6 @@ int enc_open(OutputStream *ost, AVFrame *frame) if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1}); - // copy estimated duration as a hint to the muxer - if (ost->st->duration <= 0 && ist && ist->st->duration > 0) - ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base); - ost->mux_timebase = enc_ctx->time_base; ret = of_stream_init(of, ost); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index feb014ca31..121796a55a 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -666,6 +666,11 @@ int of_stream_init(OutputFile *of, OutputStream *ost) if (ret < 0) return ret; + if (ms->stream_duration) { + ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb, + ost->st->time_base); + } + ost->initialized = 1; return mux_check_init(mux); diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h index 7e0454dfba..bee7addd6a 100644 --- a/fftools/ffmpeg_mux.h +++ b/fftools/ffmpeg_mux.h @@ -69,6 +69,9 @@ typedef struct MuxStream { * used for making up missing dts values */ int64_t last_mux_dts; + int64_t stream_duration; + AVRational stream_duration_tb; + // audio streamcopy - state for av_rescale_delta() int64_t ts_rescale_delta_last; diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 4fc6f0fb46..33c93e40f2 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -914,10 +914,6 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost) ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1}); } - // copy estimated duration as a hint to the muxer - if (ost->st->duration <= 0 && ist->st->duration > 0) - ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base); - if (!ms->copy_prior_start) { ms->ts_copy_start = (mux->of.start_time == AV_NOPTS_VALUE) ? 0 : mux->of.start_time; @@ -1283,6 +1279,12 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, exit_program(1); } + // copy estimated duration as a hint to the muxer + if (ost->ist && ost->ist->st->duration > 0) { + ms->stream_duration = ist->st->duration; + ms->stream_duration_tb = ist->st->time_base; + } + return ost; }