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()