桌面端
- 新增 LocalWsServer(ws://127.0.0.1:17301):hello / ping / get_channels /
start_preview / stop_preview / open_window / close_window / ptz / ptz_preset,
一条连接可同时推多路 JPEG 帧(帧头带 sid);慢命令并发派发,云台命令按连接串行,
页面断开自动清理会话与满帧率窗口
- LocalHttpServer 删除预览(MJPEG/快照/满帧率窗口)、/api/hik/channels、/api/hik/ptz*,
HTTP 17300 只保留 /api/query/{code}、/api/activate-login、/api/playback/download,
其余 POST 统一回 404 并提示已迁到 WS;不再依赖 HCNetSDK
- App 自行组装 HikStreamService -> HikPreviewService + HikNativePreviewService 注入 WS
- 后台服务加固:框架依赖(开发版)安装/启动时自动写服务环境变量 DOTNET_ROOT,
避免 LocalSystem 找不到用户目录里的 .NET 8 而报「后台服务启动超时」
前端
- 新增 api/localWs.ts(连接/断线/请求-响应/二进制帧分发),补 getChannels / openWindow /
closeWindow / ptz / ptzPreset
- CamerasView 预览只走 WS(保留「唤起本机软件 -> 等待 -> 提示安装」兜底),
满帧率窗口、云台、预置点改走 WS
- NvrsView 拉取通道改走 WS(连不上时自动唤起一次再重试)
- CameraLayoutCanvas 合成画面 4 路改 WS 推帧
- 删除 createHikPreview/closeHikPreview*/openHikWindow/closeHikWindow*/ptzControl/ptzPreset
与 isLocalServiceAlive/waitForLocalService
其他
- 移除 cameras.has_ptz(迁移 20261019233000)与相关接口/字段/UI
- 通道号按 struStreamMode 映射(IP 通道空槽导致 51 之后整体错位)
- 扫码 24 小时内只保留最后一次扫描(后上报时间更新时覆盖)
- 清理历史 DESKTOP 占位通道(迁移 20261019230000)
- README / desktop README 补 17301 与 WS 接口说明,修正已删除的 /api/health 描述
696 lines
31 KiB
C#
696 lines
31 KiB
C#
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.Globalization;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services.Hik;
|
||
|
||
/// <summary>一次「这个时段到底有没有录像」的探测结果</summary>
|
||
/// <param name="HasRecord">拉到数据 = 有录像</param>
|
||
/// <param name="Frames">RTSP 模式下解出的帧数(SDK 模式为 -1)</param>
|
||
/// <param name="LogicalChannel">换算出来的 RTSP 1 起通道序号(排查用)</param>
|
||
/// <param name="Error">失败原因(无录像 / 端口不通 / 密码错)</param>
|
||
/// <param name="Source">实际走通的通道:SDK(8000)/ RTSP(554)</param>
|
||
public sealed record PlaybackCheck(bool HasRecord, int Frames, int LogicalChannel, string? Error, string Source = "");
|
||
|
||
/// <summary>下载一路回放的进度</summary>
|
||
/// <param name="Percent">0~1(拿不到总时长时为 null,界面显示为不确定进度)</param>
|
||
/// <param name="Message">当前状态文案</param>
|
||
public sealed record PlaybackProgress(double? Percent, string Message);
|
||
|
||
/// <summary>
|
||
/// 海康 **按时间回放**:校验有没有录像 / 拉流预览 / 下载成 mp4
|
||
///
|
||
/// 两条通道,**优先 8000(SDK 私有协议),不行再走 554(RTSP)**:
|
||
///
|
||
/// | | SDK(`NET_DVR_GetFileByTime_V40`) | RTSP(`Streaming/tracks/...`) |
|
||
/// | --- | --- | --- |
|
||
/// | 端口 | 8000(现场基本都开) | 554(可能关/改端口) |
|
||
/// | 通道号 | **海康通道号**(34) | **RTSP 序号**(2) |
|
||
/// | 落地 | 设备原始封装 PS,需 ffmpeg 转封装 | 直接交给 ffmpeg |
|
||
/// | 速度 | 2~5x 实时,带百分比进度 | 约 0.5~3x,靠 ffmpeg 进度 |
|
||
///
|
||
/// 实测(本项目 NVR):RTSP 的 `tracks/201`(=通道34)能拉到回放,
|
||
/// `tracks/3401` 直接 400;空时段会返回 400 而不是空流 —— 两个信号都能用来判「有/无回放」。
|
||
/// </summary>
|
||
public sealed class HikPlaybackService : IDisposable
|
||
{
|
||
/// <summary>Hikvision RTSP 默认端口(设备侧可改,用环境变量覆盖)</summary>
|
||
private static readonly int RtspPort = ResolveRtspPort();
|
||
|
||
private static readonly Regex FrameRegex = new(@"frame=\s*(\d+)", RegexOptions.Compiled);
|
||
private static readonly Regex OutTimeRegex = new(@"out_time_us=(\d+)", RegexOptions.Compiled);
|
||
private static readonly Regex SpeedRegex = new(@"speed=\s*([\d.]+)x", RegexOptions.Compiled);
|
||
|
||
/// <summary>RTSP socket 读超时(微秒)。回放拉完设备不再推流,靠它让 ffmpeg 收尾退出</summary>
|
||
private const string SocketTimeoutUs = "5000000";
|
||
|
||
private readonly HikDeviceService _device;
|
||
|
||
/// <summary>
|
||
/// 「这台设备的 8000 下载能不能用」缓存(key = host|port|channel)
|
||
///
|
||
/// 实测:有些固件 <c>NET_DVR_GetFileByTime*</c> 直接不工作(返回句柄但 0 字节),
|
||
/// 每路都去试一遍要多等好几秒 → 试过一次就记住,下次直接走 RTSP。
|
||
/// </summary>
|
||
private readonly ConcurrentDictionary<string, bool> _sdkPlayback = new(StringComparer.OrdinalIgnoreCase);
|
||
|
||
/// <summary>
|
||
/// 「录像机时间 - 本机时间」偏差缓存(key = host|port|user)
|
||
///
|
||
/// 扫码时间是**本机时钟**打的,回放是按**录像机本地时间**定位的;两边不一致时
|
||
/// (现场实测本机慢了 9 秒),短回放窗口会把面单直接截掉。
|
||
/// 以录像机时钟为基准做平移,缓存 10 分钟(读不到设备时间时 1 分钟内不重试)。
|
||
/// </summary>
|
||
private readonly ConcurrentDictionary<string, (TimeSpan Offset, long At, long Ttl)> _clockOffsets =
|
||
new(StringComparer.OrdinalIgnoreCase);
|
||
|
||
private const long ClockOffsetTtlMs = 10 * 60 * 1000;
|
||
private const long ClockOffsetFailTtlMs = 60 * 1000;
|
||
|
||
public HikPlaybackService(HikDeviceService device)
|
||
{
|
||
_device = device;
|
||
}
|
||
|
||
/// <summary>ffmpeg 是否可用(没有就既不能校验也不能下载)</summary>
|
||
public bool Available => HikFfmpegService.ResolveFfmpeg() is not null;
|
||
|
||
// ==================== 通道号换算 ====================
|
||
|
||
/// <summary>
|
||
/// 海康通道号 → RTSP 的 1 起序号
|
||
///
|
||
/// 换算依赖设备的「首个数字通道号」(本项目是 33):
|
||
/// <c>logical = channel - startDigital + 1</c>。SDK 不可用时按 33 兜底。
|
||
/// </summary>
|
||
public int LogicalChannel(string host, int port, string userName, string password, int channel)
|
||
{
|
||
var startDigital = 33;
|
||
try
|
||
{
|
||
var start = _device.GetStartDigitalChannel(host, port, userName, password);
|
||
if (start > 0) startDigital = start;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"[playback] 取起始数字通道号失败,按 {startDigital} 兜底:{ex.Message}");
|
||
}
|
||
|
||
var logical = channel - startDigital + 1;
|
||
return logical > 0 ? logical : channel;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成回放 RTSP 地址
|
||
///
|
||
/// ⚠️ **时间语义实测结论(重要)**:海康 RTSP 的 <c>starttime</c> 按**设备本地时间**解析,
|
||
/// 结尾那个 <c>Z</c> 只是格式后缀、**不代表 UTC**。
|
||
/// 实测同一时间点:
|
||
/// - 传 <c>20261018T131550Z</c>(UTC 写法)→ 画面 OSD 是 **13:15:49**(2号工作台,无任务)
|
||
/// - 传 <c>20261018T211550Z</c>(本地写法)→ 画面 OSD 是 **21:15:49**(1号工作台,波次B…)
|
||
/// 也就是按 UTC 传会整整差一个时区(本机 +08:00 差 8 小时)。
|
||
///
|
||
/// 默认按本地时间拼;万一遇到严格按 UTC 解析的固件,用环境变量 <c>YUNCANG_RTSP_TIME=utc</c> 切回去。
|
||
/// </summary>
|
||
/// <param name="streamType">0 主码流 / 1 子码流(回放路径里是 01 / 02)</param>
|
||
public static string BuildRtspUrl(string host, string userName, string password, int logicalChannel,
|
||
DateTimeOffset start, DateTimeOffset end, int streamType = 0, int rtspPort = 0)
|
||
{
|
||
var port = rtspPort > 0 ? rtspPort : RtspPort;
|
||
var startText = FormatTime(start);
|
||
var endText = FormatTime(end);
|
||
var streamNo = streamType == 1 ? "02" : "01";
|
||
|
||
return $"rtsp://{Uri.EscapeDataString(userName)}:{Uri.EscapeDataString(password)}@{host}:{port}"
|
||
+ $"/Streaming/tracks/{logicalChannel}{streamNo}?starttime={startText}&endtime={endText}";
|
||
}
|
||
|
||
private static string FormatTime(DateTimeOffset value)
|
||
{
|
||
var useUtc = string.Equals(Environment.GetEnvironmentVariable("YUNCANG_RTSP_TIME"), "utc",
|
||
StringComparison.OrdinalIgnoreCase);
|
||
|
||
var time = useUtc ? value.UtcDateTime : value.ToLocalTime().DateTime;
|
||
return time.ToString("yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 录像机时钟与本机时钟的偏差(设备 - 本机,秒)。
|
||
///
|
||
/// 扫码时间来自本机时钟,回放定位用设备时间 → 两者必须在同一条时间轴上。
|
||
/// 读不到设备时间 / 设备不支持该命令时返回零(保持原有行为)。
|
||
/// </summary>
|
||
public TimeSpan ClockOffset(string host, int port, string userName, string password)
|
||
{
|
||
var key = $"{host}|{port}|{userName}";
|
||
if (_clockOffsets.TryGetValue(key, out var hit) && Environment.TickCount64 - hit.At < hit.Ttl)
|
||
{
|
||
return hit.Offset;
|
||
}
|
||
|
||
try
|
||
{
|
||
var before = DateTime.Now;
|
||
var device = _device.GetDeviceTime(host, port, userName, password);
|
||
var after = DateTime.Now;
|
||
|
||
// 取请求中点做本机时间,抵消一次往返的一半延迟
|
||
var local = before.AddTicks((after - before).Ticks / 2);
|
||
var offset = device - local;
|
||
|
||
_clockOffsets[key] = (offset, Environment.TickCount64, ClockOffsetTtlMs);
|
||
AppLog.Write($"[playback] 录像机 {host}:{port} 与本机时钟相差 {offset.TotalSeconds:+0.0;-0.0} 秒,回放时间已按录像机时钟对齐");
|
||
|
||
// 差到半小时以上基本不可能是「时钟没校准」,而是时区不一致:
|
||
// 这时回放时间会整体平移一个时区,画面 OSD 也会跟着对不上。
|
||
if (Math.Abs(offset.TotalMinutes) >= 30)
|
||
{
|
||
AppLog.Write($"[playback] ⚠ 差值达 {offset.TotalHours:+0.0;-0.0} 小时,疑似录像机时区与本机不一致"
|
||
+ "(请在录像机里把时区设为东八区,并检查摄像头是否跟随了错误的校时)");
|
||
}
|
||
|
||
return offset;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_clockOffsets[key] = (TimeSpan.Zero, Environment.TickCount64, ClockOffsetFailTtlMs);
|
||
AppLog.Write($"[playback] 读不到录像机时间(按本机时钟处理):{ex.Message}");
|
||
return TimeSpan.Zero;
|
||
}
|
||
}
|
||
|
||
/// <summary>生成回放 RTSP 地址(自动把本机时间平移成录像机时间)</summary>
|
||
public string BuildRtspUrlFor(string host, int port, string userName, string password,
|
||
int logicalChannel, DateTimeOffset start, DateTimeOffset end, int streamType = 0)
|
||
{
|
||
var offset = ClockOffset(host, port, userName, password);
|
||
return BuildRtspUrl(host, userName, password, logicalChannel,
|
||
start.Add(offset), end.Add(offset), streamType);
|
||
}
|
||
|
||
// ==================== ① 校验有没有录像 ====================
|
||
|
||
/// <summary>
|
||
/// 探测该时段有没有录像
|
||
///
|
||
/// **先走 8000(SDK 下载 2 秒到临时文件,看有没有数据)**,不行再走 RTSP 拉 2 秒看帧。
|
||
/// 设备不会主动告诉你空段,只能真拉一次。
|
||
/// </summary>
|
||
public async Task<PlaybackCheck> CheckAsync(string host, int port, string userName, string password,
|
||
int channel, DateTimeOffset start, DateTimeOffset end, CancellationToken ct = default)
|
||
{
|
||
if (end <= start) return new PlaybackCheck(false, 0, 0, "回放区间为空");
|
||
|
||
// 扫码时间是本机时钟 → 平移成录像机时间,否则时钟偏差会把面单挤出回放窗口
|
||
var clock = ClockOffset(host, port, userName, password);
|
||
start = start.Add(clock);
|
||
end = end.Add(clock);
|
||
|
||
var sdkKey = $"{host}|{port}|{channel}|pb";
|
||
var trySdk = !_sdkPlayback.TryGetValue(sdkKey, out var capable) || capable;
|
||
|
||
// ① SDK(8000 私有协议):**全速推送**,实测 14 MB/s(≈17 倍速)
|
||
var probeEnd = start.AddSeconds(Math.Min(2, Math.Max((end - start).TotalSeconds, 1)));
|
||
var probeFile = Path.Combine(Path.GetTempPath(), $"yc_probe_{Guid.NewGuid():N}.ps");
|
||
|
||
if (trySdk)
|
||
{
|
||
try
|
||
{
|
||
var bytes = await Task.Run(() => _device.PullPlaybackToFile(host, port, userName, password,
|
||
channel, start.LocalDateTime, probeEnd.LocalDateTime, probeFile, null, ct), ct)
|
||
.ConfigureAwait(false);
|
||
|
||
AppLog.Write($"[playback] SDK 探测 {host} 通道{channel}:{bytes} 字节");
|
||
|
||
if (bytes > 16 * 1024)
|
||
{
|
||
_sdkPlayback[sdkKey] = true;
|
||
return new PlaybackCheck(true, -1, LogicalChannel(host, port, userName, password, channel),
|
||
null, "SDK");
|
||
}
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
throw;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_sdkPlayback[sdkKey] = false;
|
||
AppLog.Write($"[playback] SDK 探测失败({ex.Message}),本设备后续直接走 RTSP");
|
||
}
|
||
finally
|
||
{
|
||
TryDelete(probeFile);
|
||
}
|
||
}
|
||
|
||
// ② RTSP 回落
|
||
var ffmpeg = HikFfmpegService.ResolveFfmpeg()
|
||
?? throw new HikException(uint.MaxValue, "未找到 ffmpeg,无法校验回放(安装目录 native\\ffmpeg\\ffmpeg.exe 缺失,建议重新安装本软件)");
|
||
|
||
var logical = LogicalChannel(host, port, userName, password, channel);
|
||
var url = BuildRtspUrl(host, userName, password, logical, start, end);
|
||
var seconds = Math.Clamp((end - start).TotalSeconds, 0.5, 2.0);
|
||
|
||
var args = new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-rtsp_transport", "tcp", "-timeout", SocketTimeoutUs,
|
||
"-i", url,
|
||
"-t", seconds.ToString("0.#", CultureInfo.InvariantCulture),
|
||
"-an", "-f", "null",
|
||
// ⚠️ 靠 -progress 才能拿到 frame=:ffmpeg 的统计行是 info 级别,
|
||
// 用 -loglevel error 时根本不会打印(曾经因此把所有时段误判成「无录像」)
|
||
"-progress", "pipe:2", "-nostats",
|
||
"-",
|
||
};
|
||
|
||
var output = new StringBuilder();
|
||
var exit = await RunAsync(ffmpeg, args, line => { lock (output) output.AppendLine(line); }, ct)
|
||
.ConfigureAwait(false);
|
||
|
||
string text;
|
||
lock (output) text = output.ToString();
|
||
|
||
var match = FrameRegex.Match(text);
|
||
var frames = match.Success && int.TryParse(match.Groups[1].Value, out var count) ? count : 0;
|
||
|
||
if (frames > 0) return new PlaybackCheck(true, frames, logical, null, "RTSP");
|
||
|
||
var error = Summarize(text) ?? (exit == 0 ? "该时段没有录像" : $"ffmpeg 退出码 {exit}");
|
||
return new PlaybackCheck(false, 0, logical, error, "RTSP");
|
||
}
|
||
|
||
// ==================== ② 下载成 mp4 ====================
|
||
|
||
/// <summary>
|
||
/// 下载一路回放到文件
|
||
///
|
||
/// 实测出来的两个坑(本项目 NVR):
|
||
/// 1. **回放流不会按 endtime 自己断**:ffmpeg 拉完数据还挂在 RTSP 读上,进程不退出 →
|
||
/// 必须加 <c>-timeout</c>(socket I/O 超时)オ能让它正常收尾;
|
||
/// 2. **不能直接写 mp4**:流没正常结束就不会写 moov atom,文件无法播放(moov atom not found)。
|
||
/// 所以先写 <c>mpegts</c>(流式容器,中途中断也能播),完成后本地转封装成 mp4。
|
||
///
|
||
/// 回放速度实测约 0.5x~3x(跟码流大小有关),所以**必须显示进度、必须能取消**。
|
||
/// </summary>
|
||
/// <returns>落地文件路径(mp4;转封装失败时是 .ts)</returns>
|
||
public async Task<string> DownloadAsync(string host, int port, string userName, string password,
|
||
int channel, DateTimeOffset start, DateTimeOffset end, string outputPath,
|
||
IProgress<PlaybackProgress>? progress = null, CancellationToken ct = default)
|
||
{
|
||
var ffmpeg = HikFfmpegService.ResolveFfmpeg()
|
||
?? throw new HikException(uint.MaxValue, "未找到 ffmpeg,无法下载录像(安装目录 native\\ffmpeg\\ffmpeg.exe 缺失,建议重新安装本软件)");
|
||
|
||
if (end <= start) throw new HikException(uint.MaxValue, "回放区间为空");
|
||
|
||
// 扫码时间是本机时钟 → 平移成录像机时间(见 CheckAsync 的说明)
|
||
var clock = ClockOffset(host, port, userName, password);
|
||
start = start.Add(clock);
|
||
end = end.Add(clock);
|
||
|
||
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? AppContext.BaseDirectory);
|
||
|
||
// ① 优先 SDK(8000 私有协议),但**只在它被证明可用时**;
|
||
// 实测部分固件(本项目这台 NVR)GetFileByTime 返回句柄却不推数据 → 试一次就永久回落 RTSP。
|
||
var sdkKey = $"{host}|{port}|{channel}|pb";
|
||
if (!_sdkPlayback.TryGetValue(sdkKey, out var sdkCapable) || sdkCapable)
|
||
{
|
||
var tempPs = Path.Combine(Path.GetTempPath(), $"yc_pb_{Guid.NewGuid():N}.ps");
|
||
try
|
||
{
|
||
progress?.Report(new PlaybackProgress(0, "下载中(8000 全速)…"));
|
||
|
||
var bytes = await Task.Run(() => _device.PullPlaybackToFile(host, port, userName, password, channel,
|
||
start.LocalDateTime, end.LocalDateTime, tempPs,
|
||
received => progress?.Report(new PlaybackProgress(null,
|
||
$"下载中(8000 全速){received / 1024.0 / 1024.0:0.0} MB")), ct), ct)
|
||
.ConfigureAwait(false);
|
||
|
||
AppLog.Write($"[playback] SDK 拉流完成:{bytes} 字节");
|
||
|
||
if (bytes > 16 * 1024)
|
||
{
|
||
_sdkPlayback[sdkKey] = true;
|
||
progress?.Report(new PlaybackProgress(0.99, "正在封装成 mp4…"));
|
||
if (await RepackAsync(ffmpeg, tempPs, outputPath, ct).ConfigureAwait(false))
|
||
{
|
||
var okSize = new FileInfo(outputPath).Length;
|
||
progress?.Report(new PlaybackProgress(1, $"下载完成({Math.Round(okSize / 1024.0 / 1024.0, 1)} MB)"));
|
||
AppLog.Write($"[playback] 回放已下载:{outputPath}({okSize} 字节)");
|
||
return outputPath;
|
||
}
|
||
|
||
// mp4 封装不了 → 把设备原始 PS 留下(PotPlayer/VLC 能直接播)
|
||
var psPath = outputPath + ".ps";
|
||
if (File.Exists(psPath)) File.Delete(psPath);
|
||
File.Move(tempPs, psPath);
|
||
progress?.Report(new PlaybackProgress(1, "下载完成(原始封装 .ps)"));
|
||
AppLog.Write($"[playback] 封装失败,保留原始录像:{psPath}");
|
||
return psPath;
|
||
}
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
throw;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_sdkPlayback[sdkKey] = false;
|
||
AppLog.Write($"[playback] SDK 下载失败({ex.Message}),本设备后续直接走 RTSP 554");
|
||
}
|
||
finally
|
||
{
|
||
TryDelete(tempPs);
|
||
}
|
||
}
|
||
|
||
var logical = LogicalChannel(host, port, userName, password, channel);
|
||
var url = BuildRtspUrl(host, userName, password, logical, start, end);
|
||
var totalMs = Math.Max((end - start).TotalMilliseconds, 1);
|
||
var totalSec = Math.Max((end - start).TotalSeconds, 1);
|
||
|
||
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? AppContext.BaseDirectory);
|
||
|
||
var tsPath = Path.ChangeExtension(outputPath, ".ts");
|
||
if (File.Exists(tsPath)) File.Delete(tsPath);
|
||
|
||
progress?.Report(new PlaybackProgress(0, "开始下载…"));
|
||
|
||
// ① 拉流存 ts(流式容器,中途取消也能播)
|
||
var pullArgs = new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-rtsp_transport", "tcp", "-timeout", SocketTimeoutUs,
|
||
"-i", url,
|
||
"-t", (totalSec + 1).ToString("0", CultureInfo.InvariantCulture),
|
||
"-c:v", "copy", "-an", "-f", "mpegts",
|
||
"-progress", "pipe:2", "-nostats",
|
||
"-y", tsPath,
|
||
};
|
||
|
||
var exit = await RunAsync(ffmpeg, pullArgs,
|
||
line => ReportProgress(progress, line, totalMs, "下载中"), ct).ConfigureAwait(false);
|
||
|
||
var size = File.Exists(tsPath) ? new FileInfo(tsPath).Length : 0;
|
||
if (!File.Exists(tsPath) || size < 16 * 1024)
|
||
{
|
||
throw new HikException(uint.MaxValue,
|
||
exit == 0 ? "该时段没有拉到录像数据" : $"下载失败(ffmpeg 退出码 {exit})");
|
||
}
|
||
|
||
// ② 转封装成 mp4(本地操作,很快;失败就保留 ts)
|
||
progress?.Report(new PlaybackProgress(0.99, "正在封装成 mp4…"));
|
||
if (File.Exists(outputPath)) File.Delete(outputPath);
|
||
|
||
var convertArgs = new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-i", tsPath,
|
||
"-c:v", "copy", "-an", "-movflags", "+faststart",
|
||
"-y", outputPath,
|
||
};
|
||
|
||
var convertExit = await RunAsync(ffmpeg, convertArgs, null, ct).ConfigureAwait(false);
|
||
|
||
var result = outputPath;
|
||
if (convertExit != 0 || !File.Exists(outputPath) || new FileInfo(outputPath).Length < 16 * 1024)
|
||
{
|
||
// mp4 封装不了(极少见)→ 保留 ts,也能直接播
|
||
AppLog.Write($"[playback] mp4 封装失败(exit={convertExit}),保留 ts:{tsPath}");
|
||
result = tsPath;
|
||
}
|
||
else
|
||
{
|
||
File.Delete(tsPath);
|
||
}
|
||
|
||
var finalSize = new FileInfo(result).Length;
|
||
progress?.Report(new PlaybackProgress(1, $"下载完成({Math.Round(finalSize / 1024.0 / 1024.0, 1)} MB)"));
|
||
AppLog.Write($"[playback] 回放已下载:{result}({finalSize} 字节)");
|
||
return result;
|
||
}
|
||
|
||
// ==================== ③ 按工作台布局合并成一个视频 ====================
|
||
|
||
/// <summary>
|
||
/// 把多路下载好的片段**按工作台画布布局合并成一个视频**
|
||
///
|
||
/// 做法:先铺一张黑画布(`color` 虚拟输入),每路 `scale` 到它在快照里的尺寸,
|
||
/// 再用 `overlay=x:y` 摆到对应位置 —— 与工作台页面上的画面摆放一致。
|
||
///
|
||
/// ⚠️ 踩过的坑:**不要**把每路先 <c>pad</c> 成整张画布再叠加 ——
|
||
/// 补边是黑底,叠上去会把下面那路的画面整块盖住(表现是「合成完只剩一个小窗,其余全黑」)。
|
||
///
|
||
/// 为什么要重新编码:多路叠加没法 `-c:v copy`(要重画每个像素),所以这一步比下载慢,
|
||
/// 用 <c>veryfast</c> + <c>crf 23</c> 折中。
|
||
/// </summary>
|
||
/// <param name="segments">各路下载好的文件(顺序与 <paramref name="streams"/> 一一对应)</param>
|
||
/// <returns>合并后文件路径</returns>
|
||
public async Task<string> ComposeAsync(IReadOnlyList<string> segments, IReadOnlyList<PackStream> streams,
|
||
string outputPath, IProgress<PlaybackProgress>? progress = null, CancellationToken ct = default)
|
||
{
|
||
if (segments.Count == 0) throw new HikException(uint.MaxValue, "没有可合并的片段");
|
||
if (segments.Count != streams.Count) throw new HikException(uint.MaxValue, "片段数与流数量不一致");
|
||
|
||
var ffmpeg = HikFfmpegService.ResolveFfmpeg()
|
||
?? throw new HikException(uint.MaxValue, "未找到 ffmpeg,无法合并视频(安装目录 native\\ffmpeg\\ffmpeg.exe 缺失,建议重新安装本软件)");
|
||
|
||
if (segments.Count == 1)
|
||
{
|
||
if (!string.Equals(segments[0], outputPath, StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
if (File.Exists(outputPath)) File.Delete(outputPath);
|
||
File.Copy(segments[0], outputPath);
|
||
}
|
||
return outputPath;
|
||
}
|
||
|
||
var canvasWidth = streams.Max(item => item.CanvasWidth) > 0
|
||
? streams.Max(item => item.CanvasWidth)
|
||
: 1920;
|
||
var canvasHeight = streams.Max(item => item.CanvasHeight) > 0
|
||
? streams.Max(item => item.CanvasHeight)
|
||
: 1080;
|
||
|
||
var totalSec = Math.Max((streams[0].EndTime - streams[0].StartTime).TotalSeconds, 1);
|
||
|
||
var filters = new List<string>();
|
||
for (var i = 0; i < streams.Count; i++)
|
||
{
|
||
var stream = streams[i];
|
||
var width = stream.Layout.Width > 0 ? stream.Layout.Width : canvasWidth;
|
||
var height = stream.Layout.Height > 0 ? stream.Layout.Height : canvasHeight;
|
||
|
||
// 帧率统一到 25(各路帧率不一样,overlay 会互相等待并对不齐)
|
||
filters.Add($"[{i + 1}:v]scale={width}:{height}:force_original_aspect_ratio=decrease,"
|
||
+ $"fps=25,format=yuv420p,setsar=1[v{i}]");
|
||
}
|
||
|
||
var current = "[0:v]";
|
||
for (var i = 0; i < streams.Count; i++)
|
||
{
|
||
var label = i == streams.Count - 1 ? "[out]" : $"[c{i}]";
|
||
filters.Add($"{current}[v{i}]overlay={streams[i].Layout.X}:{streams[i].Layout.Y}{label}");
|
||
current = label;
|
||
}
|
||
|
||
var args = new List<string>
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
// 黑画布当底板(d 与录像时长一致);索引 0,各路视频从 1 开始
|
||
"-f", "lavfi", "-i",
|
||
$"color=c=black:s={canvasWidth}x{canvasHeight}:r=25:d={totalSec.ToString("0", CultureInfo.InvariantCulture)}",
|
||
};
|
||
foreach (var segment in segments)
|
||
{
|
||
args.Add("-i");
|
||
args.Add(segment);
|
||
}
|
||
|
||
args.AddRange(new[]
|
||
{
|
||
"-filter_complex", string.Join(';', filters),
|
||
"-map", current,
|
||
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
|
||
"-an", "-movflags", "+faststart",
|
||
"-t", totalSec.ToString("0", CultureInfo.InvariantCulture),
|
||
"-progress", "pipe:2", "-nostats",
|
||
"-y", outputPath,
|
||
});
|
||
|
||
progress?.Report(new PlaybackProgress(0, "正在合并(按工作台布局)…"));
|
||
|
||
var totalMs = totalSec * 1000;
|
||
var exit = await RunAsync(ffmpeg, args.ToArray(),
|
||
line => ReportProgress(progress, line, totalMs, "合并中"), ct).ConfigureAwait(false);
|
||
|
||
if (exit != 0 || !File.Exists(outputPath) || new FileInfo(outputPath).Length < 16 * 1024)
|
||
{
|
||
throw new HikException(uint.MaxValue, $"合并失败(ffmpeg 退出码 {exit})");
|
||
}
|
||
|
||
var size = new FileInfo(outputPath).Length;
|
||
progress?.Report(new PlaybackProgress(1, $"合并完成({Math.Round(size / 1024.0 / 1024.0, 1)} MB)"));
|
||
AppLog.Write($"[playback] 已合并 {segments.Count} 路 → {outputPath}({size} 字节)");
|
||
return outputPath;
|
||
}
|
||
|
||
// ==================== 内部 ====================
|
||
|
||
/// <summary>把设备原始录像(PS)转封装成 mp4;失败返回 false</summary>
|
||
private async Task<bool> RepackAsync(string ffmpeg, string input, string outputPath, CancellationToken ct)
|
||
{
|
||
if (File.Exists(outputPath)) File.Delete(outputPath);
|
||
|
||
var args = new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-i", input,
|
||
"-c:v", "copy", "-an", "-movflags", "+faststart",
|
||
"-y", outputPath,
|
||
};
|
||
|
||
var exit = await RunAsync(ffmpeg, args, null, ct).ConfigureAwait(false);
|
||
if (exit == 0 && File.Exists(outputPath) && new FileInfo(outputPath).Length > 16 * 1024) return true;
|
||
|
||
// 复制失败(PS 里码流不规范)→ 转码兜底
|
||
AppLog.Write($"[playback] 转封装失败(exit={exit}),改用转码");
|
||
if (File.Exists(outputPath)) File.Delete(outputPath);
|
||
|
||
var transcode = new[]
|
||
{
|
||
"-hide_banner", "-nostdin", "-loglevel", "error",
|
||
"-i", input,
|
||
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
|
||
"-an", "-movflags", "+faststart",
|
||
"-y", outputPath,
|
||
};
|
||
|
||
exit = await RunAsync(ffmpeg, transcode, null, ct).ConfigureAwait(false);
|
||
return exit == 0 && File.Exists(outputPath) && new FileInfo(outputPath).Length > 16 * 1024;
|
||
}
|
||
|
||
private static void TryDelete(string path)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(path)) File.Delete(path);
|
||
}
|
||
catch
|
||
{
|
||
// 临时文件删不掉不影响业务
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 跑一次 ffmpeg:stderr 逐行回调(进度/错误都在 stderr)
|
||
/// </summary>
|
||
private async Task<int> RunAsync(string ffmpeg, string[] args, Action<string>? onLine, CancellationToken ct)
|
||
{
|
||
var psi = new ProcessStartInfo
|
||
{
|
||
FileName = ffmpeg,
|
||
UseShellExecute = false,
|
||
CreateNoWindow = true,
|
||
RedirectStandardOutput = true,
|
||
RedirectStandardError = true,
|
||
RedirectStandardInput = false,
|
||
};
|
||
foreach (var arg in args) psi.ArgumentList.Add(arg);
|
||
|
||
using var process = new Process { StartInfo = psi, EnableRaisingEvents = true };
|
||
process.ErrorDataReceived += (_, e) => { if (e.Data is not null) onLine?.Invoke(e.Data); };
|
||
process.OutputDataReceived += (_, e) => { if (e.Data is not null) onLine?.Invoke(e.Data); };
|
||
|
||
process.Start();
|
||
process.BeginErrorReadLine();
|
||
process.BeginOutputReadLine();
|
||
|
||
try
|
||
{
|
||
await process.WaitForExitAsync(ct).ConfigureAwait(false);
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
TryKill(process);
|
||
throw;
|
||
}
|
||
|
||
return process.ExitCode;
|
||
}
|
||
|
||
private static void TryKill(Process process)
|
||
{
|
||
try
|
||
{
|
||
if (!process.HasExited) process.Kill(entireProcessTree: true);
|
||
}
|
||
catch
|
||
{
|
||
// 进程已经退了
|
||
}
|
||
}
|
||
|
||
/// <summary>解析 `<c>-progress</c>` 的 `out_time_us=`,算百分比</summary>
|
||
private static void ReportProgress(IProgress<PlaybackProgress>? progress, string line, double totalMs, string label)
|
||
{
|
||
if (progress is null) return;
|
||
|
||
var time = OutTimeRegex.Match(line);
|
||
if (time.Success && long.TryParse(time.Groups[1].Value, out var micro))
|
||
{
|
||
var doneMs = micro / 1000.0;
|
||
var percent = Math.Clamp(doneMs / totalMs, 0, 0.999);
|
||
var speed = SpeedRegex.Match(line);
|
||
var suffix = speed.Success ? $" · {speed.Groups[1].Value}x" : string.Empty;
|
||
progress.Report(new PlaybackProgress(percent, $"{label} {percent:P0}{suffix}"));
|
||
}
|
||
}
|
||
|
||
/// <summary>把 ffmpeg 的输出压成一句能看懂的错因</summary>
|
||
private static string? Summarize(string output)
|
||
{
|
||
foreach (var line in output.Split('\n', StringSplitOptions.RemoveEmptyEntries))
|
||
{
|
||
var text = line.Trim();
|
||
if (text.Length == 0) continue;
|
||
|
||
if (text.Contains("401", StringComparison.Ordinal) || text.Contains("Unauthorized", StringComparison.OrdinalIgnoreCase))
|
||
return "录像机拒绝访问(账号/密码错)";
|
||
if (text.Contains("400 Bad Request", StringComparison.OrdinalIgnoreCase))
|
||
return "该时段没有录像(RTSP 返回 400)";
|
||
if (text.Contains("404", StringComparison.Ordinal) || text.Contains("Not Found", StringComparison.OrdinalIgnoreCase))
|
||
return "该通道/时段没有录像(RTSP 404)";
|
||
if (text.Contains("Connection refused", StringComparison.OrdinalIgnoreCase))
|
||
return "连不上录像机的 RTSP 端口(554)";
|
||
if (text.Contains("timed out", StringComparison.OrdinalIgnoreCase))
|
||
return "连接录像机超时";
|
||
if (line.StartsWith("Error", StringComparison.OrdinalIgnoreCase) || text.StartsWith("rtsp:", StringComparison.OrdinalIgnoreCase))
|
||
return text.Length > 160 ? text[..160] : text;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private static int ResolveRtspPort()
|
||
{
|
||
return int.TryParse(Environment.GetEnvironmentVariable("YUNCANG_RTSP_PORT"), out var port) && port > 0
|
||
? port
|
||
: 554;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
// 后台任务由调用方各自取消;这里没有需要释放的资源
|
||
}
|
||
}
|