Ask Your Question

Revision history [back]

How to open an M-JPEG video in an AVI container using Python 2.7.5 with OpenCV 2.4.6?

I'm trying to open a *.avi file with OpenCV using Python(x, y) and every time I run the following code, the file fails to open.

import cv2
from cv2 import cv

# Set up
fn = "./video.avi"
v = cv2.VideoCapture()

# Configure codec:
# From ffprobe, codec is M-JPEG ==> MJPG;
codec = cv.CV_FOURCC("M", "J", "P", "G")
v.set(cv.CV_CAP_PROP_FOURCC, codec)

# Test
v.open(fn)

print fn
if not v.isOpened():
    print "Video failed to open"
else:
    print "Video opened!"

The following is the output of running

ffprobe video.avi

Output:

libavutil      52. 52.100 / 52. 52.100
libavcodec     55. 41.100 / 55. 41.100
libavformat    55. 21.100 / 55. 21.100
libavdevice    55.  5.100 / 55.  5.100
libavfilter     3. 90.102 /  3. 90.102
libswscale      2.  5.101 /  2.  5.101
libswresample   0. 17.104 /  0. 17.104
libpostproc    52.  3.100 / 52.  3.100
Input #0, avi, from 'L4R01CA_T05032300.avi':
  Duration: 00:00:06.01, start: 0.000000, bitrate: 16136 kb/s
    Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc), 720x480, 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 256 kb/s

Anyone have any ideas as to why OpenCV cannot open this file? I have also tried to open the file without setting cv.CV_CAP_PROP_FOURCC to MJPG with the same results.

Thank you.