You can see all the flags here.

It looks like cv2.CAP_OPENCV_MJPEG is what you are looking for.


The following test creates MJPEG synthetic AVI video file, and reads the video using cv2.CAP_OPENCV_MJPEG backend:

import numpy as np
import cv2

#availableBackends = [cv2.videoio_registry.getBackendName(b) for b in cv2.videoio_registry.getBackends()]
#print(availableBackends)

print('cv2.CAP_OPENCV_MJPEG = ' + str(cv2.CAP_OPENCV_MJPEG))


intput_filename = 'vid.avi'

# Generate synthetic video files to be used as input:
###############################################################################
width, height, n_frames = 640, 480, 100  # 100 frames, resolution 640x480

# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-100*len(str(i+1)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)  # Green number
    synthetic_out.write(img)

synthetic_out.release()
###############################################################################


# Read the video using CV_MJPEG backend
###############################################################################
cap = cv2.VideoCapture(intput_filename, cv2.CAP_OPENCV_MJPEG)

# Keep iterating break
while True:
    ret, frame = cap.read()  # Read frame from first video

    if ret:
        cv2.imshow('frame', frame)  # Display frame for testing
        cv2.waitKey(100) #Wait 100msec (for debugging)
    else:
        break

cap.release() #Release must be inside the outer loop
###############################################################################
Answer from Rotem on Stack Overflow
Top answer
1 of 1
3

You can see all the flags here.

It looks like cv2.CAP_OPENCV_MJPEG is what you are looking for.


The following test creates MJPEG synthetic AVI video file, and reads the video using cv2.CAP_OPENCV_MJPEG backend:

import numpy as np
import cv2

#availableBackends = [cv2.videoio_registry.getBackendName(b) for b in cv2.videoio_registry.getBackends()]
#print(availableBackends)

print('cv2.CAP_OPENCV_MJPEG = ' + str(cv2.CAP_OPENCV_MJPEG))


intput_filename = 'vid.avi'

# Generate synthetic video files to be used as input:
###############################################################################
width, height, n_frames = 640, 480, 100  # 100 frames, resolution 640x480

# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-100*len(str(i+1)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)  # Green number
    synthetic_out.write(img)

synthetic_out.release()
###############################################################################


# Read the video using CV_MJPEG backend
###############################################################################
cap = cv2.VideoCapture(intput_filename, cv2.CAP_OPENCV_MJPEG)

# Keep iterating break
while True:
    ret, frame = cap.read()  # Read frame from first video

    if ret:
        cv2.imshow('frame', frame)  # Display frame for testing
        cv2.waitKey(100) #Wait 100msec (for debugging)
    else:
        break

