OpenCV VideoWriter Error "dimensions too large for MPEG-4"

168
April 01, 2022, at 08:50 AM

I have some frames (dimensions: 8192x2160) that are generated from concatenating two 4096x2160 frames side by side. When writing these frames to a video file using OpenCV VideoWriter, I get this error: dimensions too large for MPEG-4

Here is my code:

video_name = "vid.mp4"
images = sorted([img for img in os.listdir(images_folder) if img.endswith(".png")])
frame = cv2.imread(os.path.join(images_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, FRAME_RATE, (width, height))
for image in images:
    video.write(cv2.imread(os.path.join(images_folder, image)))
cv2.destroyAllWindows()
video.release()

This didn't work either:

video_name = "output.avi"
video = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*'DIVX'), 10, (width, height))

Does anyone have any suggestions?

Answer 1

DivX is related to MPEG-4 Part 2 and H.263.

Those video format specifications can't handle that size of frame. They have limits by design, that are below your requirements. No codec (implementation) can work around that.

If you need that size of frame handled, you'll need a different format.

You'd need to research available formats/codecs and see if they're suitable.

More modern formats/codecs have a better chance of being designed for large frame sizes. HEVC, AV1, ... might be suitable.

Some non-modern formats/codecs might not have this limitation either. MJPEG (MJPG) will work (just tested this). It's built into OpenCV, always there. Ffmpeg also has an implementation, so that's a very compatible choice of codec.

Answer 2

Use a different codec or scale each frame down with cv2.resize.

Rent Charter Buses Company
READ ALSO
What happens to selenium code simulating browser actiions when run in non GUI unix box? Will chrome windows open?

What happens to selenium code simulating browser actiions when run in non GUI unix box? Will chrome windows open?

I built my first selenium on a mac machine, so with help of chromedriver, I see a new tab opened by my script

131
Echarts line chart symbol formatter

Echarts line chart symbol formatter

above code print like this: https://istack

117
OSError: [WinError 740] The requested operation requires elevation (pytesseract.image_to_string)

OSError: [WinError 740] The requested operation requires elevation (pytesseract.image_to_string)

I am having a simple code that has an image called "1png" and I want to convert it from Image to Text using pytesseract

137
How to slow down sprite animation while maintaining 60 fps in pygame?

How to slow down sprite animation while maintaining 60 fps in pygame?

I'm trying to create a platformer game, and I want to maintain 60 FPS in it without having the sprite animations move really quicklyI've seen other answers on how to do so using the time module, but I don't really understand how to apply that

147