Spaces:
Sleeping
Sleeping
AndreySokolov01
commited on
Commit
•
6616ce3
1
Parent(s):
d45cee0
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
import ffmpeg
|
3 |
-
from math import ceil
|
4 |
import moviepy.editor as mp
|
|
|
5 |
|
6 |
-
def split_video(
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
stream = ffmpeg.input(input_video)
|
21 |
-
stream = ffmpeg.trim(stream, start=start_time, end=end_time)
|
22 |
-
stream = ffmpeg.output(stream, output_video)
|
23 |
-
ffmpeg.run(stream, overwrite_output=True)
|
24 |
-
|
25 |
-
segments.append(output_video)
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
split_video,
|
31 |
inputs=[
|
32 |
gr.File(label="Upload Video"),
|
33 |
-
gr.
|
|
|
34 |
],
|
35 |
-
outputs=gr.Files(label="
|
36 |
title="Video Splitter",
|
37 |
description="""
|
38 |
Split your video file into multiple parts using different methods:
|
@@ -43,4 +63,5 @@ interface = gr.Interface(
|
|
43 |
""",
|
44 |
)
|
45 |
|
46 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import moviepy.editor as mp
|
3 |
+
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
4 |
|
5 |
+
def split_video(video_file, split_method, split_value):
|
6 |
+
video = mp.VideoFileClip(video_file.name)
|
7 |
+
duration = video.duration
|
8 |
|
9 |
+
# Free Split: Пользователь может указать список временных меток через запятую для разбиения видео на сегменты.
|
10 |
+
if split_method == "Free Split":
|
11 |
+
split_times = [float(t) for t in split_value.split(",")]
|
12 |
+
split_times = [t for t in split_times if 0 <= t <= duration]
|
13 |
+
split_times.sort()
|
14 |
+
split_times = [0] + split_times + [duration]
|
15 |
+
clips = [video.subclip(start, end) for start, end in zip(split_times[:-1], split_times[1:])]
|
16 |
|
17 |
+
# Average Split: Пользователь указывает количество частей, на которые нужно разделить видео, и каждый сегмент будет иметь одинаковую продолжительность.
|
18 |
+
elif split_method == "Average Split":
|
19 |
+
num_parts = int(split_value)
|
20 |
+
part_duration = duration / num_parts
|
21 |
+
clips = [video.subclip(i * part_duration, (i + 1) * part_duration) for i in range(num_parts)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Time Split: Пользователь указывает список временных меток через запятую для разбиения видео. Каждый сегмент будет начинаться с указанного времени.
|
24 |
+
elif split_method == "Time Split":
|
25 |
+
split_times = [float(t) for t in split_value.split(",")]
|
26 |
+
clips = [ffmpeg_extract_subclip(video_file.name, start, end) for start, end in zip([0] + split_times, split_times + [duration])]
|
27 |
+
|
28 |
+
# Size Split: Пользователь указывает целевой размер сегмента в мегабайтах. Видео будет разделено на сегменты, каждый из которых будет иметь размер, близкий к указанному значению.
|
29 |
+
elif split_method == "Size Split":
|
30 |
+
target_size = int(split_value) * 1024 * 1024
|
31 |
+
clips = []
|
32 |
+
start = 0
|
33 |
+
while start < duration:
|
34 |
+
end = start + 1
|
35 |
+
while end <= duration and video.subclip(start, end).size < target_size:
|
36 |
+
end += 1
|
37 |
+
clips.append(video.subclip(start, end))
|
38 |
+
start = end
|
39 |
+
|
40 |
+
output_files = []
|
41 |
+
for i, clip in enumerate(clips):
|
42 |
+
output_filename = f"split_{i + 1}.mp4"
|
43 |
+
clip.write_videofile(output_filename)
|
44 |
+
output_files.append(output_filename)
|
45 |
+
|
46 |
+
return output_files
|
47 |
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=split_video,
|
50 |
inputs=[
|
51 |
gr.File(label="Upload Video"),
|
52 |
+
gr.Dropdown(choices=["Free Split", "Average Split", "Time Split", "Size Split"], label="Split Method"),
|
53 |
+
gr.Textbox(label="Split Value (comma-separated for Free Split and Time Split, number for Average Split and Size Split)"),
|
54 |
],
|
55 |
+
outputs=gr.Files(label="Download Split Videos"),
|
56 |
title="Video Splitter",
|
57 |
description="""
|
58 |
Split your video file into multiple parts using different methods:
|
|
|
63 |
""",
|
64 |
)
|
65 |
|
66 |
+
if __name__ == "__main__":
|
67 |
+
iface.launch()
|