Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import moviepy as mp
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def animate_video(video_path, animation_type, rotate_angle=0, move_x=0, move_y=0, fade_in_duration=0, fade_out_duration=0):
|
| 6 |
+
# Load the video
|
| 7 |
+
video = mp.editor.VideoFileClip(video_path)
|
| 8 |
+
|
| 9 |
+
# Apply the selected animation
|
| 10 |
+
if animation_type == "rotate":
|
| 11 |
+
video = video.rotate(rotate_angle)
|
| 12 |
+
elif animation_type == "move":
|
| 13 |
+
# Define movement
|
| 14 |
+
final_duration = video.duration
|
| 15 |
+
video = video.set_position(lambda t: (
|
| 16 |
+
move_x * (t / final_duration),
|
| 17 |
+
move_y * (t / final_duration)
|
| 18 |
+
))
|
| 19 |
+
elif animation_type == "fade_in":
|
| 20 |
+
video = video.fx(mp.editor.vfx.fadein, fade_in_duration)
|
| 21 |
+
elif animation_type == "fade_out":
|
| 22 |
+
video = video.fx(mp.editor.vfx.fadeout, fade_out_duration)
|
| 23 |
+
|
| 24 |
+
# Composite the final video
|
| 25 |
+
final_video = mp.editor.CompositeVideoClip([video])
|
| 26 |
+
|
| 27 |
+
# Save the final video with a unique name
|
| 28 |
+
output_path = f"output_video_{os.path.basename(video_path)}.mp4"
|
| 29 |
+
final_video.write_videofile(output_path, codec="libx264")
|
| 30 |
+
|
| 31 |
+
return output_path
|
| 32 |
+
|
| 33 |
+
# Gradio interface
|
| 34 |
+
animation_types = ["rotate", "move", "fade_in", "fade_out"]
|
| 35 |
+
|
| 36 |
+
def update_ui(animation_type):
|
| 37 |
+
return [
|
| 38 |
+
gr.Slider.update(visible=animation_type == "rotate"),
|
| 39 |
+
gr.Slider.update(visible=animation_type == "move"),
|
| 40 |
+
gr.Slider.update(visible=animation_type == "move"),
|
| 41 |
+
gr.Slider.update(visible=animation_type == "fade_in"),
|
| 42 |
+
gr.Slider.update(visible=animation_type == "fade_out"),
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# Logo URL
|
| 46 |
+
logo_url = "https://huggingface.co/spaces/Felguk/VideoAnimation/resolve/main/3077486-dzn2ssxc-v4.png"
|
| 47 |
+
|
| 48 |
+
# Create Gradio interface
|
| 49 |
+
logo_html = f'<img src="{logo_url}" alt="Logo" style="width:100px; height:auto; display:block; margin:0 auto;">'
|
| 50 |
+
logo_component = gr.HTML(value=logo_html)
|
| 51 |
+
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=animate_video,
|
| 54 |
+
inputs=[
|
| 55 |
+
logo_component, # Add logo component here
|
| 56 |
+
gr.Video(label="Input Video"),
|
| 57 |
+
gr.Dropdown(animation_types, label="Animation Type"),
|
| 58 |
+
gr.Slider(minimum=-360, maximum=360, value=0, label="Rotate Angle (degrees)", visible=False),
|
| 59 |
+
gr.Slider(minimum=-500, maximum=500, value=0, label="Move X (pixels)", visible=False),
|
| 60 |
+
gr.Slider(minimum=-500, maximum=500, value=0, label="Move Y (pixels)", visible=False),
|
| 61 |
+
gr.Slider(minimum=0, maximum=5, value=0, label="Fade In Duration (seconds)", visible=False),
|
| 62 |
+
gr.Slider(minimum=0, maximum=5, value=0, label="Fade Out Duration (seconds)", visible=False),
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.Video(label="Animated Video"),
|
| 65 |
+
title="Video Animation with Gradio",
|
| 66 |
+
description="Choose an animation type and customize the parameters to animate your video."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Show/hide sliders based on animation type
|
| 70 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
| 71 |
+
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="rotate")], outputs=update_ui("rotate"))
|
| 72 |
+
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="move")], outputs=update_ui("move"))
|
| 73 |
+
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="fade_in")], outputs=update_ui("fade_in"))
|
| 74 |
+
interface.load(fn=None, inputs=[None, gr.Dropdown.update(value="fade_out")], outputs=update_ui("fade_out"))
|
| 75 |
+
|
| 76 |
+
# Add event listener for dropdown change
|
| 77 |
+
interface.change(fn=update_ui, inputs=[interface.inputs[1]], outputs=interface.inputs[2:])
|