Skip to main content

OpenCV, Streams and Video Files

Shenzhen, China

Github Repo

Setup OpenCV

Create and activate a virtual work environment:

python -m venv .env
source .env/bin/activate
python -m pip install --upgrade pip

Add a file dependencies.txt with all project pip dependencies:

opencv-python
numpy

Install all dependencies with:

pip install -r dependencies.txt

Webcam Capture

import cv2

webcam = cv2.VideoCapture(0)

while (True):
ret_, frame = webcam.read()
cv2.imshow("webcam", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
webcam.release()
break

cv2.destroyAllWindows()

IP Camera Capture

import cv2
import os

RTSP_URL = 'rtsp://admin:instar@192.168.2.19/livestream/13'

os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' # Use tcp instead of udp if stream is unstable

cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)

if not cap.isOpened():
print('Cannot open RTSP stream')
exit(-1)

while True:
success, img = cap.read()
cv2.imshow('RTSP stream', img)

if cv2.waitKey(1) & 0xFF == ord('q'): # Keep running until you press `q`
cap.release()
break

cv2.destroyAllWindows()

Video Stream Recording

import cv2
import os

RTSP_URL = 'rtsp://admin:instar@192.168.2.19/livestream/13'

os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' # Use tcp instead of udp if stream is unstable

cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)

if not cap.isOpened():
print('Cannot open RTSP stream')
exit(-1)

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fps = 15

video_codec = cv2.VideoWriter_fourcc(*'XVID')
video_output = cv2.VideoWriter('recording/captured_video.mp4', video_codec, fps, (frame_width, frame_height))

while True:
ret, frame = cap.read()

if ret == True:
video_output.write(frame)
cv2.imshow("Video Recording", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

else:
break

cap.release()
video_output.release()
cv2.destroyAllWindows()
print('Video was saved!')

Playback Video Files

import cv2

cap = cv2.VideoCapture("recording/captured_video.mp4")

while (True):
ret, frame = cap.read()
cv2.imshow("Video Recording", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()