| 1 | import cv2 |
| 2 | |
| 3 | def convert_video(video_path): |
| 4 | cap = cv2.VideoCapture(video_path) |
| 5 | width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float |
| 6 | height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float |
| 7 | fps = cap.get(cv2.CAP_PROP_FPS) |
| 8 | video_name = video_path.split('/')[-1].split('.')[0] |
| 9 | save_name = video_name + '_converted' |
| 10 | save_path = video_path.replace(video_name, save_name) |
| 11 | vid_writer = cv2.VideoWriter( |
| 12 | save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)) |
| 13 | ) |
| 14 | while True: |
| 15 | ret_val, frame = cap.read() |
| 16 | if ret_val: |
| 17 | vid_writer.write(frame) |
| 18 | ch = cv2.waitKey(1) |
| 19 | if ch == 27 or ch == ord("q") or ch == ord("Q"): |
| 20 | break |
| 21 | else: |
| 22 | break |
| 23 | |
| 24 | if __name__ == "__main__": |
| 25 | video_path = 'videos/palace.mp4' |