From a0f1e11208b0654ebe7b8e741e158626f614b945 Mon Sep 17 00:00:00 2001 From: renlu Date: Thu, 15 Sep 2022 17:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=E7=94=9F=E6=88=90m3u8?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=83=A8=E5=88=86=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/HlsMakerSub.cpp | 49 +++++++++++++++++++------------------- src/Record/HlsMakerSub.h | 1 - 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/Record/HlsMakerSub.cpp b/src/Record/HlsMakerSub.cpp index 7825eb4d..ba114f72 100644 --- a/src/Record/HlsMakerSub.cpp +++ b/src/Record/HlsMakerSub.cpp @@ -10,6 +10,14 @@ #include "Util/File.h" #include "HlsMakerSub.h" +#if defined(_WIN32) +#include +#define _access access +#else +#include +#endif // WIN32 + + using namespace std; using namespace toolkit; @@ -188,14 +196,6 @@ void HlsMakerSub::clear() { _last_file_name.clear(); } -std::string HlsMakerSub::getM3u8TSDuration(const std::string &file_content) { - - size_t begin = file_content.find("#EXT-X-TARGETDURATION:"); - size_t end = file_content.find("#EXT-X-MEDIA-SEQUENCE"); - string duration = file_content.substr(begin, end - begin); - return duration; -} - std::string HlsMakerSub::getM3u8TSBody(const std::string &file_content) { string new_file = file_content; @@ -232,13 +232,13 @@ std::string HlsMakerSub::getTsFile(const std::string &file_content) { void HlsMakerSub::createM3u8FileForRecord() { // 1.读取直播目录下的m3u8文件,获取当前的ts文件以及时长 - string file_str = File::loadFile((getPathPrefix() + "/hls.m3u8").data()); - if (file_str.empty()) { + string live_file = File::loadFile((getPathPrefix() + "/hls.m3u8").data()); + if (live_file.empty()) { return; } - string duration = getM3u8TSDuration(file_str); - string body = getM3u8TSBody(file_str); - string ts_file_name = getTsFile(file_str); //ts_file: 2022-09-14_11-06-03 + + string body = getM3u8TSBody(live_file); + string ts_file_name = getTsFile(live_file); // ts_file: 2022-09-14_11-06-03 string m3u8_file = getPathPrefix() + "/" + ts_file_name.substr(0, 10) + "/" + ts_file_name.substr(11, 2) + "/"; @@ -271,25 +271,24 @@ void HlsMakerSub::createM3u8FileForRecord() { //3.写m3u8文件 string m3u8Header = "#EXTM3U\n" "#EXT-X-PLAYLIST-TYPE:EVENT\n" - "#EXT-X-VERSION:4\n"; - file_str = File::loadFile(_m3u8_file_path.data()); - //第一次进来,是空文件。 - if (file_str == "") { + "#EXT-X-VERSION:4\n" + "#EXT-X-TARGETDURATION:2\n" + "#EXT-X-MEDIA-SEQUENCE:0\n"; + if (access(_m3u8_file_path.data(), 0) != 0) { //文件不存在 auto file = File::create_file(_m3u8_file_path.data(), "wb"); if (file) { fwrite(m3u8Header.data(), m3u8Header.size(), 1, file); - fwrite(duration.data(), duration.size(), 1, file); - fwrite("#EXT-X-MEDIA-SEQUENCE:0\n", string("#EXT-X-MEDIA-SEQUENCE:0\n").size(), 1, file); fwrite(body.data(), body.size(), 1, file); fclose(file); } } else { - //第二次进来,读取文件的内容,修改duration,追加body,保存文件 - string old_duration = getM3u8TSDuration(file_str); - replace(file_str, old_duration, duration); - string new_str = file_str.substr(0, file_str.length() - 15); - new_str.append(body); - File::saveFile(new_str, _m3u8_file_path.data()); + // 第二次进来,去掉 "#EXT-X-ENDLIST\n",再重新追加file_content,保存文件 + auto file = File::create_file(_m3u8_file_path.data(), "r+"); + if (file) { + fseek(file, -15, SEEK_END); + fwrite(body.data(), body.size(), 1, file); + fclose(file); + } } } diff --git a/src/Record/HlsMakerSub.h b/src/Record/HlsMakerSub.h index 5c6491b2..38ce3002 100644 --- a/src/Record/HlsMakerSub.h +++ b/src/Record/HlsMakerSub.h @@ -116,7 +116,6 @@ private: //新增函数,实现录像功能 std::string getTsFile(const std::string &file_content); - std::string getM3u8TSDuration(const std::string &file_content); std::string getM3u8TSBody(const std::string &file_content); void createM3u8FileForRecord();