diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 6f71e85658..49c07ede95 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -655,6 +655,8 @@ typedef struct FrameData { uint64_t idx; int64_t pts; AVRational tb; + + AVRational frame_rate_filter; } FrameData; extern InputFile **input_files; diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index f6431b29d1..cd2faccf4e 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -217,8 +217,10 @@ int enc_open(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_VIDEO: { AVRational fr = ost->frame_rate; - if (!fr.num) - fr = av_buffersink_get_frame_rate(ost->filter->filter); + if (!fr.num && frame) { + FrameData *fd = frame_data(frame); + fr = fd->frame_rate_filter; + } if (!fr.num && !ost->max_frame_rate.num) { fr = (AVRational){25, 1}; av_log(ost, AV_LOG_WARNING, @@ -1024,17 +1026,18 @@ static void do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame) int ret; Encoder *e = ost->enc; AVCodecContext *enc = ost->enc_ctx; - AVRational frame_rate; int64_t nb_frames, nb_frames_prev, i; double duration = 0; - AVFilterContext *filter = ost->filter->filter; - if (frame) + if (frame) { + FrameData *fd = frame_data(frame); + duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base)); - frame_rate = av_buffersink_get_frame_rate(filter); - if (duration <= 0 && frame_rate.num > 0 && frame_rate.den > 0) - duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base)); + if (duration <= 0 && + fd->frame_rate_filter.num > 0 && fd->frame_rate_filter.den > 0) + duration = 1 / (av_q2d(fd->frame_rate_filter) * av_q2d(enc->time_base)); + } video_sync_process(of, ost, frame, duration, &nb_frames, &nb_frames_prev); diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 1150f6fc65..a2b45a14b8 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -1711,6 +1711,16 @@ int reap_filters(int flush) tb.num, tb.den); } + if (ost->type == AVMEDIA_TYPE_VIDEO) { + FrameData *fd = frame_data(filtered_frame); + if (!fd) { + av_frame_unref(filtered_frame); + report_and_exit(AVERROR(ENOMEM)); + } + + fd->frame_rate_filter = av_buffersink_get_frame_rate(filter); + } + enc_frame(ost, filtered_frame); av_frame_unref(filtered_frame); }