File size: 1,599 Bytes
20bee2f
709adea
1107c78
709adea
1107c78
 
 
 
 
 
0739edf
1107c78
0739edf
1107c78
 
 
 
 
 
 
 
0739edf
 
 
 
 
 
 
 
 
 
 
 
 
1107c78
 
709adea
 
1107c78
289931f
709adea
1107c78
289931f
1107c78
 
0739edf
abdd75f
 
0739edf
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
import gradio as gr
import ffmpeg
from math import ceil

def split_video(input_video, duration):
    video_info = ffmpeg.probe(input_video)
    video_duration = float(video_info['format']['duration'])
    num_segments = ceil(video_duration / duration)
    
    segments = []
    for i in range(num_segments - 1):
        start_time = i * duration
        end_time = (i + 1) * duration
        output_video = f"segment_{i+1}.mp4"
        
        stream = ffmpeg.input(input_video)
        stream = ffmpeg.trim(stream, start=start_time, end=end_time)
        stream = ffmpeg.output(stream, output_video)
        ffmpeg.run(stream, overwrite_output=True)
        
        segments.append(output_video)
        
    # Обработка последнего сегмента
    remaining_duration = video_duration - (num_segments - 1) * duration
    start_time = (num_segments - 1) * duration
    end_time = video_duration
    output_video = f"segment_{num_segments}.mp4"
    
    stream = ffmpeg.input(input_video)
    stream = ffmpeg.trim(stream, start=start_time, end=end_time)
    stream = ffmpeg.output(stream, output_video)
    ffmpeg.run(stream, overwrite_output=True)
    
    segments.append(output_video)
    
    return segments

interface = gr.Interface(
    split_video,
    inputs=[
        gr.File(label="Upload Video"),
        gr.Number(label="Segment Duration (seconds)"),
    ],
    outputs=gr.Files(label="Video Segments"),
    title="Video Splitter",
    description="Split a video into equal-duration segments. The last segment can be of unequal duration.",
)

interface.launch()