convert_video.py 859 B

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