SadHak
SadHak

Reputation: 376

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

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?

Upvotes: 2

Views: 2409

Answers (2)

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15516

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.

Upvotes: 4

Matthew Wisdom
Matthew Wisdom

Reputation: 151

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

Upvotes: 0

Related Questions