Ora

What Video Formats Are Supported by OpenCV?

Published in Video Format Support 5 mins read

OpenCV, a powerful computer vision library, offers extensive support for various video formats, primarily driven by its integration with robust multimedia frameworks. For reading video files, OpenCV leverages libraries like FFmpeg and GStreamer, enabling compatibility with a wide array of formats. For writing video files, while more direct control is often available with formats like AVI, support for other formats can also be achieved through underlying codecs and backends.

Video Reading Capabilities: Broad Support Through Backends

OpenCV's ability to read a multitude of video formats largely stems from its dependency on external multimedia libraries. The most prominent of these is FFmpeg, a comprehensive solution for handling audio and video. When OpenCV is compiled with FFmpeg or GStreamer support (which is typical for most installations), it can seamlessly process a vast range of video files.

Commonly Supported Formats for Reading:

  • MP4 (.mp4): A widely used container format, often employing H.264 or H.265 (HEVC) codecs.
  • MKV (.mkv): A flexible, open standard container format that can hold a virtually unlimited number of video, audio, picture, or subtitle tracks in one file.
  • AVI (.avi): A legacy container format, still widely supported for its simplicity and broad compatibility.
  • MOV (.mov): Apple's QuickTime File Format, commonly used in macOS environments.
  • WebM (.webm): An open, royalty-free media file format designed for the web, often using VP8/VP9 video codecs.
  • FLV (.flv): Flash Video format, historically used for streaming video over the internet.
  • WMV (.wmv): Windows Media Video, a Microsoft-developed video compression format.

The actual formats and codecs supported for reading depend on the specific FFmpeg/GStreamer version and the codecs they were compiled with on your system.

Video Writing Capabilities: Focusing on AVI and Backend Reliance

When it comes to writing video files, OpenCV provides mechanisms to create video streams from sequences of images. For reliable and direct control over output, especially when creating a video stream with specific characteristics, the AVI file format is robustly supported.

Key Aspects for Writing Video Streams:

  • AVI Format Preference: For many direct video creation scenarios and to ensure broad compatibility without relying on specific system codecs, the AVI format serves as a foundational and widely supported option within OpenCV.
  • Stream Parameters: When creating an AVI video stream, you can explicitly define crucial parameters:
    • Size of the input video frames: This specifies the (width, height) resolution of the frames you are writing.
    • Framerate of the created video stream: This dictates how many frames per second (FPS) the output video will play at.
  • Supported Surface Formats for Input Frames: For the input frames fed into the video writer, OpenCV supports various surface formats, ensuring flexibility in how pixel data is handled before encoding. These include:
    • SF_UYVY
    • SF_YUY2
    • SF_YV12
    • SF_NV12
    • SF_IYUV
    • SF_BGR (Blue, Green, Red - a common format for images in OpenCV)
    • SF_GRAY (Grayscale)

While AVI is a primary format for direct stream creation, OpenCV's VideoWriter can also leverage the underlying FFmpeg backend (if available) to write other formats like MP4, MKV, or WebM. However, this often requires specifying the correct four-character code (fourcc) for the desired codec and ensuring that the necessary codecs are available and configured correctly on the operating system.

Example: Writing an AVI Video

import cv2
import numpy as np

# Define video properties
width, height = 640, 480
fps = 30
fourcc = cv2.VideoWriter_fourcc(*'MJPG') # Codec for AVI (Motion JPEG)

# Create a VideoWriter object for AVI
out = cv2.VideoWriter('output_video.avi', fourcc, fps, (width, height))

if not out.isOpened():
    print("Error: Could not open video writer.")
else:
    for i in range(100): # Write 100 frames
        # Create a dummy frame (e.g., a colored rectangle on a black background)
        frame = np.zeros((height, width, 3), dtype=np.uint8)
        color = (i * 2 % 255, (i + 50) * 2 % 255, (i + 100) * 2 % 255)
        cv2.rectangle(frame, (i*5 % (width-50), i*3 % (height-50)), 
                      (i*5 % (width-50) + 50, i*3 % (height-50) + 50), color, -1)

        # Write the frame
        out.write(frame)

    # Release the video writer
    out.release()
    print("Video 'output_video.avi' created successfully.")

Factors Influencing Video Support

The actual range of video formats supported by your OpenCV installation can depend on several key factors:

  • FFmpeg/GStreamer Integration: Whether OpenCV was built with support for these powerful multimedia libraries is crucial. Most pre-built binaries include this.
  • Operating System Codecs: The codecs installed on your operating system (e.g., K-Lite Codec Pack on Windows, libavcodec on Linux) play a significant role, especially for less common formats or specific codec variations.
  • OpenCV Build Configuration: Custom OpenCV builds might exclude certain modules or dependencies, affecting format support.

Summary of Video Format Support

To provide a clear overview, the table below summarizes OpenCV's typical video format support for both reading and writing operations.

Feature Video Reading Video Writing
Primary Mechanism FFmpeg, GStreamer (if compiled with support) Direct AVI support, FFmpeg/GStreamer (for other formats)
Common Formats MP4, MKV, AVI, MOV, WebM, FLV, WMV, etc. (wide range) Primarily AVI for direct control; MP4, MKV, etc., possible with correct FOURCC and backend codecs.
Key Dependencies FFmpeg libraries (libavcodec, libavformat, libswscale, etc.), GStreamer FFmpeg libraries (for non-AVI), system codecs (e.g., x264 for H.264), OpenCV build configuration.
Control Parameters N/A (reads existing stream properties) AVI Specifics: Requires specifying size of input video frames, framerate of the created video stream. Supports input frame surface formats: SF_UYVY, SF_YUY2, SF_YV12, SF_NV12, SF_IYUV, SF_BGR, SF_GRAY.
Flexibility Very High (decodes almost anything FFmpeg/GStreamer supports) Moderate to High (AVI is reliable; other formats depend heavily on codec availability and correct FOURCC setup, which might require experimentation to find compatible combinations for your system and OpenCV build).

By understanding these nuances, developers can effectively utilize OpenCV for various video processing tasks, ensuring compatibility with their media pipelines.