Skip to content

VideoWriter not writing anything #1194

@MicahMaphet

Description

@MicahMaphet

Description

cv2.VideoWriter is creating a file but not writing anything in the video

Environment

Ubuntu 25.04
Python 3.13.3
Open CV 4.13.0

How to recreate

Create main.py with the file content.
Create a video file named video.mp4
mkdir .venv
python -m venv .venv
. .venv/bin/activate
pip install opencv-contrib-python
python main.py
And I get the following output with a corrupted output.mp4 file:

OpenCV Version:
4.13.0
codec
1983148141
shape
(1576, 770)
fps
24.0

However, if I run
ffmpeg -i output.mp4 out.mp4
out.mp4 works

File

import cv2
import time

video = cv2.VideoCapture("video.mp4")

fps = video.get(cv2.CAP_PROP_FPS)
shape = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter("output.mp4", fourcc, fps, shape)

print("OpenCV Version:")
print(cv2.__version__)
print("codec")
print(fourcc)
print("shape")
print(shape)
print("fps")
print(fps)

time.sleep(1.0)

while True:
    ret, frame = video.read()

    if not ret or frame is None:
        break

    if frame.ndim == 2:
        frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

    writer.write(frame)


video.release()
writer.release()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions