Spaces:
Sleeping
Sleeping
AndreySokolov01
commited on
Commit
•
1dccdbf
1
Parent(s):
289931f
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,39 @@ import os
|
|
3 |
import gradio as gr
|
4 |
|
5 |
def split_video(video_path, split_duration):
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
iface = gr.Interface(
|
9 |
fn=split_video,
|
|
|
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,
|