Spaces:
Runtime error
Runtime error
AndreySokolov01
commited on
Commit
•
abdd75f
1
Parent(s):
cef68e0
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,49 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
import gradio as gr
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import os
|
3 |
import gradio as gr
|
4 |
|
5 |
+
def split_video(video_path, split_duration):
|
6 |
+
video_capture = cv2.VideoCapture(video_path)
|
7 |
+
frame_rate = video_capture.get(cv2.CAP_PROP_FPS)
|
8 |
+
|
9 |
+
total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
10 |
+
total_duration = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) / frame_rate
|
11 |
+
|
12 |
+
num_parts = total_duration // split_duration
|
13 |
+
print(f"Total duration: {total_duration} seconds")
|
14 |
+
print(f"Splitting into {num_parts} parts of {split_duration} seconds each")
|
15 |
+
|
16 |
+
head, tail = os.path.split(video_path)
|
17 |
+
video_filename = tail.split('.')[0]
|
18 |
+
output_path = os.path.join(head, video_filename)
|
19 |
+
|
20 |
+
for i in range(num_parts):
|
21 |
+
start_frame = int(i * split_duration * frame_rate)
|
22 |
+
end_frame = int((i + 1) * split_duration * frame_rate)
|
23 |
+
output_filename = f"{output_path}_part{i+1}.mp4"
|
24 |
+
|
25 |
+
output_file = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), frame_rate, (int(video_capture.get(3)), int(video_capture.get(4))))
|
26 |
+
|
27 |
+
video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
28 |
+
|
29 |
+
while video_capture.get(1):
|
30 |
+
success, frame = video_capture.read()
|
31 |
+
if not success:
|
32 |
+
break
|
33 |
+
if start_frame <= int(video_capture.get(cv2.CAP_PROP_POS_FRAMES)) < end_frame:
|
34 |
+
output_file.write(frame)
|
35 |
+
|
36 |
+
output_file.release()
|
37 |
+
|
38 |
+
video_capture.release()
|
39 |
+
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=split_video,
|
42 |
+
title="Split Video by Time",
|
43 |
+
description="Split a video into multiple parts based on specified duration.",
|
44 |
+
inputs=[gr.Video(type="filepath", label="Video File"), gr.Number(default=5, label="Split Duration (seconds)")],
|
45 |
+
outputs=[gr.File(file_type="mp4", label="Video Parts")],
|
46 |
+
live=True
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|