DC-Video / app.py
Mavendc's picture
Create app.py
8370ebb verified
Raw History Blame Contribute Delete
2.59 kB
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()