From ddb814aeaa02f060d72a9a6bc7ed1f91f5817ba9 Mon Sep 17 00:00:00 2001 From: Moe Date: Mon, 21 Jun 2021 16:52:07 -0700 Subject: [PATCH] Make Notification Video Length apply to new video extract method --- libs/events/utils.js | 16 +++++++++++++++- libs/video/utils.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libs/events/utils.js b/libs/events/utils.js index b2f99500..3cdc1813 100644 --- a/libs/events/utils.js +++ b/libs/events/utils.js @@ -18,6 +18,9 @@ module.exports = (s,config,lang,app,io) => { const { moveCameraPtzToMatrix } = require('../control/ptz.js')(s,config,lang) + const { + cutVideoLength + } = require('../video/utils.js')(s,config,lang) async function saveImageFromEvent(options,frameBuffer){ const monitorId = options.mid || options.id const groupKey = options.ke @@ -425,13 +428,24 @@ module.exports = (s,config,lang,app,io) => { if(activeMonitor && activeMonitor.eventBasedRecording && activeMonitor.eventBasedRecording.process){ const eventBasedRecording = activeMonitor.eventBasedRecording const monitorConfig = s.group[groupKey].rawMonitorConfigurations[monitorId] + const videoLength = monitorConfig.details.detector_send_video_length const recordingDirectory = s.getVideoDirectory(monitorConfig) const fileTime = eventBasedRecording.lastFileTime const filename = `${fileTime}.mp4` response.filename = `${filename}` response.filePath = `${recordingDirectory}${filename}` eventBasedRecording.process.on('close',function(){ - setTimeout(() => { + setTimeout(async () => { + if(!isNaN(videoLength)){ + const cutResponse = await cutVideoLength({ + ke: groupKey, + mid: monitorId, + filePath: response.filePath, + cutLength: parseInt(videoLength), + }) + response.filename = cutResponse.filename + response.filePath = cutResponse.filePath + } resolve(response) },1000) }) diff --git a/libs/video/utils.js b/libs/video/utils.js index 5064d324..c36d21c5 100644 --- a/libs/video/utils.js +++ b/libs/video/utils.js @@ -176,8 +176,42 @@ module.exports = (s,config,lang) => { finish() } } + function cutVideoLength(options){ + return new Promise((resolve,reject) => { + const response = {ok: false} + const inputFilePath = options.filePath + const monitorId = options.mid + const groupKey = options.ke + const cutLength = options.cutLength || 10 + const tempDirectory = s.getStreamsDirectory(options) + let fileExt = inputFilePath.split('.') + fileExt = fileExt[fileExt.length -1] + const filename = `${s.gid(10)}.${fileExt}` + const videoOutPath = `${tempDirectory}` + const cuttingProcess = spawn(config.ffmpegDir,['-loglevel','warning','-i', inputFilePath, '-c','copy','-t',`${cutLength}`,videoOutPath]) + cuttingProcess.stderr.on('data',(data) => { + const err = data.toString() + s.debugLog('cutVideoLength',options,err) + }) + cuttingProcess.on('close',(data) => { + fs.stat(videoOutPath,(err) => { + if(!err){ + response.filename = filename + response.filePath = videoOutPath + setTimeout(() => { + s.file('delete',videoOutPath) + },1000 * 60 * 3) + }else{ + s.debugLog('cutVideoLength:readFile',options,err) + } + resolve(response) + }) + }) + }) + } return { orphanedVideoCheck: orphanedVideoCheck, scanForOrphanedVideos: scanForOrphanedVideos, + cutVideoLength: cutVideoLength, } }