{"record":{"id":"870cf95dcafd5cbf","repo":"roboflow/supervision","slug":"video-writer-is-not-open","errorCode":null,"errorMessage":"Video writer is not open","messagePattern":"Video writer is not open","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/supervision/_cv2/_video.py","lineNumber":232,"sourceCode":"            self._container = av.open(str(filename), mode=\"w\")\n            rate = Fraction(str(fps)).limit_denominator(100_000)\n            self._stream = self._container.add_stream(codec, rate=rate)\n            self._stream.width = self._width\n            self._stream.height = self._height\n            self._stream.pix_fmt = pixel_format\n            self._opened = True\n        except Exception as exc:\n            self._error = exc\n            self.release()\n\n    def isOpened(self) -> bool:\n        \"\"\"Return whether the writer initialized successfully.\"\"\"\n        return self._opened\n\n    def write(self, frame: npt.NDArray[np.uint8]) -> None:\n        \"\"\"Encode one BGR frame and mux all packets produced by the encoder.\"\"\"\n        if not self._opened or self._container is None or self._stream is None:\n            raise RuntimeError(\"Video writer is not open\") from self._error\n        if frame.shape != (self._height, self._width, 3):\n            raise ValueError(\n                \"Video frame must have shape \"\n                f\"({self._height}, {self._width}, 3), got {frame.shape}\"\n            )\n        if frame.dtype != np.uint8:\n            raise ValueError(\"Video frames must use uint8 dtype\")\n\n        video_frame = av.VideoFrame.from_ndarray(\n            np.ascontiguousarray(frame), format=\"bgr24\"\n        )\n        for packet in self._stream.encode(video_frame):\n            self._container.mux(packet)\n\n    def release(self) -> None:\n        \"\"\"Flush delayed encoder packets and close the output container.\"\"\"\n        container = self._container\n        stream = self._stream","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/_cv2/_video.py#L214-L250","documentation":"Raised by the PyAV VideoWriter.write() when the writer never opened successfully (or was released). Construction stores any init exception in self._error and defers it; the first write() then raises RuntimeError chained to that root cause — typically an unsupported codec, an unwritable path, or a PyAV/ffmpeg problem.","triggerScenarios":"Calling writer.write(frame) after creating a VideoWriter whose av.open or stream creation failed (bad codec, permission denied, missing directory), or writing after release().","commonSituations":"Real OpenCV silently absorbs open failures and isOpened() returns False, so code that skips the isOpened() check keeps 'working' (producing empty files); the fallback surfaces the failure at first write instead, surprising ported code.","solutions":["Check writer.isOpened() immediately after construction and fail fast with your own error if False.","Inspect the chained exception (__cause__) to find the real init failure — usually fix the codec fourcc or the output path.","Verify the output directory exists and is writable before creating the writer.","Do not call write() after release()."],"exampleFix":"# before\nwriter = cv2.VideoWriter('out.mp4', fourcc, fps, (w, h))\nwriter.write(frame)  # RuntimeError: Video writer is not open\n\n# after\nwriter = cv2.VideoWriter('out.mp4', fourcc, fps, (w, h))\nif not writer.isOpened():\n    raise RuntimeError(f'cannot open out.mp4 with {fourcc}')\nwriter.write(frame)","handlingStrategy":"validation","validationCode":"writer = cv2.VideoWriter(path, fourcc, fps, (w, h))\nif not writer.isOpened():\n    raise RuntimeError(f'failed to open video writer for {path}')","typeGuard":null,"tryCatchPattern":"try:\n    writer.write(frame)\nexcept RuntimeError as e:\n    if 'not open' in str(e) and e.__cause__ is not None:\n        raise RuntimeError(f'writer init failed: {e.__cause__}') from e\n    raise","preventionTips":["Always check isOpened() right after construction","Inspect __cause__ of this RuntimeError to find the real init error","Verify output directory exists and codec is supported before creating the writer","Never write after release()"],"tags":["opencv-fallback","pyav","video-writer","lifecycle","error-deferred"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}