{"record":{"id":"05374bf9c9b56211","repo":"deepinsight/insightface","slug":"video-could-not-be-opened","errorCode":null,"errorMessage":"Video could not be opened.","messagePattern":"Video could not be opened\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python-package/insightface/gui/pages/face_swap_page.py","lineNumber":238,"sourceCode":"        image = swapper.swap(target_image, target_native, source_native)\n        output_path = Path(self.context.config.export_dir) / f\"face_swap_{timestamp_for_filename()}.png\"\n        save_image(output_path, image)\n        return {\n            \"kind\": \"image\",\n            \"image\": image,\n            \"path\": str(output_path),\n            \"message\": f\"Image face swap saved to {output_path}\",\n        }\n\n    def _swap_video(self, swapper: FaceSwapEngine, source_native, target_path: str, progress=None, is_cancelled=None) -> dict:\n        try:\n            import cv2\n        except Exception as exc:\n            raise ValueError(f\"OpenCV is required for video face swap: {exc}\") from exc\n\n        cap = cv2.VideoCapture(target_path)\n        if not cap.isOpened():\n            raise ValueError(\"Video could not be opened.\")\n        fps = cap.get(cv2.CAP_PROP_FPS) or 25.0\n        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)\n        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)\n        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)\n        if width <= 0 or height <= 0:\n            cap.release()\n            raise ValueError(\"Video dimensions could not be read.\")\n        output_path = Path(self.context.config.export_dir) / f\"face_swap_video_{timestamp_for_filename()}.mp4\"\n        output_path.parent.mkdir(parents=True, exist_ok=True)\n        writer = cv2.VideoWriter(str(output_path), cv2.VideoWriter_fourcc(*\"mp4v\"), fps, (width, height))\n        if not writer.isOpened():\n            cap.release()\n            raise ValueError(\"Video writer could not be opened.\")\n\n        preview = None\n        swapped = 0\n        processed = 0\n        try:","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/deepinsight/insightface/blob/7fadd420c2351d0ffa8cac403421c1a3ed733365/python-package/insightface/gui/pages/face_swap_page.py#L220-L256","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\ncap = cv2.VideoCapture(\"clip.mov\")\n# after\nimport subprocess\nsubprocess.run([\"ffmpeg\",\"-y\",\"-i\",\"clip.mov\",\"-c:v\",\"libx264\",\"-pix_fmt\",\"yuv420p\",\"clip.mp4\"], check=True)\ncap = cv2.VideoCapture(\"clip.mp4\")","handlingStrategy":"validation","validationCode":"import os, cv2\nok = os.path.isfile(p) and p.lower().endswith(('.mp4','.avi','.mov','.mkv'))\nprobe = cv2.VideoCapture(p); ok = ok and probe.isOpened(); probe.release()","typeGuard":null,"tryCatchPattern":"try:\n    return self._swap_video(...)\nexcept ValueError as e:\n    if 'could not be opened' in str(e): suggest_reencode_to_mp4()","preventionTips":["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"],"tags":["insightface","opencv","video-capture","codec"],"backgroundTag":"opencv-videocapture-open-failed","analyzedSha":"7fadd420c2351d0ffa8cac403421c1a3ed733365","analyzedAt":"2026-08-28T15:44:01.850Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}