File size: 2,590 Bytes
8370ebb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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))) # limit between 5-60 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)
# Moving text
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))
# Natural moving particles (mist/lights)
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)
# Create real video
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() |