public int DownloadVideoRecordByTime(int userId, int channelNumber, DateTime beginTime, DateTime endTime, string fullPath) { CHCNetSDK.NET_DVR_PLAYCOND struDownPara = new CHCNetSDK.NET_DVR_PLAYCOND(); //通道号 Channel number struDownPara.dwChannel = (uint)channelNumber; //设置下载的开始时间 Set the starting time struDownPara.struStartTime = beginTime.ToHCDeviceTime(); //设置下载的结束时间 Set the stopping time struDownPara.struStopTime = endTime.ToHCDeviceTime(); //按时间下载 Download by time int downloadId = CHCNetSDK.NET_DVR_GetFileByTime_V40(userId, fullPath, ref struDownPara); if (downloadId < 0) { uint iLastErr = CHCNetSDK.NET_DVR_GetLastError(); string str = "NET_DVR_GetFileByTime_V40 failed, error code= " + iLastErr; return(-1); } uint iOutValue = 0; if (!CHCNetSDK.NET_DVR_PlayBackControl_V40(downloadId, CHCNetSDK.NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue)) { uint iLastErr = CHCNetSDK.NET_DVR_GetLastError(); //下载控制失败,输出错误号 string str = "NET_DVR_PLAYSTART failed, error code= " + iLastErr; return(-1); } return(downloadId); }
private void btnDownloadTime_Click(object sender, EventArgs e) { if (m_lDownHandle >= 0) { MessageBox.Show("Downloading, please stop firstly!");//正在下载,请先停止下载 return; } CHCNetSDK.NET_DVR_PLAYCOND struDownPara = new CHCNetSDK.NET_DVR_PLAYCOND(); struDownPara.dwChannel = (uint)iChannelNum[(int)iSelIndex]; //通道号 Channel number //设置下载的开始时间 Set the starting time struDownPara.struStartTime.dwYear = (uint)dateTimeStart.Value.Year; struDownPara.struStartTime.dwMonth = (uint)dateTimeStart.Value.Month; struDownPara.struStartTime.dwDay = (uint)dateTimeStart.Value.Day; struDownPara.struStartTime.dwHour = (uint)dateTimeStart.Value.Hour; struDownPara.struStartTime.dwMinute = (uint)dateTimeStart.Value.Minute; struDownPara.struStartTime.dwSecond = (uint)dateTimeStart.Value.Second; //设置下载的结束时间 Set the stopping time struDownPara.struStopTime.dwYear = (uint)dateTimeEnd.Value.Year; struDownPara.struStopTime.dwMonth = (uint)dateTimeEnd.Value.Month; struDownPara.struStopTime.dwDay = (uint)dateTimeEnd.Value.Day; struDownPara.struStopTime.dwHour = (uint)dateTimeEnd.Value.Hour; struDownPara.struStopTime.dwMinute = (uint)dateTimeEnd.Value.Minute; struDownPara.struStopTime.dwSecond = (uint)dateTimeEnd.Value.Second; string sVideoFileName; //录像文件保存路径和文件名 the path and file name to save sVideoFileName = "D:\\Downtest_Channel" + struDownPara.dwChannel + ".mp4"; //按时间下载 Download by time m_lDownHandle = CHCNetSDK.NET_DVR_GetFileByTime_V40(m_lUserID, sVideoFileName, ref struDownPara); if (m_lDownHandle < 0) { iLastErr = CHCNetSDK.NET_DVR_GetLastError(); str = "NET_DVR_GetFileByTime_V40 failed, error code= " + iLastErr; MessageBox.Show(str); return; } uint iOutValue = 0; if (!CHCNetSDK.NET_DVR_PlayBackControl_V40(m_lDownHandle, CHCNetSDK.NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue)) { iLastErr = CHCNetSDK.NET_DVR_GetLastError(); str = "NET_DVR_PLAYSTART failed, error code= " + iLastErr; //下载控制失败,输出错误号 MessageBox.Show(str); return; } timerDownload.Interval = 1000; timerDownload.Enabled = true; btnStopDownload.Enabled = true; }
public void DownloadTime(object req) { LastRunTime = DateTime.Now.Ticks / 10000; _obj = req; _finsh = false; RunParam parm = (RunParam)req; Channel request = parm.Channel; _channel = request; int userId = request.UserId; int index = request.Index; DateTime start = request.StartTime; DateTime end = request.EndTime; // Console.WriteLine("下载" + userId + "." + index); _userId = userId; CHCNetSDK.NET_DVR_PLAYCOND struDownPara = new CHCNetSDK.NET_DVR_PLAYCOND(); struDownPara.dwChannel = (uint)index; //设置下载的开始时间 Set the starting time struDownPara.struStartTime.dwYear = (uint)start.Year; struDownPara.struStartTime.dwMonth = (uint)start.Month; struDownPara.struStartTime.dwDay = (uint)start.Day; struDownPara.struStartTime.dwHour = (uint)start.Hour; struDownPara.struStartTime.dwMinute = (uint)start.Minute; struDownPara.struStartTime.dwSecond = (uint)start.Second; //设置下载的结束时间 Set the stopping time struDownPara.struStopTime.dwYear = (uint)end.Year; struDownPara.struStopTime.dwMonth = (uint)end.Month; struDownPara.struStopTime.dwDay = (uint)end.Day; struDownPara.struStopTime.dwHour = (uint)end.Hour; struDownPara.struStopTime.dwMinute = (uint)end.Minute; struDownPara.struStopTime.dwSecond = (uint)end.Second; string sVideoFileName; //录像文件保存路径和文件名 the path and file name to save sVideoFileName = parm.SavePath + "\\" + parm.FileName + "." + request.No + ".mp4"; _log.Error(sVideoFileName); if (File.Exists(sVideoFileName)) { _log.Info(sVideoFileName + "文件已存在"); _downloadprogress = 100; _finsh = true; } else { //按时间下载 Download by time this._download = CHCNetSDK.NET_DVR_GetFileByTime_V40(userId, sVideoFileName, ref struDownPara); int t = 0; while (this._download < 0 && t < 10) { Console.WriteLine(_channel.ChannelIp + "." + _download + ":重试" + CHCNetSDK.NET_DVR_GetLastError()); _log.Info(_channel.ChannelIp + "." + _download + ":重试" + CHCNetSDK.NET_DVR_GetLastError()); _download = CHCNetSDK.NET_DVR_GetFileByTime_V40(userId, sVideoFileName, ref struDownPara); t++; Thread.Sleep(300); } uint iOutValue = 0; if (!CHCNetSDK.NET_DVR_PlayBackControl_V40(_download, CHCNetSDK.NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue)) { _log.Info(_download + "." + CHCNetSDK.NET_DVR_GetLastError()); Console.WriteLine(CHCNetSDK.NET_DVR_GetLastError()); } } }
public async Task <OResult <Boolean> > DownloadByTimeAsync(DownloadFileByTimeRequest request, CancellationToken cancellationToken = default) { try { var now = DateTime.Now; if (request.From > now) { request.From = now; } if (request.To > now) { request.To = now; } if (request.From > request.To) { request.To = DateTime.Now; } CHCNetSDK.NET_DVR_PLAYCOND struDownPara = new CHCNetSDK.NET_DVR_PLAYCOND(); struDownPara.dwChannel = (UInt32)_loginRequest.ChannelNo; //设置下载的开始时间 struDownPara.struStartTime.dwYear = (UInt32)request.From.Year; struDownPara.struStartTime.dwMonth = (UInt32)request.From.Month; struDownPara.struStartTime.dwDay = (UInt32)request.From.Day; struDownPara.struStartTime.dwHour = (UInt32)request.From.Hour; struDownPara.struStartTime.dwMinute = (UInt32)request.From.Minute; struDownPara.struStartTime.dwSecond = (UInt32)request.From.Second; //设置下载的结束时间 struDownPara.struStopTime.dwYear = (UInt32)request.To.Year; struDownPara.struStopTime.dwMonth = (UInt32)request.To.Month; struDownPara.struStopTime.dwDay = (UInt32)request.To.Day; struDownPara.struStopTime.dwHour = (UInt32)request.To.Hour; struDownPara.struStopTime.dwMinute = (UInt32)request.To.Minute; struDownPara.struStopTime.dwSecond = (UInt32)request.To.Second; var m_lDownHandle = CHCNetSDK.NET_DVR_GetFileByTime_V40(_userId, request.FilePath, ref struDownPara); if (m_lDownHandle < 0) { return(new OResult <Boolean>(false, HkvsErrorCode.GetLastErrorCode(), $"按时间下载失败:{HkvsErrorCode.GetLastErrorMessage()}")); } UInt32 iOutValue = 0; var succ = CHCNetSDK.NET_DVR_PlayBackControl_V40(m_lDownHandle, CHCNetSDK.NET_DVR_PLAYSTART, IntPtr.Zero, 0, IntPtr.Zero, ref iOutValue); if (!succ) { return(new OResult <Boolean>(false, HkvsErrorCode.GetLastErrorCode(), $"按时间下载失败:{HkvsErrorCode.GetLastErrorMessage()}")); } var tcs = new TaskCompletionSource <Boolean>(); _ = Task.Run(async() => { var iPos = 0; while (!cancellationToken.IsCancellationRequested) { iPos = CHCNetSDK.NET_DVR_GetDownloadPos(m_lDownHandle); switch (iPos) { case 100: CHCNetSDK.NET_DVR_StopGetFile(m_lDownHandle); tcs.SetResult(true); return; case 200: tcs.SetException(new SystemException("网络中断")); CHCNetSDK.NET_DVR_StopGetFile(m_lDownHandle); return; } await Task.Delay(1000); } }, CancellationToken.None); succ = await tcs.Task; if (!succ) { return(new OResult <Boolean>(false, "按时间下载失败")); } return(new OResult <Boolean>(true)); } catch (Exception ex) { return(new OResult <Boolean>(false, ex)); } }