From 2422285829f840a645436deb4970e5b28fb45b57 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 20 Oct 2021 17:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9mp4=E5=BD=95=E5=88=B6?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Recorder.cpp | 80 +++++++++++++++++--------------------- src/Record/MP4Recorder.h | 23 +++++------ 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index e57ab862..8d60d69d 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -20,17 +20,13 @@ using namespace toolkit; namespace mediakit { -MP4Recorder::MP4Recorder(const string &strPath, - const string &strVhost, - const string &strApp, - const string &strStreamId, - size_t max_second) { - _strPath = strPath; +MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) { + _folder_path = path; /////record 业务逻辑////// - _info.app = strApp; - _info.stream = strStreamId; - _info.vhost = strVhost; - _info.folder = strPath; + _info.app = app; + _info.stream = stream_id; + _info.vhost = vhost; + _info.folder = path; GET_CONFIG(size_t ,recordSec,Record::kFileSecond); _max_second = max_second ? max_second : recordSec; } @@ -40,31 +36,27 @@ MP4Recorder::~MP4Recorder() { void MP4Recorder::createFile() { closeFile(); - auto strDate = getTimeStr("%Y-%m-%d"); - auto strTime = getTimeStr("%H-%M-%S"); - auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4"; - auto strFile = _strPath + strDate + "/" + strTime + ".mp4"; + auto date = getTimeStr("%Y-%m-%d"); + auto time = getTimeStr("%H-%M-%S"); + auto full_path_tmp = _folder_path + date + "/." + time + ".mp4"; + auto full_path = _folder_path + date + "/" + time + ".mp4"; /////record 业务逻辑////// _info.start_time = ::time(NULL); - _info.file_name = strTime + ".mp4"; - _info.file_path = strFile; - GET_CONFIG(string,appName,Record::kAppName); - _info.url = appName + "/" - + _info.app + "/" - + _info.stream + "/" - + strDate + "/" - + strTime + ".mp4"; + _info.file_name = time + ".mp4"; + _info.file_path = full_path; + GET_CONFIG(string, appName, Record::kAppName); + _info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + time + ".mp4"; try { _muxer = std::make_shared(); - _muxer->openMP4(strFileTmp); + _muxer->openMP4(full_path_tmp); for (auto &track :_tracks) { //添加track _muxer->addTrack(track); } - _strFileTmp = strFileTmp; - _strFile = strFile; + _full_path_tmp = full_path_tmp; + _full_path = full_path; } catch (std::exception &ex) { WarnL << ex.what(); } @@ -72,29 +64,29 @@ void MP4Recorder::createFile() { void MP4Recorder::asyncClose() { auto muxer = _muxer; - auto strFileTmp = _strFileTmp; - auto strFile = _strFile; + auto full_path_tmp = _full_path_tmp; + auto full_path = _full_path; auto info = _info; - WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() mutable{ + WorkThreadPool::Instance().getExecutor()->async([muxer, full_path_tmp, full_path, info]() mutable { //获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间 - info.time_len = (float)(::time(NULL) - info.start_time); + info.time_len = (float) (::time(NULL) - info.start_time); //关闭mp4非常耗时,所以要放在后台线程执行 muxer->closeMP4(); //获取文件大小 struct stat fileData; - stat(strFileTmp.data(), &fileData); + stat(full_path_tmp.data(), &fileData); info.file_size = fileData.st_size; if (fileData.st_size < 1024) { //录像文件太小,删除之 - File::delete_file(strFileTmp.data()); + File::delete_file(full_path_tmp.data()); return; } //临时文件名改成正式文件名,防止mp4未完成时被访问 - rename(strFileTmp.data(),strFile.data()); + rename(full_path_tmp.data(), full_path.data()); /////record 业务逻辑////// - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4, info); }); } @@ -106,17 +98,18 @@ void MP4Recorder::closeFile() { } bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { - if (_baseSec == 0) { - _baseSec = frame->dts(); + if (_last_dts == 0) { + //极少情况下dts时间戳可能回退 + _last_dts = frame->dts(); } - auto duration = frame->dts() - _baseSec; - if (!_muxer || ((duration > _max_second * 1000) && (!_haveVideo || (_haveVideo && frame->keyFrame())))) { + auto duration = frame->dts() - _last_dts; + if (!_muxer || ((duration > _max_second * 1000) && (!_have_video || (_have_video && frame->keyFrame())))) { //成立条件 //1、_muxer为空 //2、到了切片时间,并且只有音频 //3、到了切片时间,有视频并且遇到视频的关键帧 - _baseSec = 0; + _last_dts = 0; createFile(); } @@ -127,11 +120,11 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { return false; } -bool MP4Recorder::addTrack(const Track::Ptr & track){ +bool MP4Recorder::addTrack(const Track::Ptr &track) { //保存所有的track,为创建MP4MuxerFile做准备 _tracks.emplace_back(track); - if(track->getTrackType() == TrackVideo){ - _haveVideo = true; + if (track->getTrackType() == TrackVideo) { + _have_video = true; } return true; } @@ -139,10 +132,9 @@ bool MP4Recorder::addTrack(const Track::Ptr & track){ void MP4Recorder::resetTracks() { closeFile(); _tracks.clear(); - _haveVideo = false; + _have_video = false; } } /* namespace mediakit */ - -#endif //ENABLE_MP4 +#endif //ENABLE_MP4 \ No newline at end of file diff --git a/src/Record/MP4Recorder.h b/src/Record/MP4Recorder.h index 4765ca21..16937f0e 100644 --- a/src/Record/MP4Recorder.h +++ b/src/Record/MP4Recorder.h @@ -26,16 +26,12 @@ using namespace toolkit; namespace mediakit { #ifdef ENABLE_MP4 -class MP4Recorder : public MediaSinkInterface{ +class MP4Recorder : public MediaSinkInterface { public: - typedef std::shared_ptr Ptr; + using Ptr = std::shared_ptr; - MP4Recorder(const string &strPath, - const string &strVhost, - const string &strApp, - const string &strStreamId, - size_t max_second); - virtual ~MP4Recorder(); + MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second); + ~MP4Recorder() override; /** * 重置所有Track @@ -56,16 +52,17 @@ private: void createFile(); void closeFile(); void asyncClose(); + private: - bool _haveVideo = false; + bool _have_video = false; size_t _max_second; - string _strPath; - string _strFile; - string _strFileTmp; + string _folder_path; + string _full_path; + string _full_path_tmp; RecordInfo _info; MP4Muxer::Ptr _muxer; list _tracks; - uint64_t _baseSec = 0; + uint64_t _last_dts = 0; }; #endif ///ENABLE_MP4