Home > OS >  OpenCV VideoWriter Error "dimensions too large for MPEG-4"
OpenCV VideoWriter Error "dimensions too large for MPEG-4"

Time:02-11

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related