deepinsight/insightface · error · ValueError
Video could not be opened.
Error message
Video could not be opened.
What it means
cv2.VideoCapture failed to open the target video (cap.isOpened() returned False), so the video path is not readable as a video stream by this OpenCV build.
Source
Thrown at python-package/insightface/gui/pages/face_swap_page.py:238
image = swapper.swap(target_image, target_native, source_native)
output_path = Path(self.context.config.export_dir) / f"face_swap_{timestamp_for_filename()}.png"
save_image(output_path, image)
return {
"kind": "image",
"image": image,
"path": str(output_path),
"message": f"Image face swap saved to {output_path}",
}
def _swap_video(self, swapper: FaceSwapEngine, source_native, target_path: str, progress=None, is_cancelled=None) -> dict:
try:
import cv2
except Exception as exc:
raise ValueError(f"OpenCV is required for video face swap: {exc}") from exc
cap = cv2.VideoCapture(target_path)
if not cap.isOpened():
raise ValueError("Video could not be opened.")
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
if width <= 0 or height <= 0:
cap.release()
raise ValueError("Video dimensions could not be read.")
output_path = Path(self.context.config.export_dir) / f"face_swap_video_{timestamp_for_filename()}.mp4"
output_path.parent.mkdir(parents=True, exist_ok=True)
writer = cv2.VideoWriter(str(output_path), cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
if not writer.isOpened():
cap.release()
raise ValueError("Video writer could not be opened.")
preview = None
swapped = 0
processed = 0
try:View on GitHub (pinned to 7fadd420c2)
Solutions
- Verify the file exists and plays in VLC/ffplay
- Check OpenCV video backends: print(cv2.getBuildInformation()) and confirm FFmpeg is listed; if not, install an FFmpeg-enabled opencv build
- Re-encode the video: ffmpeg -i in.mov -c:v libx264 -pix_fmt yuv420p out.mp4
- Use an absolute ASCII path on a local disk
Example fix
# before
cap = cv2.VideoCapture("clip.mov")
# after
import subprocess
subprocess.run(["ffmpeg","-y","-i","clip.mov","-c:v","libx264","-pix_fmt","yuv420p","clip.mp4"], check=True)
cap = cv2.VideoCapture("clip.mp4") Defensive patterns
Strategy: validation
Validate before calling
import os, cv2
ok = os.path.isfile(p) and p.lower().endswith(('.mp4','.avi','.mov','.mkv'))
probe = cv2.VideoCapture(p); ok = ok and probe.isOpened(); probe.release() Try / catch
try:
return self._swap_video(...)
except ValueError as e:
if 'could not be opened' in str(e): suggest_reencode_to_mp4() Prevention
- Check cv2.getBuildInformation() includes FFmpeg in deployment images
- Standardize inputs to H.264 MP4 via ffmpeg preprocessing
- Probe files with ffprobe before feeding them to the pipeline
When it happens
Trigger: Passing a nonexistent path, a non-video file, or a codec/container this OpenCV build cannot demux (often due to missing FFmpeg support) to the video swap task.
Common situations: opencd builds compiled without FFmpeg/GStreamer backends cannot open MP4; DRM-protected or truncated videos; non-ASCII paths on Windows; remote URLs when the build lacks network stream support.
Related errors
- Video writer could not be opened.
- OpenCV is required for video face swap: {exc}
- Video dimensions could not be read.
- NV21 data size is not enough: expected {expected_size} bytes
- skipped multi-face image ({face_count} faces)
AI-assisted analysis of deepinsight/insightface@7fadd420c2 (2026-08-28).
Data as JSON: /api/errors/05374bf9c9b56211.
Report an issue: GitHub.