Spaces:
Runtime error
Runtime error
import gradio as gr | |
from moviepy.editor import AudioFileClip, ImageClip, CompositeVideoClip | |
from PIL import Image | |
# Ensure compatibility with updated PIL library | |
if not hasattr(Image, 'ANTIALIAS'): # Image.ANTIALIAS is deprecated; LANCZOS is the replacement | |
Image.ANTIALIAS = Image.LANCZOS | |
def create_video(image, audio): | |
# Load the audio file | |
audio_clip = AudioFileClip(audio) | |
# Load the main image and set its duration to match the audio | |
image_clip = ImageClip(image).set_duration(audio_clip.duration) | |
# Load the logo image, resize it, and position it in the top-right corner | |
logo = ImageClip("Logo.png").resize(height=100) # Adjust the height as needed | |
logo = logo.set_position(("right", "top")).set_duration(audio_clip.duration) | |
# Create a composite video with the main image and the logo overlay | |
video_clip = CompositeVideoClip([image_clip, logo]).set_audio(audio_clip) | |
# Save the video to a temporary file with Twitter-compatible settings | |
output_path = "/tmp/output_video_with_logo.mp4" | |
video_clip.write_videofile( | |
output_path, | |
fps=30, | |
codec="libx264", | |
audio_codec="aac", | |
preset="slow", | |
ffmpeg_params=["-b:v", "2000k"] # Adjust bitrate if needed | |
) | |
return output_path | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=create_video, | |
inputs=[ | |
gr.Image(type="filepath", label="Upload Image"), | |
gr.Audio(type="filepath", label="Upload Audio") | |
], | |
outputs=gr.Video(label="Output Video"), | |
title="Image + Audio to Video Converter with Logo Overlay", | |
description="Upload an image and an audio file to generate a video with the image and audio combined, including a logo overlay in the top-right corner." | |
) | |
iface.launch() | |