From 851f13bd4d0dcdd6ddbea78a24ce59a962a6e473 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Sun, 3 Nov 2024 00:00:00 +0000 Subject: [PATCH] fix(libvideo2x): fixed errors in estimating the total number of frames Signed-off-by: k4yt3x --- src/libvideo2x.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index 34d3a84..375d692 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -55,9 +55,18 @@ static int process_frames( spdlog::debug("Read total number of frames from 'nb_frames': {}", proc_ctx->total_frames); } else { spdlog::warn("Estimating the total number of frames from duration * fps"); - // Calculate duration in seconds - double duration_secs = static_cast(ifmt_ctx->streams[vstream_idx]->duration) * - av_q2d(ifmt_ctx->streams[vstream_idx]->time_base); + + // Get the duration of the video + double duration_secs = 0.0; + if (ifmt_ctx->duration != AV_NOPTS_VALUE) { + duration_secs = + static_cast(ifmt_ctx->duration) / static_cast(AV_TIME_BASE); + } else if (ifmt_ctx->streams[vstream_idx]->duration != AV_NOPTS_VALUE) { + duration_secs = static_cast(ifmt_ctx->streams[vstream_idx]->duration) * + av_q2d(ifmt_ctx->streams[vstream_idx]->time_base); + } else { + spdlog::warn("Unable to determine video duration"); + } spdlog::debug("Video duration: {}s", duration_secs); // Calculate average FPS @@ -74,8 +83,8 @@ static int process_frames( spdlog::debug("Unable to estimate the average frame rate with 'av_guess_frame_rate'"); fps = av_q2d(ifmt_ctx->streams[vstream_idx]->time_base); } - if (fps <= 0) { - spdlog::debug("Unable to estimate the video's average frame rate"); + if (fps <= 0 || duration_secs <= 0) { + spdlog::warn("Unable to estimate the video's average frame rate"); } else { // Calculate total frames proc_ctx->total_frames = static_cast(duration_secs * fps); @@ -83,7 +92,7 @@ static int process_frames( } // Check if the total number of frames is still 0 - if (proc_ctx->total_frames == 0) { + if (proc_ctx->total_frames <= 0) { spdlog::warn("Unable to determine the total number of frames"); } else { spdlog::debug("{} frames to process", proc_ctx->total_frames);