TABLE OF CONTENTS1. Overview
2. How to Implement the Code Snippet
3. Conclusion
4. About CloudThat
Overview
Closed-circuit TV, also known as video surveillance, is an abbreviation of CCTV. Contrary to “normal” television which is broadcast to the general public, “closed circuit” television is only broadcast to a limited (closed) number monitors. CCTV networks are used widely to detect and discourage criminal activity, and to record traffic violations. However, they can also be used for other purposes.
CCTV cameras are used to protect homes, shops, airports, roads, and public transport. CCTV allows you to capture live video and store them on your devices, or send data to the cloud if needed.
Most CCTV camera data can be saved locally, so storing recorded films on the device takes up a lot of space. Most CCTV providers have to delete captured recordings frequently. It is best to backup the CCTV video recordings to the cloud. This blog will show you how to backup the captured videos to the AWS S3 using the OpenCV and other libraries.

You must have created an AWS account, and logged into it. Next, go to IAM Service to create an IAM user with AmazonS3FullAccess permission. When creating a user, be sure to note the Access Key ID (and AWS Secret Access Key).
Click on AWS S3 to select the Create Bucket option. Enter webcambucket for the bucket name. Leave the rest of the options as is. Scroll down and click Create Bucket. It is obvious that a bucket has been created with the name webcambucket. If you get an error saying that a bucket with the same title already exists, change its name and make a note.
How to Implement the Code Snippet
First, import the libraries you need as described below.
# Python Code to upload Webcam videos into AWS S3import Cv2 from Datetime import Datetime import pytz Import Boto3 Import Os from Threading Import Thread
The Python code can be divided into two sections. The first step is to capture frames, create a video file, then the second step is publishing the video file to AWS S3. The following function allows you to upload the file to AWS S3. Replace the aws_access_key_id and aws_secret_access_key with the values you noticed before, and specify the bucket name in the variable bucket_name.
#function for uploading the file into S3def upload_to_s3(): ct = boto3.client(‘s3′, aws_access_key_id=’XXXXXXXXXXXXXXXXXXX’, aws_secret_access_key=’XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’) sec1 = int(round(time.time()))#start the timer for uploading the files into S3 while 1: if int(round(time.time())) >=sec1+5: sec1 = int(round(time.time()))#start the timer for uploading the files into S3 print(“file going to upload”) for file in os.listdir(): if ‘.avi’ in file and file_name1 != file: bucket_name = ‘webcam-bucket’ #mention bucket name which you created in S3 path = ‘webcammer/’#mention path where the files need to be upload upload_file_key = path + str(file) ct.upload_file(file, bucket_name, upload_file_key) print(“file uploaded successfully…” + str(file)) os.remove(file) print(“file upload done”) Thread(target = upload_to_s3).start()
This code will create live stream video files using the webcam. The file name must contain the date and the time. For example, 22-01-2022 11.50.50.avi It makes it much easier to discover the records.#capturing frames and make videoprint(“libraries are imported”) tz = pytz.timezone(‘Asia/Kolkata’) #reading