In today’s surveillance-driven world, managing and retrieving recorded videos from Network Video Recorders (NVRs) is crucial for security professionals. This blog post will introduce a set of Python scripts that automate the process of searching for and downloading recorded videos from a HIKVision NVR. The scripts enable users to specify a date range and camera track, making it easier to access and manage video footage efficiently.
The Python Scripts:
generate.py
#!/usr/bin/env python
# This scripts calls search.py to search in the HIKVision NVR for recorded videos and the uses download.py to download those videos.
# The script loops over the camera tracks and the last 120 days.
import sys
import os
import datetime
base = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0);
numdays = 120;
dateList = [base - datetime.timedelta(days=x) for x in range(numdays)];
tracks = ["101", "201", "301", "401", "501", "601", "701", "801"];
for trackID in tracks:
for dateItem in dateList:
os.system("python search.py " + trackID + " " + dateItem.strftime('%Y-%m-%dT%H:%M:%SZ') + " " + (dateItem + datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'));
for trackID in tracks:
for dateItem in dateList:
os.system("python download.py " + trackID + " " + dateItem.strftime('%Y-%m-%dT%H:%M:%SZ') + " " + (dateItem + datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'));
- This script acts as the orchestrator, controlling the entire process.
- It generates a list of dates, spanning the last 120 days, and a list of camera tracks to search for video recordings.
- It then iterates through each camera track and date, calling two other Python scripts:
search.py
anddownload.py
.
search.py
#!/usr/bin/env python
# This script makes an API call to the HIKVision NVR with a Track ID and a datetime range and retrieves an XML list with all videos with their download links that were recorded on that camera during that time period.
import sys
import os
trackID = sys.argv[1];
startTime = sys.argv[2];
endTime = sys.argv[3];
xmlFilename = "results/" + trackID + "." + startTime + "." + endTime + ".xml";
os.system("curl 'http://username:[email protected]/ISAPI/ContentMgmt/search' --data-raw $'<?xml version='1.0' encoding='UTF-8'?>\n<CMSearchDescription><searchID>CA77BA52-0780-0001-34B2-6120F2501D36</searchID><trackList><trackID>" + trackID + "</trackID></trackList><timeSpanList><timeSpan><startTime>" + startTime + "</startTime><endTime>" + endTime + "</endTime></timeSpan></timeSpanList><maxResults>100</maxResults><searchResultPostion>0</searchResultPostion><metadataList><metadataDescriptor>//recordType.meta.std-cgi.com</metadataDescriptor></metadataList></CMSearchDescription>' -o "+ xmlFilename);
- This script is responsible for making an API call to the HIKVision NVR to search for recorded videos.
- It takes three command-line arguments: track ID, start time, and end time.
- It constructs a search request in XML format and uses
curl
to send the request to the NVR. - The search results are saved as an XML file for later processing.
download.py
#!/usr/bin/env python
# This script reads an XML file that was retrieved from the HIKVision NVR which containes videos with their download links. For each link, it appends the credentials for login and uses ffmpeg to download the video.
from xml.dom import minidom
import os
import sys
trackID = sys.argv[1];
startTime = sys.argv[2];
endTime = sys.argv[3];
xmlFilename = "results/" + trackID + "." + startTime + "." + endTime + ".xml";
dom = minidom.parse(xmlFilename)
elements = dom.getElementsByTagName('playbackURI')
i = 0
for element in elements:
video = element.firstChild.data
video = video.replace("rtsp://10.20.30.1", "rtsp://username:[email protected]")
video = video.replace("\n", "")
size = video.rsplit('=', 1)[1]
os.system("ffmpeg -i '" + video + "' -max_muxing_queue_size " + size + "0 videos/" + trackID + "." + startTime + "." + endTime + "." + str(i+1) + ".mp4;")
i += 1
exit
- After the search has been performed and results stored in an XML file, this script is called to download the videos.
- It reads the XML file and extracts the video playback URLs.
- For each video, it appends the required credentials for login and uses
ffmpeg
to download the video. - Downloaded videos are saved with a filename indicating track ID, start time, end time, and a unique index.
Usage:
To use these scripts, you’ll need to modify the following parts:
- Update the
base
variable ingenerate.py
to set the desired starting date. - Adjust the
tracks
list ingenerate.py
to specify the camera tracks you want to search. - Replace
username
,password
, and the IP address in thecurl
command insearch.py
with your NVR’s credentials and address. - Ensure you have
ffmpeg
installed on your system for video downloading.
With these Python scripts, you can automate the process of searching for and downloading recorded videos from a HIKVision NVR. This can significantly simplify video retrieval tasks for security professionals, saving time and effort in managing surveillance footage. By customizing and expanding upon these scripts, you can further enhance your video management capabilities and streamline your security operations.
This post is also available in: Greek