修复mp4切片时间可能不准的bug (#4332)

This commit is contained in:
老衲不出家 2025-06-30 20:46:04 +08:00 committed by GitHub
parent f0bc7a75aa
commit 48fd240817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 28 deletions

View File

@ -117,33 +117,19 @@ void MP4Recorder::flush() {
} }
bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
if (!(_have_video && frame->getTrackType() == TrackAudio)) { auto stamp_inc = _delta_stamp[frame->getTrackType()].relativeStamp(frame->pts(), false);
// 如果有视频且输入的是音频,那么应该忽略切片逻辑 [AUTO-TRANSLATED:fbb15d93] if (!_muxer || (stamp_inc > int64_t(_max_second) * 1000 && (!_have_video || frame->keyFrame()))) {
// If there is video and the input is audio, then the slice logic should be ignored // 成立条件 [AUTO-TRANSLATED:8c9c6083]
if (_last_dts == 0) { // Conditions for establishment
// first frame assign dts // 1、_muxer为空 [AUTO-TRANSLATED:fa236097]
_last_dts = frame->dts(); // 1. _muxer is empty
} else if (_last_dts > frame->dts()) { // 2、到了切片时间并且只有音频 [AUTO-TRANSLATED:212e9d23]
// b帧情况下dts时间戳可能回退 [AUTO-TRANSLATED:1de38f77] // 2. It's time to slice, and there is only audio
// In the case of b-frames, the dts timestamp may regress // 3、到了切片时间有视频并且遇到视频的关键帧 [AUTO-TRANSLATED:fa4a71ad]
_last_dts = MIN(frame->dts(), _last_dts); // 3. It's time to slice, there is video and a video keyframe is encountered
} createFile();
for (auto &ref : _delta_stamp) {
auto duration = 5u; // 默认至少一帧5ms ref.reset();
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();
} }
} }

View File

@ -70,7 +70,7 @@ private:
private: private:
bool _have_video = false; bool _have_video = false;
size_t _max_second; size_t _max_second;
uint64_t _last_dts = 0; DeltaStamp _delta_stamp[TrackMax];
std::atomic<uint64_t> _file_index { 0 }; std::atomic<uint64_t> _file_index { 0 };
std::string _full_path_tmp; std::string _full_path_tmp;
RecordInfo _info; RecordInfo _info;