Generates a simple valid MP4 video using OpenCV. Requires opencv-python.
(filename: str)
| 71 | |
| 72 | |
| 73 | def generate_video(filename: str): |
| 74 | """Generates a simple valid MP4 video using OpenCV. |
| 75 | |
| 76 | Requires opencv-python. |
| 77 | """ |
| 78 | try: |
| 79 | import cv2 |
| 80 | import numpy as np |
| 81 | except ImportError: |
| 82 | raise ImportError( |
| 83 | "opencv-python and numpy are required to generate video. Install" |
| 84 | " them with: pip install opencv-python numpy" |
| 85 | ) |
| 86 | |
| 87 | width, height = 320, 240 |
| 88 | fourcc = cv2.VideoWriter_fourcc(*"avc1") |
| 89 | out = cv2.VideoWriter(filename, fourcc, 20.0, (width, height)) |
| 90 | if not out.isOpened(): |
| 91 | raise RuntimeError( |
| 92 | "Failed to open VideoWriter. The 'avc1' codec might not be supported on" |
| 93 | " this system." |
| 94 | ) |
| 95 | |
| 96 | for i in range(60): # 3 seconds at 20fps |
| 97 | frame = np.zeros((height, width, 3), dtype=np.uint8) |
| 98 | # Draw a moving white square |
| 99 | x = (i * 5) % width |
| 100 | cv2.rectangle(frame, (x, 100), (x + 50, 150), (255, 255, 255), -1) |
| 101 | out.write(frame) |
| 102 | |
| 103 | out.release() |
| 104 | |
| 105 | |
| 106 | async def generate_report( |
no test coverage detected