Spaces:
Running
Running
import gradio as gr | |
import moviepy as mp | |
import os | |
def animate_video(video_path, animation_type, rotate_angle=0, move_x=0, move_y=0, fade_in_duration=0, fade_out_duration=0): | |
# Load the video | |
video = mp.editor.VideoFileClip(video_path) | |
# Apply the selected animation | |
if animation_type == "rotate": | |
video = video.rotate(rotate_angle) | |
elif animation_type == "move": | |
# Define movement | |
final_duration = video.duration | |
video = video.set_position(lambda t: ( | |
move_x * (t / final_duration), | |
move_y * (t / final_duration) | |
)) | |
elif animation_type == "fade_in": | |
video = video.fx(mp.editor.vfx.fadein, fade_in_duration) | |
elif animation_type == "fade_out": | |
video = video.fx(mp.editor.vfx.fadeout, fade_out_duration) | |
# Composite the final video | |
final_video = mp.editor.CompositeVideoClip([video]) | |
# Save the final video with a unique name | |
output_path = f"output_video_{os.path.basename(video_path)}.mp4" | |
final_video.write_videofile(output_path, codec="libx264") | |
return output_path | |
# Gradio interface | |
animation_types = ["rotate", "move", "fade_in", "fade_out"] | |
def update_ui(animation_type): | |
return [ | |
gr.Slider.update(visible=animation_type == "rotate"), | |
gr.Slider.update(visible=animation_type == "move"), | |
gr.Slider.update(visible=animation_type == "move"), | |
gr.Slider.update(visible=animation_type == "fade_in"), | |
gr.Slider.update(visible=animation_type == "fade_out"), | |
] | |
# Logo URL | |
logo_url = "https://huggingface.co/spaces/Felguk/VideoAnimation/resolve/main/3077486-dzn2ssxc-v4.png" | |
# Create Gradio interface | |
logo_html = f'<img src="{logo_url}" alt="Logo" style="width:100px; height:auto; display:block; margin:0 auto;">' | |
logo_component = gr.HTML(value=logo_html) | |
interface = gr.Interface( | |
fn=animate_video, | |
inputs=[ | |
logo_component, # Add logo component here | |
gr.Video(label="Input Video"), | |
gr.Dropdown(animation_types, label="Animation Type"), | |
gr.Slider(minimum=-360, maximum=360, value=0, label="Rotate Angle (degrees)", visible=False), | |
gr.Slider(minimum=-500, maximum=500, value=0, label="Move X (pixels)", visible=False), | |
gr.Slider(minimum=-500, maximum=500, value=0, label="Move Y (pixels)", visible=False), | |
gr.Slider(minimum=0, maximum=5, value=0, label="Fade In Duration (seconds)", visible=False), | |
gr.Slider(minimum=0, maximum=5, value=0, label="Fade Out Duration (seconds)", visible=False), | |
], | |
outputs=gr.Video(label="Animated Video"), | |
title="Video Animation with Gradio", | |
description="Choose an animation type and customize the parameters to animate your video." | |
) | |
# Show/hide sliders based on animation type | |
interface.launch(server_name="0.0.0.0", server_port=7860) | |
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="rotate")], outputs=update_ui("rotate")) | |
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="move")], outputs=update_ui("move")) | |
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="fade_in")], outputs=update_ui("fade_in")) | |
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="fade_out")], outputs=update_ui("fade_out")) | |
# Add event listener for dropdown change | |
interface.change(fn=update_ui, inputs=[interface.inputs[1]], outputs=interface.inputs[2:]) |