| 88 | print(f"Error during conversion: {e}") |
| 89 | |
| 90 | def extractframes(videopath: Path, startframe=0, endframe=300, downscale=1, save_subdir = '', ext='png'): |
| 91 | output_dir = videopath.parent / save_subdir / videopath.stem |
| 92 | |
| 93 | if all((output_dir / f"{i}.{ext}").exists() for i in range(startframe, endframe)): |
| 94 | print(f"Already extracted all the frames in {output_dir}") |
| 95 | return |
| 96 | |
| 97 | cam = cv2.VideoCapture(str(videopath)) |
| 98 | cam.set(cv2.CAP_PROP_POS_FRAMES, startframe) |
| 99 | |
| 100 | output_dir.mkdir(parents=True, exist_ok=True) |
| 101 | |
| 102 | for i in range(startframe, endframe): |
| 103 | success, frame = cam.read() |
| 104 | if not success: |
| 105 | print(f"Error reading frame {i}") |
| 106 | break |
| 107 | |
| 108 | if downscale > 1: |
| 109 | new_width, new_height = int(frame.shape[1] / downscale), int(frame.shape[0] / downscale) |
| 110 | frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA) |
| 111 | |
| 112 | cv2.imwrite(str(output_dir / f"{i}.{ext}"), frame) |
| 113 | |
| 114 | cam.release() |
| 115 | |
| 116 | |
| 117 | if __name__ == "__main__": |