Skip to content
This SDK only works with devices released within the past 3 years. We regularly optimize and update the SDK to ensure compatibility and performance. We do not guarantee compatibility or provide technical support for devices older than 3 years.

# Video Download

Finding and downloading videos

## Interface Overview

interface Name | Functional Description
--- | ---
`NET_SDK_FindFile` | Find Recording
`NET_SDK_FindNextFile` | Get file information (file name, size, start and end time)
`NET_SDK_FindClose` | Close Find File
`NET_SDK_GetFileByTimeExV2` | Download by time
`NET_SDK_StopGetFile` | Stop download

Note

For detailed parameters of the interface, see the interface definition section.

## Process Description

The business flow chart is as follows:

    ---
    title: Video Download
    ---

    flowchart TD
        A(Device SDK Initialization <br><strong>NET_SDK_Init</strong>)
        B(User Registers Device <br><strong>NET_SDK_Login</strong> or <br><strong>NET_SDK_LoginEx</strong>)

        subgraph SS [Find Video File]
            direction LR
            S0(Find Video File <br><strong>NET_SDK_FindFile</strong>)
            S1(Get Video File Information One by One <br><strong>NET_SDK_FindNextFile</strong>)
            S2(Stop Video Search <br><strong>NET_SDK_FindClose</strong>)
            S0 --> S1 --> S2
        end

        subgraph SS1 [Video File Download]
            direction LR
            S00(Playback by Time <br><strong>NET_SDK_GetFileByTimeExV2</strong>)
            S01(Stop Video Playback <br><strong>NET_SDK_StopPlayBack</strong>)
            S00 --> S01
        end

        E(Logout Device <br><strong>NET_SDK_Logout</strong>)
        F(Release SDK resources <br><strong>NET_SDK_Cleanup</strong>)

        A --> B --> SS --> SS1 --> E --> F

- `NET_SDK_FindFile`: Query whether there is a video file in a certain time period

- `NET_SDK_FindNextFile`: Handle returned by `NET_SDK_FindFile`, query the specific information of the recording file, including the start and end time of the recording, etc.

- `NET_SDK_GetFileByTimeExV2`: The start and end time of the video returned by `NET_SDK_FindNextFile`, download the video

## Sample Code

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    #include <iostream>
    #include <string>
    #include "stdafx.h"
    #include "DVR_NET_SDK.h"
    #include <iomanip>
    #include <sstream>
    #include <ctime>

    using namespace std;

    /*
        Transform string time to CTime
    */
    CTime parseStrTimeToCTime(string_view intime)
    {
        tm tm = {};
        istringstream ss(intime.data());
        // Parse string time
        ss >> get_time(& tm, "%Y-%m-%d %H:%M:%S");
        // Transform tm to time_t
        time_t tt = mktime(& tm);
        // Transform time_t to CTime
        CTime ctime(tt);
        return ctime;
    }

    void DownloadRecord()
    {
        // Initial
        NET_SDK_Init();
        // Device information
        const std::string device_ip = "10.80.1.177";
        const DWORD decice_port = 6036;
        const std::string username = "admin";
        const std::string password = "123456";
        // Login
        NET_SDK_DEVICEINFO device_info = { 0 };
        LONG userid = NET_SDK_Login(const_cast<char*>(device_ip.c_str()), decice_port, const_cast<char*>(username.c_str()), const_cast<char*>(password.c_str()), & device_info);

        if(userid > 0) {
            cout << "Login successfully: " << userid << endl;

        } else {
            cout << "Failed to login: " << userid << endl;
            return;
        }

        // Find record file
        LONG chnn = 0;
        CTime startTime = parseStrTimeToCTime("2023-05-09 00:00:00");
        CTime endTime = parseStrTimeToCTime("2023-05-09 23:59:59");
        DD_TIME start = { startTime.GetSecond(), startTime.GetMinute(), startTime.GetHour(), 3, startTime.GetDay(), startTime.GetMonth() - 1, startTime.GetYear() - 1900 };
        DD_TIME end = { endTime.GetSecond(), endTime.GetMinute(), endTime.GetHour(), 3, endTime.GetDay(), endTime.GetMonth() - 1, endTime.GetYear() - 1900 };
        LONG ffHandle = NET_SDK_FindFile(userid, chnn, & start, & end);

        if(ffHandle == -1) {
            DWORD err1 = NET_SDK_GetLastError();
            cout << "Failed to find record file! Error code: " << err1 << endl;

        } else {
            cout << "Find record file successfully!" << endl;
            // Get file information (filename, size, startTime and stopTime)
            NET_SDK_REC_FILE searchFile;
            LONG result = NET_SDK_FindNextFile(ffHandle, & searchFile);
            HWND hWnd = GetConsoleWindow();

            if(result != NET_SDK_FILE_SUCCESS) {
                DWORD err4 = NET_SDK_GetLastError();
                cout << "Failed to get file information! Error code: " << err4 << endl;

            } else {
                cout << "Get file information successfully!" << endl;
                NET_SDK_REC_FILE downloadFile;
                CString fname;
                CString savePath = "D:\\\\";
                SYSTEMTIME time = { 0 };
                ::GetLocalTime(& time);
                fname.Format("%sbackup_%d%02d%02d%02d%02d%02d.avi", savePath.GetBuffer(0), time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
                LONG dlHandle = NET_SDK_GetFileByTimeExV2(userid, chnn, &searchFile.startTime, &searchFile.stopTime, fname.GetBuffer(), FALSE, 0, FALSE, NULL, 0);
                // Waiting for downloading
                Sleep(10000);

                if(dlHandle == -1) {
                    DWORD err4 = NET_SDK_GetLastError();
                    cout << "Failed to download! error code: " << err4 << endl;

                } else {
                    cout << "Download successfully!" << endl;
                }
            }

            // Stop to Find record file
            NET_SDK_FindClose(ffHandle);
        }

        // Logout
        NET_SDK_Logout(userid);
        // Clean up the data
        NET_SDK_Cleanup();
    }

## Relevant Instructions

- The download path must be a valid path that already exists, if there is a folder in the path that does not exist, the download will fail and file will not be created automatically.

- The download wait time may need to be increased when the downloaded video is too large

Error Code