Capturing Video Stream Using OpenCV in Python: A Guide

The simplest method to display video streaming in a tkinter frame is through the PIL library. Are there any specific types of video streams that OpenCV can capture? Is there a solution that doesn’t require a change of scheme or transcoding? Thank you!


I am interested in utilizing Python’s OpenCV to handle an mms video stream originating from an external source (
IP camera
), specifically a traffic monitor. The stream can be accessed through either mms or mmst schemes.


It is compatible with both VLC and Windows Media Player.


The functionality is limited to VLC only. Despite my attempts to re-stream with FFmpeg and VLC and alter the scheme to HTTP, the issue persisted.

From my understanding, the stream is encoded using
Windows Media Video
by mms. Unfortunately, appending ‘.mjpeg’ to the URI did not work. I am still searching for the supported streaming formats for OpenCV.

Here’s my code –

import cv2, platform
#import numpy as np
cam = "mms://"
#cam = 0 # Use  local webcam.
cap = cv2.VideoCapture(cam)
if not cap:
    print("!!! Failed VideoCapture: invalid parameter!")
    # Capture frame-by-frame
    ret, current_frame =
    if type(current_frame) == type(None):
        print("!!! Couldn't read frame!")
    # Display the resulting frame
    if cv2.waitKey(1) & 0xFF == ord('q'):
# release the capture

What type of video streams can be captured by OpenCV? Is there a way to solve this without altering the scheme or transcoding? Essentially, what am I missing?


The system comprises Python version 2.7.8 and OpenCV version 2.4.9, both designed for x86 architecture and compatible with Windows 7 operating system running on a 64-bit platform.


The issue was resolved by utilizing FFmpeg and FFserver, with the caveat that FFserver is solely compatible with Linux. Ryan provided a recommendation to incorporate Python code from this source, which was implemented in the solution.

Flow is as follows –

  • Begin the desired configuration’s FFserver background process, which in this case is mjpeg.
  • The input for FFmpeg is the mmst stream which is then directed to the localhost as the output stream.
  • Execute the
    python script
    command to initiate the localhost stream and then proceed with decoding each frame individually.

Run FFserver

ffserver -d -f /etc/ffserver.conf

On a second terminal run FFmpeg

ffmpeg -i mmst:// http://localhost:8090/cam2.ffm

In this instance, the Python code will initiate a video stream by launching a window.

import cv2, platform
import numpy as np
import urllib
import os
cam2 = "http://localhost:8090/cam2.mjpeg"
while True:
    # to read mjpeg frame -
    a = bytes.find('xffxd8')
    b = bytes.find('xffxd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
    frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
    # we now have frame stored in frame.
    # Press 'q' to quit 
    if cv2.waitKey(1) & 0xFF == ord('q'):

ffserver.config –

Port 8090
MaxClients 10
MaxBandWidth 50000
CustomLog -

    File /tmp/cam2.ffm
    FileMaxSize 1G
    ACL allow
    ACL allow localhost

    Feed cam2.ffm
    Format mpjpeg
    VideoFrameRate 25
    VideoBitRate 10240
    VideoBufferSize 20480
    VideoSize 320x240
    VideoQMin 3
    VideoQMax 31
    Strict -1

    Format status
    # Only allow local people to get the status
    ACL allow localhost
    ACL allow


Please be aware that the configuration of ffserver could benefit from further refinement, however, it is currently performing quite satisfactorily as it produces a nearly identical frame to the source with only a slight
frame freeze

Frequently Asked Questions