cap.release() #Release must be inside the outer loop
###############################################################################
🌐
Intel Community
community.intel.com › t5 › Intel-Distribution-of-OpenVINO › cv-VideoCapture-with-flag-cv-CAP-INTEL-MFX-fails-to-open › td-p › 1134125
Solved: cv::VideoCapture with flag cv::CAP_INTEL_MFX fails to open - Intel Community
July 26, 2019 - But the capture of frame from camera with openCV still executes on CPU. In order to offload even this to GPU so that frame directly gets captured into memory accessible by integrated gfx I was trying to use Intel media SDK support. But the call cap.open(0, cv::CAP_INTEL_MFX) fails.
🌐
Stack Overflow
stackoverflow.com › questions › 56534609 › hardware-accelerated-decoding-with-opencv-and-python-on-windows-msmt-intelmfx
Hardware Accelerated Decoding with OpenCV and Python on Windows (MSMT / IntelMFX / FFMPEG / any backend) - Stack Overflow
{ video=cv2.VideoCapture('input_video.264', cv2.CAP_INTEL_MFX ) video=cv2.VideoCapture('input_video.264', cv2.CAP_FFMPEG ) video=cv2.VideoCapture('input_video.264', cv2.CAP_GSTREAMER ) video=cv2.VideoCapture('input_video.264', cv2.CAP_MSMF ) video=cv2.VideoCapture('input_video.264', cv2.CAP_DSHOW ) }
🌐
OpenCV
docs.opencv.org › 3.4.20 › d4 › d15 › group__videoio__flags__base.html
OpenCV: Flags for video I/O
VideoCapture API backends identifier · Select preferred API for a capture object. To be used in the VideoCapture::VideoCapture() constructor or VideoCapture::open()
🌐
Intel Community
community.intel.com › t5 › Intel-Distribution-of-OpenVINO › OpenCV-with-VAAPI-video-capture-support › td-p › 1150740
OpenCV with VAAPI video capture support . - Intel Community
November 2, 2018 - CAP_INTEL_MFX · Python: cv.CAP_INTEL_MFX · Intel MediaSDK. 0 Kudos · Copy link · Reply · Maksim_S_Intel · Employee · ‎11-02-2018 01:25 AM · 2,724 Views · Mark as New · Bookmark · Subscribe · Mute · Subscribe to RSS Feed · Permalink · Print ·
🌐
Fossies
fossies.org › linux › opencv › modules › videoio › test › test_mfx.cpp
OpenCV: modules/videoio/test/test_mfx.cpp | Fossies
October 11, 2020 - 2 // It is subject to the license terms in the LICENSE file found in the top-level directory 3 // of this distribution and at http://opencv.org/license.html 4 5 #include "test_precomp.hpp" 6 7 namespace opencv_test { namespace { 8 9 TEST(videoio_mfx, read_invalid) 10 { 11 if (!videoio_registry::hasBackend(CAP_INTEL_MFX)) 12 throw SkipTestException("MediaSDK backend was not found"); 13 14 VideoCapture cap; 15 ASSERT_NO_THROW(cap.open("nonexistent-file", CAP_INTEL_MFX)); 16 ASSERT_FALSE(cap.isOpened()); 17 Mat img; 18 ASSERT_NO_THROW(cap >> img); 19 ASSERT_TRUE(img.empty()); 20 } 21 22 TEST(vide
🌐
GitHub
github.com › opencv › opencv › issues › 13306
VideoWriter with apiPreference CAP_INTEL_MFX fourcc('H','2','6','4') don't compress video · Issue #13306 · opencv/opencv · GitHub
November 27, 2018 - int fourcc = cv::VideoWriter::fourcc('H','2','6','4'); cv::VideoWriter writer("filename.mp4", cv::INTEL_MFX, fourcc, 30, cv::Size(1280,720), true); What interesting is, when I run the code, it will show a line with MFX: unsupported extension: /dev/video0, but the video could be written in a new file.
Author: opencv
🌐
GitHub
github.com › opencv › opencv › blob › 4.x › modules › videoio › test › test_mfx.cpp
opencv/modules/videoio/test/test_mfx.cpp at 4.x · opencv/opencv · GitHub
ASSERT_NO_THROW(cap.open("nonexistent-file", CAP_INTEL_MFX)); ASSERT_FALSE(cap.isOpened()); Mat img; ASSERT_NO_THROW(cap >> img); ASSERT_TRUE(img.empty()); } · TEST(videoio_mfx, write_invalid) { if (!videoio_registry::hasBackend(CAP_INTEL_MFX)) throw SkipTestException("MediaSDK backend was not found"); ·
🌐
Stack Overflow
stackoverflow.com › questions › 67584120 › decode-encode-hevc-with-opencv-python-with-intel-media-sdk-backend
Decode/encode HEVC with OpenCV Python with Intel Media SDK backend - Stack Overflow
writer = cv.VideoWriter('output.hevc', apiPreference = cv.CAP_INTEL_MFX, fourcc = cv.VideoWriter_fourcc('h','v','c','1'), fps = 20.0, frameSize = (640,480)) # MFX: Unsupported FourCC: hvc1 (0x31637668) # 'hevc' or 'h265' give same error code # write to 'output.mp4' with 'mp4v' yield a different error code 0x7634706d ·
Find elsewhere
🌐
OpenCV
forum.opencv.org › python
Reading and Writing Videos: Python on GPU with CUDA - VideoCapture and VideoWriter - Python - OpenCV
December 5, 2020 - I’m trying to crop a video, using Python 3+, by reading it frame-by-frame and write certain frames to a new video. I want to use GPU to speed up this process, as for a 1h video, it would take my CPU ~24h to complete. My understanding is, Reading a video using CPU: vid = cv2.VideoCapture(vid_path) ...
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 208283 › problem-with-intel-media-sdk-backend-for-videocapture
Problem with Intel Media SDK Backend for VideoCapture - OpenCV Q&A Forum
February 1, 2019 - Simply adding the CAP_INTEL_MFX backend Flag to the writer initialization as decribed in https://github.com/opencv/opencv/wiki..., brought that down to about 70% usage:
🌐
GitHub
github.com › opencv › opencv › issues › 16545
VideoWroter using CAP_INTEL_MFX doesn't seem to close the file properly. Can't get info of the file length, etc. · Issue #16545 · opencv/opencv · GitHub
February 10, 2020 - int compress = VideoWriter::fourcc('H', '2', '6', '5'); m_videoWriter VideoWriter(fileName + ".mp4", cv::CAP_INTEL_MFX, compress, Camera::recordFPS, Size(1920, 1080), true);
Author: opencv
🌐
Fossies
fossies.org › linux › opencv › modules › videoio › include › opencv2 › videoio.hpp
OpenCV: modules/videoio/include/opencv2/videoio.hpp | Fossies
267 268 VIDEO_ACCELERATION_D3D11 = 2, //!< DirectX 11 269 VIDEO_ACCELERATION_VAAPI = 3, //!< VAAPI 270 VIDEO_ACCELERATION_MFX = 4, //!< libmfx (Intel MediaSDK/oneVPL) 271 }; 272 273 //! @} Hardware acceleration support 274 275 /** @name IEEE 1394 drivers 276 @{ 277 */ 278 279 /** @brief Modes of the IEEE 1394 controlling registers 280 (can be: auto, manual, auto single push, absolute Latter allowed with any other mode) 281 every feature can have only one mode turned on at a time 282 */ 283 enum { CAP_PROP_DC1394_OFF = -4, //!< turn the feature off (not controlled manually nor automatically).
🌐
Intel Community
community.intel.com › t5 › Intel-Distribution-of-OpenVINO › cv2-VideoCapture-rtsp-192-168-60-70-channel1-gets-MFX › td-p › 1237844
Solved: cv2.VideoCapture('rtsp://192.168.60.70/channel1') gets MFX: Unsupported extension error - Intel Community
December 29, 2020 - cv2.VideoCapture('rtsp://192.168.60.70/channel1') gets MFX: Unsupported extension error · 6582 Discussions · Subscribe · More actions · Subscribe to RSS Feed · Mark Topic as New · Mark Topic as Read · Float this Topic for Current User · Bookmark · Subscribe ·
🌐
Intel Community
community.intel.com › t5 › Intel-Distribution-of-OpenVINO › OpenCV-4-1-2-openvino-Unsupported-Extension-Error-Solved › m-p › 1129118
Solved: MFX: Unsupported extension: - Intel Community
November 16, 2021 - out_video=cv2.VideoWriter(out_video_name,cv2.VideoWriter_fourcc('M','J','P','G'), fps, (frame_width,frame_height)) ... MFX: Unsupported extension: .\test_images\test_video.avi MFX: Unsupported FourCC: MJPG (0x47504a4d) [ERROR:0] global C:\jenkins\workspace\OpenCV\OpenVINO\build\opencv\modules\videoio\src\cap.cpp (392) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception: