Spaces:
Runtime error
Runtime error
Last commit not found
import cv2 | |
import os | |
import gradio as gr | |
def split_video(video_path, split_duration): | |
video_capture = cv2.VideoCapture(video_path) | |
frame_rate = video_capture.get(cv2.CAP_PROP_FPS) | |
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) | |
total_duration = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) / frame_rate | |
num_parts = total_duration // split_duration | |
print(f"Total duration: {total_duration} seconds") | |
print(f"Splitting into {num_parts} parts of {split_duration} seconds each") | |
head, tail = os.path.split(video_path) | |
video_filename = tail.split('.')[0] | |
output_path = os.path.join(head, video_filename) | |
for i in range(num_parts): | |
start_frame = int(i * split_duration * frame_rate) | |
end_frame = int((i + 1) * split_duration * frame_rate) | |
output_filename = f"{output_path}_part{i+1}.mp4" | |
output_file = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), frame_rate, (int(video_capture.get(3)), int(video_capture.get(4)))) | |
video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame) | |
while video_capture.get(1): | |
success, frame = video_capture.read() | |
if not success: | |
break | |
if start_frame <= int(video_capture.get(cv2.CAP_PROP_POS_FRAMES)) < end_frame: | |
output_file.write(frame) | |
output_file.release() | |
video_capture.release() | |
iface = gr.Interface( | |
fn=split_video, | |
title="Split Video by Time", | |
description="Split a video into multiple parts based on specified duration.", | |
inputs=[ | |
gr.inputs.File(type="video", label="Select Video File"), | |
gr.Number(default=5, label="Split Duration (seconds)") | |
], | |
outputs=[gr.File(file_type="mp4", label="Video Parts")], | |
live=True | |
) | |
iface.launch() |