Yalantis/uCrop · error · CImgIOException
save_video(): File '%s', unable to initialize video writer w
Error message
save_video(): File '%s', unable to initialize video writer with codec '%c%c%c%c'.
What it means
After constructing cv::VideoWriter with the requested codec fourcc, fps and frame size, save_video() checks isOpened(). OpenCV failing to open the writer causes this IO exception listing the filename and codec characters.
Source
Thrown at ucrop/src/main/jni/CImg.h:68599
cimglist_instance);
const unsigned int W = _data?_data[0]._width:0, H = _data?_data[0]._height:0;
if (!W || !H)
throw CImgInstanceException(_cimglist_instance
"save_video(): Frame [0] is an empty image.",
cimglist_instance);
const char
*const _codec = codec && *codec?codec:"h264",
codec0 = cimg::uppercase(_codec[0]),
codec1 = _codec[0]?cimg::uppercase(_codec[1]):0,
codec2 = _codec[1]?cimg::uppercase(_codec[2]):0,
codec3 = _codec[2]?cimg::uppercase(_codec[3]):0;
cimg::mutex(9);
writers[index] = new cv::VideoWriter(filename,_cimg_fourcc(codec0,codec1,codec2,codec3),fps,cv::Size(W,H));
if (!writers[index]->isOpened()) {
delete writers[index];
writers[index] = 0;
cimg::mutex(9,0);
throw CImgIOException(_cimglist_instance
"save_video(): File '%s', unable to initialize video writer with codec '%c%c%c%c'.",
cimglist_instance,filename,
codec0,codec1,codec2,codec3);
}
CImg<charT>::string(filename).move_to(filenames[index]);
sizes(index,0) = W;
sizes(index,1) = H;
cimg::mutex(9,0);
}
if (!is_empty()) {
const unsigned int W = sizes(index,0), H = sizes(index,1);
cimg::mutex(9);
cimglist_for(*this,l) {
CImg<T> &src = _data[l];
if (src.is_empty())
cimg::warn(_cimglist_instance
"save_video(): Skip empty frame %d for file '%s'.",View on GitHub (pinned to f788b534b4)
Solutions
- Use a codec the OpenCV backend supports (e.g. 'mp4v' with .mp4, 'MJPG' with .avi) or install OpenH264/ffmpeg for h264
- Verify the output directory exists and is writable
- Check fps>0 and that WxH match the actual frame size
- Test writing a tiny video with cv::VideoWriter directly to isolate backend issues
Example fix
// before
frames.save_video("out.mp4", 25, "h264"); // fails without H264 encoder
// after
frames.save_video("out.mp4", 25, "mp4v"); // widely supported fourcc Defensive patterns
Strategy: fallback
Validate before calling
// C++ std::string ext = extension_of(fname); bool okPair = (codec == "mp4v" && ext == ".mp4") || (codec == "MJPG" && ext == ".avi"); if (!okPair) codec = defaultCodecFor(ext);
Try / catch
try { frames.save_video(fname, fps, "h264"); }
catch (CImgIOException& e) { frames.save_video(fname, fps, "mp4v"); } Prevention
- Use fourcc/extension combos known to work (MJPG+.avi, mp4v+.mp4)
- Ensure the OpenCV build has a video backend and H264 support if needed
- Pass fps>0 and correct WxH
- Test writer initialization once at app startup
When it happens
Trigger: cv::VideoWriter cannot open: unsupported codec fourcc for the extension (e.g. 'h264' without OpenH264/ffmpeg backend), invalid fps, unsupported resolution, unwritable/invalid output path, or missing OpenCV video backend.
Common situations: Using 'h264' codec with a stock OpenCV build lacking H264 encoder support; mismatched extension/codec (e.g. .avi with mp4v unavailable); fps of 0; target directory missing.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- load_video(): File '%s', unable to detect format of video fi
- load_video() : File '%s', arguments 'first_frame', 'last_fra
- load_video(): No already open video reader found. You must s
- load_video(): File '%s', no video reader slots available. Yo
- load_video(): File '%s', unable to locate frame %u.
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/fcab1ac2873a066f.
Report an issue: GitHub.