From 80eef693c63d389107d6fdf10560a82c22ab3f62 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sun, 16 Oct 2022 16:40:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=A1=AEmp4=E5=BD=95=E5=88=B6?= =?UTF-8?q?=E6=97=B6=E9=95=BF=EF=BC=9A#1795?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Muxer.cpp | 10 ++++++++++ src/Record/MP4Muxer.h | 7 ++++++- src/Record/MP4Recorder.cpp | 17 +++++++---------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index 0b4357a6..11747126 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -61,6 +61,16 @@ bool MP4MuxerInterface::haveVideo() const { return _have_video; } +uint64_t MP4MuxerInterface::getDuration() const { + uint64_t ret = 0; + for (auto &pr : _codec_to_trackid) { + if (pr.second.stamp.getRelativeStamp() > ret) { + ret = pr.second.stamp.getRelativeStamp(); + } + } + return ret; +} + void MP4MuxerInterface::resetTracks() { _started = false; _have_video = false; diff --git a/src/Record/MP4Muxer.h b/src/Record/MP4Muxer.h index 25fd0447..877b05ff 100644 --- a/src/Record/MP4Muxer.h +++ b/src/Record/MP4Muxer.h @@ -58,6 +58,11 @@ public: */ void initSegment(); + /** + * 获取mp4时长,单位毫秒 + */ + uint64_t getDuration() const; + protected: virtual MP4FileIO::Writer createWriter() = 0; @@ -73,7 +78,7 @@ private: Stamp stamp; }; std::unordered_map _codec_to_trackid; - FrameMerger _frame_merger{FrameMerger::mp4_nal_size}; + FrameMerger _frame_merger { FrameMerger::mp4_nal_size }; }; class MP4Muxer : public MP4MuxerInterface{ diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index d527bb27..efb011fc 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -70,24 +70,21 @@ void MP4Recorder::asyncClose() { auto full_path = _full_path; auto info = _info; WorkThreadPool::Instance().getExecutor()->async([muxer, full_path_tmp, full_path, info]() mutable { - //获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间 - info.time_len = (float) (::time(NULL) - info.start_time); - //关闭mp4非常耗时,所以要放在后台线程执行 + info.time_len = muxer->getDuration() / 1000.0f; + // 关闭mp4可能非常耗时,所以要放在后台线程执行 muxer->closeMP4(); - - if(!full_path_tmp.empty()) { - //获取文件大小 + if (!full_path_tmp.empty()) { + // 获取文件大小 info.file_size = File::fileSize(full_path_tmp.data()); if (info.file_size < 1024) { - //录像文件太小,删除之 + // 录像文件太小,删除之 File::delete_file(full_path_tmp.data()); return; } - //临时文件名改成正式文件名,防止mp4未完成时被访问 + // 临时文件名改成正式文件名,防止mp4未完成时被访问 rename(full_path_tmp.data(), full_path.data()); } - - /////record 业务逻辑////// + //触发mp4录制切片生成事件 NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4, info); }); }