low storage video in opencv python code example
Example 1: how to read video in opencv python
import numpy as np
import cv2
cap = cv2.VideoCapture('videos/wa.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Example 2: read live video from usb opencv python
import cv2
cap = cv2.VideoCapture()
cap.open(0, cv2.CAP_DSHOW)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Example 3: get video duration opencv python
import cv2
cap = cv2.VideoCapture("./video.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frame_count/fps
print('fps = ' + str(fps))
print('number of frames = ' + str(frame_count))
print('duration (S) = ' + str(duration))
minutes = int(duration/60)
seconds = duration%60
print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))
cap.release()
Example 4: cv2 get framerete video
fps = capture.get(cv2.CAP_PROP_FPS)