From f56ca33a8d6f0c309e4d7237b65d5bcae568f75e Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 31 Jul 2026 23:31:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=AF=8FNVR=203=E5=B9=B6=E5=8F=91=20+?= =?UTF-8?q?=20ffmpeg=E7=BA=AF=E5=A4=8D=E5=88=B6=E4=BF=AE=E5=A4=8D=20+=20on?= =?UTF-8?q?vif=E4=BE=9D=E8=B5=96=20+=20OSD=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调度器: maxPerNvr 1→3, running追踪从Set改Map计数 - ffmpeg: 移除libx264重编码兜底, 修正-f mpegps→自动检测 - 新增 onvif 依赖用于摄像头OSD - setting.setCameraOSD 增加入口/参数/结果日志 - wsClient 心跳更新 --- core/main.js | 136 ++++++++++++++++++++++-------------------- core/wsClient.js | 4 +- package.json | 1 + services/hikvision.js | 6 +- services/setting.js | 8 ++- 5 files changed, 84 insertions(+), 71 deletions(-) diff --git a/core/main.js b/core/main.js index 67f811a..cd43635 100644 --- a/core/main.js +++ b/core/main.js @@ -586,15 +586,16 @@ class Main extends EventEmitter { try { const p = JSON.parse(row.task_params || '{}'); const fp = this._resolvePath(p.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {} } - // ④ 每个录像机(IP)独占一个 worker,避免不同 NVR 互相阻塞 - const maxPerNvr = 1; // 每个 NVR 最多 1 个并发 + // ④ 每个录像机(IP)最多 maxPerNvr 个并发 worker + const maxPerNvr = 3; // 每个 NVR 最多 3 个并发 const maxTotal = 4; // 总并发上限 - // 获取正在运行的 NVR IP - const runningRows = this.db.all(`SELECT DISTINCT json_extract(task_params, '$.ip') as ip FROM hik_tasks WHERE status = 'running'`); - const runningIps = new Set(runningRows.map(r => r.ip).filter(Boolean)); + // 获取正在运行的 IP 及其并发数 + const runningRows = this.db.all(`SELECT json_extract(task_params, '$.ip') as ip, COUNT(*) as cnt FROM hik_tasks WHERE status = 'running' GROUP BY ip`); + const runningCount = new Map(); + for (const r of runningRows) { if (r.ip) runningCount.set(r.ip, r.cnt); } - // 获取待处理任务,按 IP 分组取最早的一条 + // 获取待处理任务,按 IP 分组取最早的多条 const pendingRows = this.db.all(`SELECT * FROM hik_tasks WHERE status = 'pending' ORDER BY id ASC`); const pendingByIp = new Map(); for (const pt of pendingRows) { @@ -604,71 +605,76 @@ class Main extends EventEmitter { this.db.run(`UPDATE hik_tasks SET status='failed', error_message='参数不完整' WHERE id = ?`, pt.id); continue; } - if (!pendingByIp.has(ip)) pendingByIp.set(ip, pt); + if (!pendingByIp.has(ip)) pendingByIp.set(ip, []); + if (pendingByIp.get(ip).length < maxPerNvr) pendingByIp.get(ip).push(pt); } let started = 0; - for (const [ip, pt] of pendingByIp) { - if (runningIps.has(ip)) continue; // 该 NVR 已有下载中 + for (const [ip, tasks] of pendingByIp) { + const current = runningCount.get(ip) || 0; + if (current >= maxPerNvr) continue; // 该 NVR 已达并发上限 if (started >= maxTotal) break; - if (runningIps.size + started >= maxTotal) break; + // 最多启动 maxPerNvr - current 个新任务 + const slots = Math.min(tasks.length, maxPerNvr - current, maxTotal - started); + for (let i = 0; i < slots; i++) { + const pt = tasks[i]; + const downloadCfg = { ...JSON.parse(pt.task_params || '{}'), output: this._resolvePath(JSON.parse(pt.task_params || '{}').output) }; + const wid = ip; // 用 IP 作为 worker_id + this.db.run(`UPDATE hik_tasks SET status='running', worker_id=?, start_time=datetime('now') WHERE id=?`, wid, pt.id); - const downloadCfg = { ...JSON.parse(pt.task_params || '{}'), output: this._resolvePath(JSON.parse(pt.task_params || '{}').output) }; - const wid = ip; // 用 IP 作为 worker_id - this.db.run(`UPDATE hik_tasks SET status='running', worker_id=?, start_time=datetime('now') WHERE id=?`, wid, pt.id); - - const taskId = pt.task_id; - const tp = JSON.parse(pt.task_params || '{}'); - const self = this; - const task = { - startTime: Date.now(), - progress: { chunks: 0, totalBytes: 0, totalMB: 0, speed: tp.speed || 4 }, - params: tp, status: 'running', - unique_code: pt.unique_code || '', // 服务器下发的任务编号 - _aborted: false, // receiveTask 强制覆盖时设为 true - abort() { - task._aborted = true; - if (typeof task._abortDownload === 'function') { - try { task._abortDownload(); } catch {} + const taskId = pt.task_id; + const tp = JSON.parse(pt.task_params || '{}'); + const self = this; + const task = { + startTime: Date.now(), + progress: { chunks: 0, totalBytes: 0, totalMB: 0, speed: tp.speed || 4 }, + params: tp, status: 'running', + unique_code: pt.unique_code || '', // 服务器下发的任务编号 + _aborted: false, // receiveTask 强制覆盖时设为 true + abort() { + task._aborted = true; + if (typeof task._abortDownload === 'function') { + try { task._abortDownload(); } catch {} + } + }, + _onDone: (result) => { + if (task._aborted) return; // 已被强制终止,跳过所有后续处理 + if (result.success === false || (result.size || 0) === 0) { + const errMsg = result.success === false ? '录制返回失败' : '录制文件大小为 0'; + task._onError(new Error(errMsg)); + return; + } + task.status = 'done'; task.result = result; + const relResult = { ...result, output: self._relativizePath(result.output || '') }; + self.db.run(`UPDATE hik_tasks SET status='completed', end_time=datetime('now'), error_message=NULL, result=? WHERE task_id=?`, JSON.stringify(relResult), taskId); + try { self.db.table('hik_downloads').insert({ task_id: taskId, ip: tp.ip || '', channel: tp.channel || 0, output_path: relResult.output || tp.output || '', file_size: result.size || 0, duration: result.duration || 0, resolution: result.resolution || '', status: 'done' }); } catch (e) {} + self.checkHikStorageQuota().catch(() => {}); + self._pushTaskResult(taskId, relResult); + setTimeout(() => self._hikTasks.delete(taskId), 5 * 60 * 1000); + self._scheduleHikTasks(); + }, + _onError: (err) => { + if (task._aborted) return; // 已被强制终止,跳过所有后续处理 + task.status = 'error'; task.error = err.message; + // 录像机无此时间视频 → 永久失败,不再重试 + const permanent = err.message === '录像机无此时间视频'; + self.db.run( + permanent + ? `UPDATE hik_tasks SET status='failed', retry=3, notice=1, end_time=datetime('now'), error_message=? WHERE task_id=?` + : `UPDATE hik_tasks SET status='failed', end_time=datetime('now'), error_message=? WHERE task_id=?`, + err.message, taskId + ); + try { const fp = self._resolvePath(tp.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {} + self._pushTaskFailed(taskId, err.message); + setTimeout(() => self._hikTasks.delete(taskId), 5 * 60 * 1000); + self._scheduleHikTasks(); } - }, - _onDone: (result) => { - if (task._aborted) return; // 已被强制终止,跳过所有后续处理 - if (result.success === false || (result.size || 0) === 0) { - const errMsg = result.success === false ? '录制返回失败' : '录制文件大小为 0'; - task._onError(new Error(errMsg)); - return; - } - task.status = 'done'; task.result = result; - const relResult = { ...result, output: self._relativizePath(result.output || '') }; - self.db.run(`UPDATE hik_tasks SET status='completed', end_time=datetime('now'), error_message=NULL, result=? WHERE task_id=?`, JSON.stringify(relResult), taskId); - try { self.db.table('hik_downloads').insert({ task_id: taskId, ip: tp.ip || '', channel: tp.channel || 0, output_path: relResult.output || tp.output || '', file_size: result.size || 0, duration: result.duration || 0, resolution: result.resolution || '', status: 'done' }); } catch (e) {} - self.checkHikStorageQuota().catch(() => {}); - self._pushTaskResult(taskId, relResult); - setTimeout(() => self._hikTasks.delete(taskId), 5 * 60 * 1000); - self._scheduleHikTasks(); - }, - _onError: (err) => { - if (task._aborted) return; // 已被强制终止,跳过所有后续处理 - task.status = 'error'; task.error = err.message; - // 录像机无此时间视频 → 永久失败,不再重试 - const permanent = err.message === '录像机无此时间视频'; - self.db.run( - permanent - ? `UPDATE hik_tasks SET status='failed', retry=3, notice=1, end_time=datetime('now'), error_message=? WHERE task_id=?` - : `UPDATE hik_tasks SET status='failed', end_time=datetime('now'), error_message=? WHERE task_id=?`, - err.message, taskId - ); - try { const fp = self._resolvePath(tp.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {} - self._pushTaskFailed(taskId, err.message); - setTimeout(() => self._hikTasks.delete(taskId), 5 * 60 * 1000); - self._scheduleHikTasks(); - } - }; - this._hikTasks.set(taskId, task); - hikvisionService.startDownload(downloadCfg, taskId, task); - runningIps.add(ip); - started++; + }; + this._hikTasks.set(taskId, task); + hikvisionService.startDownload(downloadCfg, taskId, task); + runningCount.set(ip, (runningCount.get(ip) || 0) + 1); + started++; + } } // 空闲拉取 const pendingCount = pendingByIp.size; diff --git a/core/wsClient.js b/core/wsClient.js index 72b5bc3..d5d560f 100644 --- a/core/wsClient.js +++ b/core/wsClient.js @@ -110,7 +110,7 @@ class WSClient { const { token, refreshToken, tokenExpireTime } = this.app.config; if (!this.app.info && token && refreshToken && tokenExpireTime) { if (tokenExpireTime < Date.now()) { - //console.log('[WSClient] 心跳检测: token 已过期,尝试刷新'); + console.log('[WSClient] 心跳检测: token 已过期,尝试刷新'); this.send({ method: 'auth.refreshToken', data: { token: refreshToken } }); } else { //拉取信息 @@ -122,7 +122,7 @@ class WSClient { } async _handleMessage(msg) { if(msg === 'pong') { - //console.log('[WSClient] 收到心跳响应'); + console.log('[WSClient] 收到心跳响应'); return; } //console.log("[WSClient]原始消息", msg) diff --git a/package.json b/package.json index bde705f..67de32f 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@xmldom/xmldom": "^0.9.10", "better-sqlite3": "^12.11.1", "iconv-lite": "^0.7.2", + "onvif": "^0.8.1", "ws": "^8.19.0" }, "optionalDependencies": { diff --git a/services/hikvision.js b/services/hikvision.js index af9c946..75a6bcf 100644 --- a/services/hikvision.js +++ b/services/hikvision.js @@ -508,9 +508,9 @@ class HikvisionService extends BaseService { return false; } }; - transcoded = await ffRun(`-f mpegts -i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000); - if (!transcoded) transcoded = await ffRun(`-f mpegps -i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000); - if (!transcoded) transcoded = await ffRun(`-i "${workFile}" -c:v libx264 -preset fast -crf 23 -an "${mp4Path}"`, 120000); + // PS → MP4 纯复制(视频流 copy,丢弃音频),不指定 -f 让 ffmpeg 自动检测 + transcoded = await ffRun(`-i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000); + // copy 失败直接保留 PS 原文件,不再使用 libx264 重编码(多 worker 时 CPU 不可接受) if (transcoded) { const mp4Size = fs.statSync(mp4Path).size; try { fs.unlinkSync(workFile); } catch { /* */ } diff --git a/services/setting.js b/services/setting.js index 5d43e81..f5d5734 100644 --- a/services/setting.js +++ b/services/setting.js @@ -316,10 +316,16 @@ class SettingService extends service { * data: { rtspUrl, text, x?, y? } */ async setCameraOSD(data) { - if (!data || !data.rtspUrl || !data.text) return; + console.log('[CameraOSD] set 收到请求:', JSON.stringify(data)); + if (!data || !data.rtspUrl || !data.text) { + console.log('[CameraOSD] set 参数不完整,跳过:', { hasRtsp: !!data?.rtspUrl, hasText: !!data?.text }); + return; + } try { + console.log('[CameraOSD] set 开始执行 rtspUrl=%s text=%s x=%d y=%d', data.rtspUrl, data.text, data.x, data.y); const { setCameraOSD } = await import('../utils/cameraOsd.js'); await setCameraOSD(data.rtspUrl, data.text, data.x, data.y); + console.log('[CameraOSD] set 执行成功'); } catch (e) { console.error('[CameraOSD] set 执行失败:', e.message); }