From 1bd1fdcb2a2d8b356dd0516d3ea71a7e55de7118 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Wed, 23 Nov 2022 13:56:57 -0600 Subject: [PATCH] Silence audio data if repeating last frame in FFmpegReader (to prevent audio glitch for files with invalid durations) --- src/FFmpegReader.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 96cf4e06..e46f03c6 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -1057,13 +1057,26 @@ std::shared_ptr FFmpegReader::ReadStream(int64_t requested_frame) { // Check if largest frame is still cached frame = final_cache.GetFrame(largest_frame_processed); + int samples_in_frame = Frame::GetSamplesPerFrame(requested_frame, info.fps, + info.sample_rate, info.channels); if (frame) { - // return the largest processed frame (assuming it was the last in the video file) + // Copy and return the largest processed frame (assuming it was the last in the video file) + std::shared_ptr f = CreateFrame(largest_frame_processed); + + // Use solid color (if no image data found) + if (!frame->has_image_data) { + // Use solid black frame if no image data available + f->AddColor(info.width, info.height, "#000"); + } + // Silence audio data (if any), since we are repeating the last frame + frame->AddAudioSilence(samples_in_frame); + return frame; } else { // The largest processed frame is no longer in cache, return a blank frame std::shared_ptr f = CreateFrame(largest_frame_processed); f->AddColor(info.width, info.height, "#000"); + f->AddAudioSilence(samples_in_frame); return f; } }