精简生成m3u8文件部分的代码

This commit is contained in:
renlu 2022-09-15 17:15:33 +08:00
parent e5f43df67b
commit a0f1e11208
2 changed files with 24 additions and 26 deletions

View File

@ -10,6 +10,14 @@
#include "Util/File.h" #include "Util/File.h"
#include "HlsMakerSub.h" #include "HlsMakerSub.h"
#if defined(_WIN32)
#include <io.h>
#define _access access
#else
#include <unistd.h>
#endif // WIN32
using namespace std; using namespace std;
using namespace toolkit; using namespace toolkit;
@ -188,14 +196,6 @@ void HlsMakerSub::clear() {
_last_file_name.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) { std::string HlsMakerSub::getM3u8TSBody(const std::string &file_content) {
string new_file = file_content; string new_file = file_content;
@ -232,13 +232,13 @@ std::string HlsMakerSub::getTsFile(const std::string &file_content) {
void HlsMakerSub::createM3u8FileForRecord() { void HlsMakerSub::createM3u8FileForRecord() {
// 1.读取直播目录下的m3u8文件获取当前的ts文件以及时长 // 1.读取直播目录下的m3u8文件获取当前的ts文件以及时长
string file_str = File::loadFile((getPathPrefix() + "/hls.m3u8").data()); string live_file = File::loadFile((getPathPrefix() + "/hls.m3u8").data());
if (file_str.empty()) { if (live_file.empty()) {
return; return;
} }
string duration = getM3u8TSDuration(file_str);
string body = getM3u8TSBody(file_str); string body = getM3u8TSBody(live_file);
string ts_file_name = getTsFile(file_str); //ts_file: 2022-09-14_11-06-03 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) + "/"; 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文件 //3.写m3u8文件
string m3u8Header = "#EXTM3U\n" string m3u8Header = "#EXTM3U\n"
"#EXT-X-PLAYLIST-TYPE:EVENT\n" "#EXT-X-PLAYLIST-TYPE:EVENT\n"
"#EXT-X-VERSION:4\n"; "#EXT-X-VERSION:4\n"
file_str = File::loadFile(_m3u8_file_path.data()); "#EXT-X-TARGETDURATION:2\n"
//第一次进来,是空文件。 "#EXT-X-MEDIA-SEQUENCE:0\n";
if (file_str == "") { if (access(_m3u8_file_path.data(), 0) != 0) { //文件不存在
auto file = File::create_file(_m3u8_file_path.data(), "wb"); auto file = File::create_file(_m3u8_file_path.data(), "wb");
if (file) { if (file) {
fwrite(m3u8Header.data(), m3u8Header.size(), 1, 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); fwrite(body.data(), body.size(), 1, file);
fclose(file); fclose(file);
} }
} else { } else {
//第二次进来读取文件的内容修改duration追加body保存文件 // 第二次进来,去掉 "#EXT-X-ENDLIST\n"再重新追加file_content保存文件
string old_duration = getM3u8TSDuration(file_str); auto file = File::create_file(_m3u8_file_path.data(), "r+");
replace(file_str, old_duration, duration); if (file) {
string new_str = file_str.substr(0, file_str.length() - 15); fseek(file, -15, SEEK_END);
new_str.append(body); fwrite(body.data(), body.size(), 1, file);
File::saveFile(new_str, _m3u8_file_path.data()); fclose(file);
}
} }
} }

View File

@ -116,7 +116,6 @@ private:
//新增函数,实现录像功能 //新增函数,实现录像功能
std::string getTsFile(const std::string &file_content); std::string getTsFile(const std::string &file_content);
std::string getM3u8TSDuration(const std::string &file_content);
std::string getM3u8TSBody(const std::string &file_content); std::string getM3u8TSBody(const std::string &file_content);
void createM3u8FileForRecord(); void createM3u8FileForRecord();