v2.0.t8
Adjusted the event loop and motion detection to make better stand alone m3u8 files. Hopefully doing this will make browsers treat the recorded events as VODs instead of streams.
This commit is contained in:
parent
3c5dbec24c
commit
baa69da2cd
12
src/common.h
12
src/common.h
|
@ -28,6 +28,7 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <map>
|
||||
|
||||
#include <opencv4/opencv2/opencv.hpp>
|
||||
#include <opencv4/opencv2/videoio.hpp>
|
||||
|
@ -37,7 +38,7 @@ using namespace std;
|
|||
using namespace std::filesystem;
|
||||
using namespace std::chrono;
|
||||
|
||||
#define APP_VER "2.0.t7"
|
||||
#define APP_VER "2.0.t8"
|
||||
#define APP_NAME "Motion Watch"
|
||||
#define REC_LOG_NAME "rec_log_lines.html"
|
||||
#define DET_LOG_NAME "det_log_lines.html"
|
||||
|
@ -45,17 +46,18 @@ using namespace std::chrono;
|
|||
|
||||
struct pls_t
|
||||
{
|
||||
string fileName;
|
||||
string clipPath;
|
||||
string evName;
|
||||
string dstPath;
|
||||
string extINF;
|
||||
vector<string> srcPaths;
|
||||
vector<string> dstPaths;
|
||||
vector<string> extINFs;
|
||||
uint64_t createTime;
|
||||
Mat thumbnail;
|
||||
};
|
||||
|
||||
struct shared_t
|
||||
{
|
||||
vector<pls_t> recList;
|
||||
map<string, pls_t> recList;
|
||||
string conf;
|
||||
string recLog;
|
||||
string detLog;
|
||||
|
|
26
src/main.cpp
26
src/main.cpp
|
@ -27,27 +27,28 @@ void eventLoop(shared_t *share)
|
|||
{
|
||||
while (share->retCode == 0)
|
||||
{
|
||||
while (!share->recList.empty())
|
||||
for (auto it = share->recList.begin(); !share->recList.empty();)
|
||||
{
|
||||
auto pls = share->recList[0];
|
||||
auto timeDiff = pls.createTime - genEpoch();
|
||||
auto evName = it->first;
|
||||
auto event = it->second;
|
||||
auto timeDiff = genEpoch() - event.createTime;
|
||||
|
||||
// future time protection. this should never occur but
|
||||
// this is in place in case the system time is incorrect.
|
||||
if (timeDiff < 0) timeDiff = 0;
|
||||
|
||||
// wait at least 6 seconds before processing the event in
|
||||
if (timeDiff > 0)
|
||||
{
|
||||
// wait at least 61 seconds before processing the event in
|
||||
// queue.
|
||||
if (timeDiff < 6) sleep(6 - timeDiff);
|
||||
if (timeDiff < 61) sleep(61 - timeDiff);
|
||||
|
||||
try
|
||||
{
|
||||
wrOutm3u8(pls, share);
|
||||
genHTMLvid(pls.fileName);
|
||||
wrOutm3u8(event, share);
|
||||
genHTMLvid(evName);
|
||||
|
||||
if (!exists(pls.fileName + ".jpg"))
|
||||
if (!exists(evName + ".jpg"))
|
||||
{
|
||||
imwrite(string(pls.fileName + ".jpg").c_str(), pls.thumbnail);
|
||||
imwrite(string(evName + ".jpg").c_str(), event.thumbnail);
|
||||
}
|
||||
}
|
||||
catch (filesystem_error &ex)
|
||||
|
@ -55,7 +56,8 @@ void eventLoop(shared_t *share)
|
|||
recLog(string("err: ") + ex.what(), share);
|
||||
}
|
||||
|
||||
share->recList.erase(share->recList.begin());
|
||||
share->recList.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
sleep(5);
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
void detectMoInStream(const string &bufPath, shared_t *share)
|
||||
{
|
||||
ifstream fileIn(bufPath);
|
||||
pls_t clip;
|
||||
string tsPath;
|
||||
string extINF;
|
||||
Mat thumbnail;
|
||||
|
||||
auto clipPathFilter = genTimeStr("VIDEO_TS/live/%Y/%j/%H/");
|
||||
|
||||
|
@ -23,25 +25,42 @@ void detectMoInStream(const string &bufPath, shared_t *share)
|
|||
{
|
||||
if (line.starts_with(clipPathFilter))
|
||||
{
|
||||
clip.clipPath = line;
|
||||
tsPath = line;
|
||||
}
|
||||
else if (line.starts_with("#EXTINF"))
|
||||
{
|
||||
clip.extINF = line;
|
||||
extINF = line;
|
||||
}
|
||||
}
|
||||
|
||||
if (!clip.clipPath.empty() && !clip.extINF.empty())
|
||||
if (!tsPath.empty() && !extINF.empty())
|
||||
{
|
||||
if (moDetect(clip.clipPath, clip.thumbnail, share))
|
||||
if (moDetect(tsPath, thumbnail, share))
|
||||
{
|
||||
clip.fileName = genTimeStr("%Y-%j-%H-%M");
|
||||
clip.dstPath = genEventPath(clip.clipPath);
|
||||
clip.createTime = genEpoch();
|
||||
auto eventName = genTimeStr("%Y-%j-%H-%M");
|
||||
|
||||
if (share->recList.find(eventName) != share->recList.end())
|
||||
{
|
||||
share->recList[eventName].srcPaths.push_back(tsPath);
|
||||
share->recList[eventName].dstPaths.push_back(genEventPath(tsPath));
|
||||
share->recList[eventName].extINFs.push_back(extINF);
|
||||
}
|
||||
else
|
||||
{
|
||||
pls_t event;
|
||||
|
||||
event.srcPaths.push_back(tsPath);
|
||||
event.dstPaths.push_back(genEventPath(tsPath));
|
||||
event.extINFs.push_back(extINF);
|
||||
|
||||
event.createTime = genEpoch();
|
||||
event.thumbnail = thumbnail.clone();
|
||||
event.evName = eventName;
|
||||
|
||||
share->recList.insert(pair{eventName, event});
|
||||
}
|
||||
|
||||
share->skipCmd = true;
|
||||
|
||||
share->recList.push_back(clip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,38 +136,32 @@ bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share)
|
|||
return mod;
|
||||
}
|
||||
|
||||
void wrOutm3u8(const pls_t &pls, shared_t *share)
|
||||
void wrOutm3u8(const pls_t &event, shared_t *share)
|
||||
{
|
||||
recLog("live-to-event-src: " + pls.clipPath, share);
|
||||
recLog("live-to-event-dst: " + pls.dstPath, share);
|
||||
auto plsFile = event.evName + ".m3u8";
|
||||
|
||||
createDirTree(path(pls.dstPath).parent_path().string());
|
||||
copy_file(pls.clipPath, pls.dstPath);
|
||||
|
||||
auto plsFile = pls.fileName + ".m3u8";
|
||||
|
||||
if (exists(plsFile))
|
||||
{
|
||||
ofstream file(plsFile.c_str(), ofstream::app);
|
||||
|
||||
file << endl;
|
||||
file << pls.extINF << endl;
|
||||
file << pls.dstPath;
|
||||
|
||||
file.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
ofstream file(plsFile.c_str());
|
||||
|
||||
file << "#EXTM3U" << endl;
|
||||
file << "#EXT-X-VERSION:3" << endl;
|
||||
file << "#EXT-X-TARGETDURATION:10" << endl;
|
||||
file << "#EXT-X-MEDIA-SEQUENCE:0" << endl;
|
||||
file << "#EXT-X-DISCONTINUITY" << endl;
|
||||
file << pls.extINF << endl;
|
||||
file << pls.dstPath;
|
||||
file << "#EXT-X-START:TIME-OFFSET=0" << endl;
|
||||
file << "#EXT-X-PLAYLIST-TYPE:VOD" << endl;
|
||||
|
||||
for (auto i = 0; i < event.extINFs.size(); ++i)
|
||||
{
|
||||
auto src = event.srcPaths[i];
|
||||
auto dst = event.dstPaths[i];
|
||||
|
||||
createDirTree(path(dst).parent_path().string());
|
||||
copy_file(src, dst);
|
||||
|
||||
file << event.extINFs[i] << endl;
|
||||
file << dst << endl;
|
||||
}
|
||||
|
||||
file << "#EXT-X-ENDLIST" << endl;
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,11 +51,8 @@ void genHTMLul(const string &outputDir, const string &title, shared_t *share)
|
|||
htmlText += "</ul>\n";
|
||||
htmlText += "<h4>Motion Events</h4>\n";
|
||||
|
||||
if (!exists("stream.html"))
|
||||
{
|
||||
genHTMLvid("stream");
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &®Name : regNames)
|
||||
{
|
||||
|
@ -71,7 +68,7 @@ void genHTMLul(const string &outputDir, const string &title, shared_t *share)
|
|||
// regName.substr(0, regName.size() - 5) removes .html
|
||||
auto name = regName.substr(0, regName.size() - 5);
|
||||
|
||||
htmlText += "<a href='" + regName + "'><img src='" + name + ".jpg" + "' style='width:12%;height:12%;'</a>\n";
|
||||
htmlText += "<a href='" + regName + "'><img src='" + name + ".jpg" + "' style='width:25%;height:25%;'</a>\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user