Spaces:
Runtime error
Runtime error
pip install opencv-python | |
import cv2 | |
import os | |
import gradio as gr | |
def extract_clip(video_path, clip_start, clip_duration): | |
video = cv2.VideoCapture(video_path) | |
success, frame = video.read() | |
count = 0 | |
frames = [] | |
while success: | |
current_time = video.get(cv2.CAP_PROP_POS_MSEC) / 1000 | |
# Seek to the desired starting point | |
if current_time < clip_start: | |
video.set(cv2.CAP_PROP_POS_MSEC, clip_start * 1000) | |
success, frame = video.read() | |
continue | |
# Check duration after reaching the clip_start timestamp | |
elif current_time - clip_start <= clip_duration: | |
frames.append(frame) | |
success, frame = video.read() | |
count += 1 | |
else: | |
break | |
video.release() | |
return frames | |
def display_frames(frames): | |
num_cols = 4 # Number of columns in the image grid | |
num_rows = (len(frames) + num_cols - 1) // num_cols | |
grid_image = cv2.hconcat([cv2.hconcat(frames[i:i+num_cols]) for i in range(0, len(frames), num_cols)]) | |
return grid_image | |
# Create Gradio interface | |
video_input = gr.inputs.Video(type="file") | |
start_input = gr.inputs.Number(default=5, label="Start Time (seconds)") | |
duration_input = gr.inputs.Number(default=3, label="Duration (seconds)") | |
image_output = gr.outputs.Image(type="pil") | |
gr.Interface(fn=lambda video, start, duration: display_frames(extract_clip(video, start, duration)), | |
inputs=[video_input, start_input, duration_input], | |
outputs=image_output, | |
title="Extract Video Clip").launch() |