Goal


  • You will learn how to read, write, and display a video using OpenCV
  • How to capture from camera and display it using OpenCV.
  • You will learn the cv2.VideoCapture() and cv2.VideoWrite() functions.

In this tutorial you will learn two video capture formats. These are camera and from a file.


Video Capture From a File


You can use OpenCV cv2.VideoCapture function to capture video from a file. This cv2.VideoCapture function gets a parameter. This parameter determines whether we get the video from a camera or a file. If you want to read a video from a file you need to set a video file name into the cv2.VideoCapture() function. To read a video from the file you can use the following code.

import numpy as np
import cv2
# Load a video from file
capture = cv2.VideoCapture("line_detection.mp4")
while True:
    # capture frame-by-frame from video file
    ret, frame = capture.read() 
    # show the frame on the screen
    cv2.imshow("frame", frame)
    
    if cv2.waitKey(27) & 0xFF == ord('q'):
        break
# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()

Video Capture From Camera


We often use OpenCV for live video streaming. For instance, self-driving cars need to process real-time video. OpenCV provides a simple interface for us to get video from the camera. To get a video from the camera you can use cv2.VideoCapture object. But, you use two sources to get video from the camera. To use your computer primary camera, you should use “0” parameters. If you want to use your external webcam, you should use the “1” parameter. To read a video from the primary camera you can use the following code.

import numpy as np
import cv2
# Load a video from file
capture = cv2.VideoCapture(0)
while True:
    # capture frame-by-frame from video file
    ret, frame = capture.read() 
    # show the frame on the screen
    cv2.imshow("frame", frame)
    
    if cv2.waitKey(27) & 0xFF == ord('q'):
        break
# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()

To read a video from external webcam you can use the following code.

import numpy as np
import cv2
# Load a video from file
capture = cv2.VideoCapture(1)
while True:
    # capture frame-by-frame from video file
    ret, frame = capture.read() 
    # show the frame on the screen
    cv2.imshow("frame", frame)
    
    if cv2.waitKey(27) & 0xFF == ord('q'):
        break
# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()

Saving a Video


You can save a video that you get from the camera using OpenCV. You can save a video that you get from the camera using OpenCV. To do this you can use cv2.VideoWriter function. cv2.VideoWriter requires four parameters. video name, fourcc, fps, and frame size.


Fourcc


It is called the “four-character code”, ie the four-character code. The purpose of FourCC is to define codecs in media data with four characters, that is, to create a standard definition format. A character not in the ASCI table cannot be used when defining the FourCC code. The best known are DIVX, XVID, H264, etc.

The Fourcc format is as follows.

8 – 8 – 8 – 8 = 4byte – 32bit

We will use fourcc to define codecs in OpenCV.

You can check the current fourcc codec list here.


To write a video you can use the following code.

import numpy as np
import cv2
# To load video from camera
capture = cv2.VideoCapture(0)
# To define video codec format
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# To define video writer variable
output = cv2.VideoWriter('output_video.avi', fourcc, 25, (640,480)) # 3. parameter is fps, 4. parameter is video frame size.
while(capture.isOpened()): #To check if the camera is turned on.
 ret, frame =  capture.read()
 if ret == True:
     # write the frame
     output.write(frame)
     cv2.imshow("frame", frame) # To dispay video
     if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
capture.release() # To release the capture
output.release() # To release the output
cv2.destroyAllWindows()