Advertisement
sourav8256

Untitled

Aug 18th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import cv2
  2.  
  3. def split_video(input_path, output_folder, clip_duration=30):
  4. video_capture = cv2.VideoCapture(input_path)
  5. fps = int(video_capture.get(cv2.CAP_PROP_FPS))
  6. total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
  7. clip_frame_count = clip_duration * fps
  8.  
  9. clip_number = 1
  10. frame_number = 0
  11.  
  12. while frame_number < total_frames:
  13. clip_output_path = f"{output_folder}/clip_{clip_number}.mp4"
  14. out = cv2.VideoWriter(clip_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (640, 480))
  15.  
  16. while frame_number < min(frame_number + clip_frame_count, total_frames):
  17. ret, frame = video_capture.read()
  18. out.write(frame)
  19. frame_number += 1
  20.  
  21. out.release()
  22. clip_number += 1
  23.  
  24. video_capture.release()
  25.  
  26. input_video_path = "input_video.mp4"
  27. output_folder_path = "output_clips"
  28. split_video(input_video_path, output_folder_path)
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement