#!/usr/bin/env python3 from pathlib import Path import cv2 as cv import numpy as np output = Path("sample-video.mp4") width, height = 640, 360 fps = 24.0 frame_count = 72 writer = cv.VideoWriter( str(output), cv.VideoWriter_fourcc(*"mp4v"), fps, (width, height), ) if not writer.isOpened(): raise SystemExit("Cannot create sample-video.mp4") for index in range(frame_count): frame = np.zeros((height, width, 3), dtype=np.uint8) frame[:] = (34, 44, 64) x = 60 + (index * 7) % (width - 160) color = (80 + index % 120, 180, 230) cv.rectangle(frame, (x, 120), (x + 100, 220), color, -1) cv.putText( frame, f"Frame {index + 1:02d}", (40, 70), cv.FONT_HERSHEY_SIMPLEX, 1.3, (245, 245, 245), 2, cv.LINE_AA, ) cv.putText( frame, "OpenCV video display", (40, 315), cv.FONT_HERSHEY_SIMPLEX, 0.9, (210, 230, 255), 2, cv.LINE_AA, ) writer.write(frame) writer.release() print(f"Created {output}") print(f"Frames: {frame_count}") print(f"Frame size: {width}x{height}")