修复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,23 +117,8 @@ 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 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]
@ -142,8 +127,9 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
// 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();
for (auto &ref : _delta_stamp) {
ref.reset();
}
}

View File

@ -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<uint64_t> _file_index { 0 };
std::string _full_path_tmp;
RecordInfo _info;