Spaces:
Build error
Build error
| import gradio | |
| import moviepy | |
| import pillow | |
| from moviepy.editor as mp | |
| import ImageClip | |
| import TextClip | |
| import CompositeVideoClip | |
| import AudioFileClip | |
| from PIL import Image | |
| import numpy as np | |
| import os | |
| def generate_video(image, text_prompt, audio_file, duration, fps): | |
| """ | |
| Generate a static video from a single image with animated text prompt overlay and optional audio. | |
| Parameters: | |
| - image: PIL Image object (input image) | |
| - text_prompt: str (text to overlay with animation) | |
| - audio_file: str (path to uploaded audio file, optional) | |
| - duration: int (5-10 seconds) | |
| - fps: int (frames per second, e.g., 30) | |
| Returns: | |
| - str: Path to the generated MP4 video | |
| """ | |
| # Validate inputs | |
| if not 5 <= duration <= 10: | |
| raise ValueError("Duration must be between 5 and 10 seconds.") | |
| if not 10 <= fps <= 60: | |
| raise ValueError("FPS must be between 10 and 60.") | |
| # Convert PIL Image to NumPy array for MoviePy | |
| img_array = np.array(image) | |
| # Create base ImageClip | |
| clip = ImageClip(img_array).set_duration(duration).set_fps(fps) | |
| # Add animated text overlay if provided (uncensored, any content) | |
| if text_prompt: | |
| text_clip = TextClip(text_prompt, fontsize=50, color='white', font='Arial') | |
| text_clip = text_clip.set_duration(duration) | |
| # Add animations: fade in (first 1s), fade out (last 1s), and slide-in from left | |
| text_clip = text_clip.fadein(1).fadeout(1) # Fade effects | |
| text_clip = text_clip.set_position(lambda t: (50 + 200 * (t / duration), 'center')) # Horizontal slide from left | |
| clip = CompositeVideoClip([clip, text_clip]) | |
| # Add audio if provided (uncensored, loop/trim to fit duration) | |
| if audio_file: | |
| audio = AudioFileClip(audio_file).subclip(0, duration) # Trim to video duration | |
| if audio.duration < duration: | |
| audio = audio.loop(duration=duration) # Loop if shorter | |
| clip = clip.set_audio(audio) | |
| # Output file path (temporary MP4) | |
| output_path = "/content/output_video.mp4" | |
| # Write the video file (H.264 codec for compatibility) | |
| clip.write_videofile(output_path, codec='libx264') | |
| return output_path | |
| # Gradio Interface | |
| with gr.Blocks(title="Image to Video Generator with Animated Text & Audio") as demo: | |
| gr.Markdown(""" | |
| # Uncensored Image-to-Video Generator with Animated Text Effects & Audio Support | |
| Upload an image, optional text prompt for animated overlay (fade-in/out + slide), and optional audio file for background sound. No content moderation applied. | |
| """) | |
| with gr.Row(): | |
| image_input = gr.Image(label="Upload Image", type="pil") | |
| text_input = gr.Textbox(label="Text Prompt to Add (Optional, with Animation)", placeholder="Enter any text for animated overlay...") | |
| audio_input = gr.Audio(label="Upload Audio File (Optional, MP3/WAV/etc.)", type="filepath") | |
| duration_slider = gr.Slider(minimum=5, maximum=10, step=1, value=5, label="Video Duration (seconds)") | |
| fps_slider = gr.Slider(minimum=10, maximum=60, step=5, value=30, label="Frames Per Second (FPS)") | |
| generate_button = gr.Button("Generate Video") | |
| video_output = gr.Video(label="Generated Video") | |
| generate_button.click( | |
| fn=generate_video, | |
| inputs=[image_input, text_input, audio_input, duration_slider, fps_slider], | |
| outputs=video_output | |
| ) | |
| # Launch with public sharing | |
| demo.launch(share=True) | |