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 if split_method == "Free Split": split_times = [float(t) for t in split_value.split(",")] split_times = [t for t in split_times if t >= 0 and 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:])] 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)] 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])] elif split_method == "Size Split": target_size = int(split_value) * 1024 * 1024 # Convert from MB to bytes 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.", ) if __name__ == "__main__": iface.launch()