// This file is part of Motion Watch. // Motion Watch is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Motion Watch is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. #include "mo_detect.h" void detectMoInStream(const string &bufPath, shared_t *share) { ifstream fileIn(bufPath); string tsPath; string extINF; Mat thumbnail; auto clipPathFilter = genTimeStr("VIDEO_TS/live/%Y/%j/%H/"); for (string line; getline(fileIn, line); ) { if (line.starts_with(clipPathFilter)) { tsPath = line; } else if (line.starts_with("#EXTINF")) { extINF = line; } } if (!tsPath.empty() && !extINF.empty()) { if (moDetect(tsPath, thumbnail, share)) { 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; } } } bool imgDiff(Mat &prev, Mat &next, shared_t *share) { auto ret = false; cvtColor(prev, prev, COLOR_BGR2GRAY); cvtColor(next, next, COLOR_BGR2GRAY); Mat diff; absdiff(prev, next, diff); threshold(diff, diff, share->pixThresh, 255, THRESH_BINARY); auto diffScore = countNonZero(diff); detLog("diff_score: " + to_string(diffScore), share); return diffScore >= share->imgThresh; } bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) { auto mod = false; detLog("stream_clip: " + buffFile, share); VideoCapture capture(buffFile.c_str(), CAP_FFMPEG); if (capture.isOpened()) { Mat prev; Mat next; detLog("capture open successful.", share); while (capture.grab()) { if (prev.empty()) { capture.retrieve(prev); } else { capture.retrieve(next); if (!share->postCmdRunning) { if (imgDiff(prev, next, share)) { resize(next, vidThumb, Size(720, 480), INTER_LINEAR); mod = true; break; } } prev.release(); next.release(); } sleep(1); } } else { detLog("capture open failure, check debug output.", share); } capture.release(); return mod; } void wrOutm3u8(const pls_t &event, shared_t *share) { auto plsFile = event.evName + ".m3u8"; 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-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(); }