File size: 1,850 Bytes
55eaf15
 
abdd75f
 
20bee2f
 
abdd75f
1dccdbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abdd75f
 
 
 
 
289931f
 
 
 
abdd75f
 
 
 
 
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
pip install opencv-python

import cv2
import os
import gradio as gr

def split_video(video_path, split_duration):
    video_capture = cv2.VideoCapture(video_path)
    frame_rate = video_capture.get(cv2.CAP_PROP_FPS)

    total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
    total_duration = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) / frame_rate

    num_parts = total_duration // split_duration
    print(f"Total duration: {total_duration} seconds")
    print(f"Splitting into {num_parts} parts of {split_duration} seconds each")

    head, tail = os.path.split(video_path)
    video_filename = tail.split('.')[0]
    output_path = os.path.join(head, video_filename)

    for i in range(num_parts):
        start_frame = int(i * split_duration * frame_rate)
        end_frame = int((i + 1) * split_duration * frame_rate)
        output_filename = f"{output_path}_part{i+1}.mp4"

        output_file = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), frame_rate, (int(video_capture.get(3)), int(video_capture.get(4))))

        video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)

        while video_capture.get(1):
            success, frame = video_capture.read()
            if not success:
                break
            if start_frame <= int(video_capture.get(cv2.CAP_PROP_POS_FRAMES)) < end_frame:
                output_file.write(frame)

        output_file.release()

    video_capture.release()

iface = gr.Interface(
    fn=split_video,
    title="Split Video by Time",
    description="Split a video into multiple parts based on specified duration.",
    inputs=[
        gr.inputs.File(type="video", label="Select Video File"),
        gr.Number(default=5, label="Split Duration (seconds)")
    ],
    outputs=[gr.File(file_type="mp4", label="Video Parts")],
    live=True
)

iface.launch()