Spaces:
Runtime error
Runtime error
import gradio as gr | |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, concatenate_videoclips, vfx, afx | |
def process_video(video_path, tool, text=None, start_time=0, end_time=None, speed=1.0, volume=1.0, color=(255, 255, 255), fontsize=50): | |
clip = VideoFileClip(video_path) | |
if tool == "Trim": | |
clip = clip.subclip(start_time, end_time) | |
elif tool == "Speed up": | |
clip = clip.fx(vfx.speedx, speed) | |
elif tool == "Slow down": | |
clip = clip.fx(vfx.speedx, 1/speed) | |
elif tool == "Add text": | |
txt_clip = TextClip(text, fontsize=fontsize, color=color) | |
txt_clip = txt_clip.set_position('center').set_duration(clip.duration) | |
clip = CompositeVideoClip([clip, txt_clip]) | |
elif tool == "Volume up": | |
clip = clip.fx(afx.volumex, volume) | |
elif tool == "Volume down": | |
clip = clip.fx(afx.volumex, 1/volume) | |
elif tool == "Black and White": | |
clip = clip.fx(vfx.blackwhite) | |
elif tool == "Invert colors": | |
clip = clip.fx(vfx.invert_colors) | |
elif tool == "Mirror": | |
clip = clip.fx(vfx.mirror_x) | |
elif tool == "Flip vertically": | |
clip = clip.fx(vfx.mirror_y) | |
elif tool == "Rotate": | |
clip = clip.fx(vfx.rotate, 90) | |
elif tool == "Fade in": | |
clip = clip.fx(vfx.fadein, 2) | |
elif tool == "Fade out": | |
clip = clip.fx(vfx.fadeout, 2) | |
elif tool == "Crop": | |
clip = clip.crop(x1=100, y1=100, x2=400, y2=400) | |
elif tool == "Resize": | |
clip = clip.resize(height=360) | |
elif tool == "Loop": | |
clip = concatenate_videoclips([clip, clip]) | |
elif tool == "Reverse": | |
clip = clip.fx(vfx.time_mirror) | |
elif tool == "Add audio": | |
audio_clip = afx.audio_loop(clip.audio, duration=clip.duration) | |
clip = clip.set_audio(audio_clip) | |
elif tool == "Remove audio": | |
clip = clip.without_audio() | |
output_path = "output.mp4" | |
clip.write_videofile(output_path, codec="libx264") | |
return output_path | |
tools = [ | |
"Trim", "Speed up", "Slow down", "Add text", "Volume up", | |
"Volume down", "Black and White", "Invert colors", "Mirror", | |
"Flip vertically", "Rotate", "Fade in", "Fade out", "Crop", | |
"Resize", "Loop", "Reverse", "Add audio", "Remove audio" | |
] | |
interface = gr.Interface( | |
fn=process_video, | |
inputs=[ | |
gr.Video(label="Input Video"), | |
gr.Dropdown(label="Tool", choices=tools), | |
gr.Textbox(label="Text", visible=False), | |
gr.Number(label="Start Time (s)", visible=False), | |
gr.Number(label="End Time (s)", visible=False), | |
gr.Number(label="Speed", visible=False), | |
gr.Number(label="Volume", visible=False), | |
gr.ColorPicker(label="Text Color", visible=False), | |
gr.Number(label="Font Size", visible=False) | |
], | |
outputs=gr.Video(label="Output Video"), | |
live=False, | |
title="MoviePy Video Editor", | |
description="Edit your video using various tools from MoviePy." | |
) | |
def update_interface(tool): | |
return [ | |
gr.Textbox(visible=tool == "Add text"), | |
gr.Number(visible=tool in ["Trim", "Speed up", "Slow down"]), | |
gr.Number(visible=tool in ["Trim"]), | |
gr.Number(visible=tool in ["Speed up", "Slow down"]), | |
gr.Number(visible=tool in ["Volume up", "Volume down"]), | |
gr.ColorPicker(visible=tool == "Add text"), | |
gr.Number(visible=tool == "Add text") | |
] | |
interface.launch() |