使用VideoWriter将4K图片生成4K MP4视频报错,求解决方案
Great question—let’s break down why you’re hitting that resolution limit and how to fix it to get your 4K video working.
Why You’re Seeing the Error
The default MPEG-4 encoder used by Matlab’s VideoWriter (especially in older versions or when relying on system-provided codecs like Windows’ DirectShow or macOS’ QuickTime components) has strict resolution caps—often maxing out at 1088x1920. Your 2160x3840 4K frames exceed this limit, hence the error.
Fixes to Generate 4K MP4 Video
1. Use a Supported H.264 Profile (Matlab Built-in)
Newer Matlab versions (R2019b+) let you specify a higher H.264 profile that supports 4K resolutions. Update your code to set the Profile property to 'High'—this enables 4K support for the MPEG-4 writer:
clear all; load images.mat % 图片尺寸为2160x3840x3x38 myVideo = VideoWriter('A9_B4_4k.mp4', 'MPEG-4'); myVideo.FrameRate = 30; myVideo.Quality = 100; % Enable High Profile for 4K support (if your Matlab version allows it) if isprop(myVideo, 'Profile') myVideo.Profile = 'High'; end open(myVideo); writeVideo(myVideo, images); close(myVideo);
If your Matlab version doesn’t have the Profile property (pre-R2019b), you’ll need to either upgrade Matlab or use the FFmpeg method below.
2. Use FFmpeg (More Reliable for 4K)
FFmpeg is a robust open-source tool that handles 4K and beyond seamlessly. Here’s how to use it from Matlab:
First, save your image sequence to temporary files, then call FFmpeg to stitch them into a 4K MP4:
clear all; load images.mat % 图片尺寸为2160x3840x3x38 % Step 1: Save each frame as a PNG (lossless, preserves 4K quality) tempDir = tempdir; for frameIdx = 1:size(images, 4) imwrite(images(:,:,:,frameIdx), fullfile(tempDir, sprintf('frame_%03d.png', frameIdx))); end % Step 2: Use FFmpeg to create 4K MP4 (libx264 codec, highest quality) ffmpegCmd = sprintf(... 'ffmpeg -framerate 30 -i %s/frame_%%03d.png -c:v libx264 -crf 0 -preset slow -pix_fmt yuv420p A9_B4_4k.mp4', ... tempDir); system(ffmpegCmd); % Step 3: Clean up temporary frames delete(fullfile(tempDir, 'frame_*.png'));
Quick notes:
- You’ll need FFmpeg installed on your system and added to your PATH so Matlab can call it.
-crf 0sets lossless quality (adjust to 18-23 for a balanced quality/file size if needed).-preset slowoptimizes compression efficiency (trades off with encoding time).
Key Takeaways
- Matlab’s default MPEG-4 codec has resolution limits; switch to the High Profile if your version supports it.
- FFmpeg is a foolproof alternative for 4K video generation, especially with older Matlab versions.
内容的提问来源于stack exchange,提问作者Ying Wang

