From 48fd24081781be1b27d001d47701602bda95c0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E8=A1=B2=E4=B8=8D=E5=87=BA=E5=AE=B6?= Date: Mon, 30 Jun 2025 20:46:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmp4=E5=88=87=E7=89=87?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=8F=AF=E8=83=BD=E4=B8=8D=E5=87=86=E7=9A=84?= =?UTF-8?q?bug=20(#4332)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Recorder.cpp | 40 +++++++++++++------------------------- src/Record/MP4Recorder.h | 2 +- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index 31732043..0b438dd2 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -117,33 +117,19 @@ void MP4Recorder::flush() { } bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { - if (!(_have_video && frame->getTrackType() == TrackAudio)) { - // 如果有视频且输入的是音频,那么应该忽略切片逻辑 [AUTO-TRANSLATED:fbb15d93] - // If there is video and the input is audio, then the slice logic should be ignored - if (_last_dts == 0) { - // first frame assign dts - _last_dts = frame->dts(); - } else if (_last_dts > frame->dts()) { - // b帧情况下dts时间戳可能回退 [AUTO-TRANSLATED:1de38f77] - // In the case of b-frames, the dts timestamp may regress - _last_dts = MIN(frame->dts(), _last_dts); - } - - auto duration = 5u; // 默认至少一帧5ms - if (frame->dts() > 0 && frame->dts() > _last_dts) { - duration = MAX(duration, frame->dts() - _last_dts); - } - if (!_muxer || ((duration > _max_second * 1000) && (!_have_video || (_have_video && frame->keyFrame())))) { - // 成立条件 [AUTO-TRANSLATED:8c9c6083] - // Conditions for establishment - // 1、_muxer为空 [AUTO-TRANSLATED:fa236097] - // 1. _muxer is empty - // 2、到了切片时间,并且只有音频 [AUTO-TRANSLATED:212e9d23] - // 2. It's time to slice, and there is only audio - // 3、到了切片时间,有视频并且遇到视频的关键帧 [AUTO-TRANSLATED:fa4a71ad] - // 3. It's time to slice, there is video and a video keyframe is encountered - _last_dts = 0; - createFile(); + auto stamp_inc = _delta_stamp[frame->getTrackType()].relativeStamp(frame->pts(), false); + if (!_muxer || (stamp_inc > int64_t(_max_second) * 1000 && (!_have_video || frame->keyFrame()))) { + // 成立条件 [AUTO-TRANSLATED:8c9c6083] + // Conditions for establishment + // 1、_muxer为空 [AUTO-TRANSLATED:fa236097] + // 1. _muxer is empty + // 2、到了切片时间,并且只有音频 [AUTO-TRANSLATED:212e9d23] + // 2. It's time to slice, and there is only audio + // 3、到了切片时间,有视频并且遇到视频的关键帧 [AUTO-TRANSLATED:fa4a71ad] + // 3. It's time to slice, there is video and a video keyframe is encountered + createFile(); + for (auto &ref : _delta_stamp) { + ref.reset(); } } diff --git a/src/Record/MP4Recorder.h b/src/Record/MP4Recorder.h index 8faec96b..c12585d4 100644 --- a/src/Record/MP4Recorder.h +++ b/src/Record/MP4Recorder.h @@ -70,7 +70,7 @@ private: private: bool _have_video = false; size_t _max_second; - uint64_t _last_dts = 0; + DeltaStamp _delta_stamp[TrackMax]; std::atomic _file_index { 0 }; std::string _full_path_tmp; RecordInfo _info;