| import gradio as gr |
| import numpy as np |
| from PIL import Image, ImageDraw |
| from moviepy.editor import ImageSequenceClip |
| import random |
|
|
| def generate_video(prompt, duration_seconds, start_image=None): |
| if not prompt: |
| prompt = "NaturalVid AI video" |
| |
| duration_seconds = max(5, min(60, int(duration_seconds))) |
| |
| width, height = 512, 288 |
| fps = 24 |
| total_frames = int(duration_seconds * fps) |
| |
| frames = [] |
| for i in range(total_frames): |
| img = Image.new("RGB", (width, height), color=(10, 20, 40)) |
| draw = ImageDraw.Draw(img) |
| |
| |
| text = prompt[:65] |
| x = 40 + int(12 * np.sin(i / 10.0)) |
| y = height // 2 - 40 |
| draw.text((x, y), text, fill=(255, 255, 255)) |
| |
| |
| for _ in range(12): |
| px = (i * 4 + random.randint(0, 60)) % (width - 10) |
| py = random.randint(30, height - 30) + int(10 * np.sin(i / 7.0)) |
| draw.ellipse((px, py, px + 6, py + 6), fill=(180, 220, 255)) |
| |
| frame = np.array(img) |
| frames.append(frame) |
| |
| |
| clip = ImageSequenceClip(frames, fps=fps) |
| output_path = "/tmp/naturalvid_output.mp4" |
| clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None) |
| |
| return output_path |
|
|
| with gr.Blocks(title="NaturalVid AI", theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# π NaturalVid AI\n**Your personal free text-to-video generator**") |
|
|
| with gr.Row(): |
| with gr.Column(scale=2): |
| prompt = gr.Textbox( |
| label="Describe your video", |
| placeholder="A serene mountain lake at sunrise, gentle mist rising, birds flying slowly, cinematic", |
| lines=5 |
| ) |
| duration = gr.Slider( |
| minimum=5, |
| maximum=60, |
| value=10, |
| step=5, |
| label="Video Length (seconds)" |
| ) |
| start_image = gr.Image(label="Optional Start Image", type="pil") |
| btn = gr.Button("π Generate Natural Video", variant="primary", size="large") |
| |
| with gr.Column(scale=1): |
| output_video = gr.Video(label="π₯ Your Generated Video", height=400) |
|
|
| gr.Markdown("π‘ Choose length from 5 to 60 seconds. On free hardware longer videos take more time.") |
|
|
| btn.click( |
| fn=generate_video, |
| inputs=[prompt, duration, start_image], |
| outputs=output_video |
| ) |
|
|
| demo.launch() |