import cv2
from IPython.display import HTML
from scripts.jupyter.helpers import play_video
# Download some sample movie
!wget -nc -nv http://techslides.com/demos/sample-videos/small.mp4 -P ../downloads
2018-12-31 12:23:42 URL:http://techslides.com/demos/sample-videos/small.mp4 [383631/383631] -> "../downloads/small.mp4" [1]
# Let's play it
play_video('downloads/small.mp4')
# Load the video with OpenCV and check it's properties
cap = cv2.VideoCapture('downloads/small.mp4')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
print("Width={} Height={} FPS={}".format(width, height, fps))
Width=560 Height=320 FPS=30
# Convert video to grayscale and save it to file
# Seek to first frame in input video
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
# Open video writer
vid = cv2.VideoWriter('../output/small_bw.mp4',
cv2.VideoWriter_fourcc(*'H264'),
fps,
(width, height),
isColor=False)
# Initialize number of frames counter
frames_count = 0
while(True):
ret, frame = cap.read()
frames_count += 1 if ret else 0
if ret:
# Convert frame from color to grayscale and write it
frame_bw = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
vid.write(frame_bw)
else:
vid.release()
break
print("Processed {} frames".format(frames_count))
Processed 166 frames
# Check the result
play_video('output/small_bw.mp4')