File size: 3,813 Bytes
20bee2f
4b8dbde
6616ce3
709adea
6616ce3
 
 
709adea
6616ce3
 
 
 
 
 
 
d45cee0
6616ce3
 
 
 
 
d45cee0
6616ce3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a52a536
6616ce3
 
48d0e40
 
6616ce3
 
48d0e40
6616ce3
48d0e40
d45cee0
972598c
 
 
 
 
 
48d0e40
 
6616ce3
 
1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import moviepy.editor as mp
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

def split_video(video_file, split_method, split_value):
    video = mp.VideoFileClip(video_file.name)
    duration = video.duration

    # Free Split: Пользователь может указать список временных меток через запятую для разбиения видео на сегменты.
    if split_method == "Free Split":
        split_times = [float(t) for t in split_value.split(",")]
        split_times = [t for t in split_times if 0 <= t <= duration]
        split_times.sort()
        split_times = [0] + split_times + [duration]
        clips = [video.subclip(start, end) for start, end in zip(split_times[:-1], split_times[1:])]
    
    # Average Split: Пользователь указывает количество частей, на которые нужно разделить видео, и каждый сегмент будет иметь одинаковую продолжительность.
    elif split_method == "Average Split":
        num_parts = int(split_value)
        part_duration = duration / num_parts
        clips = [video.subclip(i * part_duration, (i + 1) * part_duration) for i in range(num_parts)]
    
    # Time Split: Пользователь указывает список временных меток через запятую для разбиения видео. Каждый сегмент будет начинаться с указанного времени.
    elif split_method == "Time Split":
        split_times = [float(t) for t in split_value.split(",")]
        clips = [ffmpeg_extract_subclip(video_file.name, start, end) for start, end in zip([0] + split_times, split_times + [duration])]
    
    # Size Split: Пользователь указывает целевой размер сегмента в мегабайтах. Видео будет разделено на сегменты, каждый из которых будет иметь размер, близкий к указанному значению.
    elif split_method == "Size Split":
        target_size = int(split_value) * 1024 * 1024
        clips = []
        start = 0
        while start < duration:
            end = start + 1
            while end <= duration and video.subclip(start, end).size < target_size:
                end += 1
            clips.append(video.subclip(start, end))
            start = end

    output_files = []
    for i, clip in enumerate(clips):
        output_filename = f"split_{i + 1}.mp4"
        clip.write_videofile(output_filename)
        output_files.append(output_filename)

    return output_files

iface = gr.Interface(
    fn=split_video,
    inputs=[
        gr.File(label="Upload Video"),
        gr.Dropdown(choices=["Free Split", "Average Split", "Time Split", "Size Split"], label="Split Method"),
        gr.Textbox(label="Split Value (comma-separated for Free Split and Time Split, number for Average Split and Size Split)"),
    ],
    outputs=gr.Files(label="Download Split Videos"),
    title="Video Splitter",
    description="""
    Split your video file into multiple parts using different methods:
    1. Free Split: Specify a list of comma-separated time stamps to split the video into segments.
    2. Average Split: Enter the number of parts to divide the video into, with each segment having equal duration.
    3. Time Split: Provide a list of comma-separated time stamps to split the video, with each segment starting at the specified time.
    4. Size Split: Enter the target size of each segment in megabytes. The video will be divided into segments with sizes close to the specified value.
    """,
)

if __name__ == "__main__":
    iface.launch()