From 006c06027bc00586a3cc4d8823e8c1c52ec328a1 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 16 Jul 2025 00:28:15 +0800 Subject: [PATCH] loggerd: fix audio truncation by processing remaining partial frames (#35718) fix audio truncation by processing remaining partial frames --- system/loggerd/video_writer.cc | 13 +++++++++++++ system/loggerd/video_writer.h | 1 + 2 files changed, 14 insertions(+) diff --git a/system/loggerd/video_writer.cc b/system/loggerd/video_writer.cc index cf98459490..1b47a8fceb 100644 --- a/system/loggerd/video_writer.cc +++ b/system/loggerd/video_writer.cc @@ -198,10 +198,23 @@ void VideoWriter::encode_and_write_audio_frame(AVFrame* frame) { audio_pts += audio_codec_ctx->frame_size; } +void VideoWriter::process_remaining_audio() { + // Process remaining audio samples by padding with silence + if (audio_buffer.size() > 0 && audio_buffer.size() < audio_codec_ctx->frame_size) { + audio_buffer.resize(audio_codec_ctx->frame_size, 0.0f); + + // Encode final frame + audio_frame->pts = audio_pts; + float *f_samples = reinterpret_cast(audio_frame->data[0]); + std::copy(audio_buffer.begin(), audio_buffer.end(), f_samples); + encode_and_write_audio_frame(audio_frame); + } +} VideoWriter::~VideoWriter() { if (this->remuxing) { if (this->audio_codec_ctx) { + process_remaining_audio(); encode_and_write_audio_frame(NULL); // flush encoder avcodec_free_context(&this->audio_codec_ctx); } diff --git a/system/loggerd/video_writer.h b/system/loggerd/video_writer.h index 25e6484d58..e973c5d811 100644 --- a/system/loggerd/video_writer.h +++ b/system/loggerd/video_writer.h @@ -21,6 +21,7 @@ public: private: void initialize_audio(int sample_rate); void encode_and_write_audio_frame(AVFrame* frame); + void process_remaining_audio(); std::string vid_path, lock_path; FILE *of = nullptr;