Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,48 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
gr.inputs.Directory(),
|
10 |
-
gr.inputs.Number(),
|
11 |
-
gr.inputs.Number()],
|
12 |
-
output_types=[str],
|
13 |
-
title="Video Splitter")
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import os
|
3 |
import gradio as gr
|
4 |
|
5 |
+
def extract_clip(video_path, clip_start, clip_duration):
|
6 |
+
video = cv2.VideoCapture(video_path)
|
7 |
+
|
8 |
+
success, frame = video.read()
|
9 |
+
count = 0
|
10 |
+
frames = []
|
11 |
+
while success:
|
12 |
+
current_time = video.get(cv2.CAP_PROP_POS_MSEC) / 1000
|
13 |
+
|
14 |
+
# Seek to the desired starting point
|
15 |
+
if current_time < clip_start:
|
16 |
+
video.set(cv2.CAP_PROP_POS_MSEC, clip_start * 1000)
|
17 |
+
success, frame = video.read()
|
18 |
+
continue
|
19 |
+
|
20 |
+
# Check duration after reaching the clip_start timestamp
|
21 |
+
elif current_time - clip_start <= clip_duration:
|
22 |
+
frames.append(frame)
|
23 |
+
|
24 |
+
success, frame = video.read()
|
25 |
+
count += 1
|
26 |
|
27 |
+
else:
|
28 |
+
break
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
video.release()
|
31 |
+
return frames
|
32 |
+
|
33 |
+
def display_frames(frames):
|
34 |
+
num_cols = 4 # Number of columns in the image grid
|
35 |
+
num_rows = (len(frames) + num_cols - 1) // num_cols
|
36 |
+
grid_image = cv2.hconcat([cv2.hconcat(frames[i:i+num_cols]) for i in range(0, len(frames), num_cols)])
|
37 |
+
return grid_image
|
38 |
+
|
39 |
+
# Create Gradio interface
|
40 |
+
video_input = gr.inputs.Video(type="file")
|
41 |
+
start_input = gr.inputs.Number(default=5, label="Start Time (seconds)")
|
42 |
+
duration_input = gr.inputs.Number(default=3, label="Duration (seconds)")
|
43 |
+
image_output = gr.outputs.Image(type="pil")
|
44 |
+
|
45 |
+
gr.Interface(fn=lambda video, start, duration: display_frames(extract_clip(video, start, duration)),
|
46 |
+
inputs=[video_input, start_input, duration_input],
|
47 |
+
outputs=image_output,
|
48 |
+
title="Extract Video Clip").launch()
|