Save the video stream from your camera using OpenCV

OpenСV offers great opportunities when working with video and images. For example, saving streaming video from a video camera in real time.

All examples were written for OpenCV version 3.4.2

Example on python3.6.5:

import cv2
# function video capture
cameraCapture = cv2.VideoCapture(0)
# rame rate or frames per second
fps = 30

# Width and height of the frames in the video stream
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),

Create a VideoWriter object. We should specify the output file name (eg: MyOutput.avi). Then we should specify the FourCC. Then number of frames per second (fps) and frame size should be passed. May specify isColor flag. If it is True, encoder expect color frame, otherwise it works with grayscale frame.
FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in It is platform dependent.

videoWriter = cv2.VideoWriter('MyOutput.avi', 
    cv2.VideoWriter_fourcc('I','4','2','0'), fps, size)

success, frame =

# some variable
numFramesRemaining = 10*fps - 1

# loop until there are no more frames and variable > 0
while success and numFramesRemaining > 0:
    success, frame =
    numFramesRemaining -= 1

#Closes video file or capturing device

Examples of different meanings FourCC:

  • cv2.VideoWriter_fourcc(‘I’,’4’,’2’,’0’): This option is an uncompressed YUV encoding, 4:2:0 chroma subsampled. This encoding is widely compatible but produces large files. The file extension should be .avi.
  • cv2.VideoWriter_fourcc(‘P’,’I’,’M’,’1’): This option is MPEG-1 The file extension should be .avi.
  • cv2.VideoWriter_fourcc(‘X’,’V’,’I’,’D’): This option is MPEG-4 and a preferred option if you want the resulting video size to be average. The file extension should be .avi.
  • cv2.VideoWriter_fourcc(‘T’,’H’,’E’,’O’): This option is Ogg Vorbis. The file extension should be .ogv.
  • cv2.VideoWriter_fourcc(‘F’,’L’,’V’,’1’): This option is a Flash video. The file extension should be .flv.
  • cv2.VideoWriter_fourcc(‘U’,’2’,’6’,’3’): H263 codec
  • cv2.VideoWriter_fourcc(‘I’,’2’,’6’,’3’): H263I codec
  • cv2.VideoWriter_fourcc(‘D’,’I’,’V’,’X’): MPEG-4 codec
  • cv2.VideoWriter_fourcc(‘M’,’P’,’4’,’2’): MPEG-4.2 codec
  • cv2.VideoWriter_fourcc(‘D’,’I’,’V’,’3’): MPEG-4.3 codec

The same code written in C ++ and gcc 7.3.0:

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(){

  // Create a VideoCapture object and use camera to capture the video
  VideoCapture cap(0);

  int fps = 30;

  // Check if camera opened successfully
   	cout << "Error opening video stream" << endl;
        return -1;

  // Default resolutions of the frame are obtained.The default resolutions are system dependent.
  int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
  int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

  // Define the codec and create VideoWriter object.The output is stored in 'outcpp.avi' file.
  VideoWriter video("outcpp.avi",CV_FOURCC('I','4','2','0'), fps, Size(frame_width,frame_height));

int numFramesRemaining = (10*fps - 1);

  while (1 and numFramesRemaining > 0) {

    Mat frame;

    // Capture frame-by-frame
    cap >> frame;

    // If the frame is empty, break immediately
    if (frame.empty())

    // Write the frame into the file 'outcpp.avi'

    // Display the resulting frame
    imshow( "Frame", frame );

    char c = (char)waitKey(1);

    // possible use ESC for break
    //if( c == 27 )
    //  break;

    numFramesRemaining -= 1;

  // When everything done, release the video capture and write object

  // Closes all the frames
  return 0;


There are 3 comments below.

Post your comment

  • Jan. 5, 2022, 10:49 a.m. - Klee  
    I have tried all FourCC from above, but none of them works for me. What are the file extensions which has to be added for the last few codecs? And do I need specific drivers for the different options and if I need some where should I get it?
    Jan. 6, 2022, 9:23 p.m. - Ihar moderator  
    Drivers are needed. For my Linux machine, I found in the repository. To install codec in Ubuntu need run command: sudo apt-get install ffmpeg x264 libx264-dev
  • Sept. 23, 2021, 9:38 p.m. - Klaus  
    Thanks for the blog!
  • Search
    Popular Posts