Spaces:
Sleeping

111 / app.py
hhh454's picture
Update app.py
b4abac7 verified
Raw History Blame Contribute Delete
2.45 kB
import gradio as gr
from gtts import gTTS
import requests
import os
# ุณูƒุฑูŠุจุช ุญุณุจ ุงู„ุงุณุชุงูŠู„
def generate_script(idea, style):
if style == "Horror":
return f"A dark horror story about {idea}. Something is watching you. The silence is not safe. Fear is everywhere."
elif style == "Sci-Fi":
return f"A sci-fi story about {idea}. Reality is breaking. Technology controls everything. Nothing is real anymore."
else:
return f"A story about {idea}. A journey begins. Everything starts to change. Unexpected things happen."
# ุชู‚ุณูŠู…
def split_script(script):
return [s.strip() for s in script.split(".") if s.strip()]
def generate_video(idea, duration_minutes, style):
# ุชุญูˆูŠู„ ุงู„ุฏู‚ุงุฆู‚ ุฅู„ู‰ ุซูˆุงู†ูŠ
duration = int(duration_minutes * 60)
script = generate_script(idea, style)
sentences = split_script(script)
clips = []
# ุชูˆุฒูŠุน ุงู„ูˆู‚ุช ุนู„ู‰ ุงู„ู…ุดุงู‡ุฏ
scene_time = max(3, duration // len(sentences))
for i, sentence in enumerate(sentences):
# ุตูˆุฑุฉ
img_url = f"https://picsum.photos/1280/720?random={i}"
img_data = requests.get(img_url).content
img_file = f"img_{i}.jpg"
with open(img_file, "wb") as f:
f.write(img_data)
# ุตูˆุช
audio_file = f"audio_{i}.mp3"
tts = gTTS(sentence)
tts.save(audio_file)
# FFmpeg ููŠุฏูŠูˆ ุงู„ู…ุดู‡ุฏ
output_file = f"scene_{i}.mp4"
os.system(f"""
ffmpeg -y -loop 1 -i {img_file} -i {audio_file} \
-c:v libx264 -t {scene_time} -c:a aac \
-pix_fmt yuv420p -shortest {output_file}
""")
clips.append(output_file)
# ุฏู…ุฌ ุงู„ููŠุฏูŠูˆู‡ุงุช
with open("list.txt", "w") as f:
for c in clips:
f.write(f"file '{c}'\n")
os.system("ffmpeg -y -f concat -safe 0 -i list.txt -c copy output.mp4")
return script, "output.mp4"
demo = gr.Interface(
fn=generate_video,
inputs=[
gr.Textbox(label="๐Ÿ’ก ููƒุฑุฉ ุงู„ููŠุฏูŠูˆ"),
gr.Slider(0.1, 10, value=1, label="โฑ๏ธ ู…ุฏุฉ ุงู„ููŠุฏูŠูˆ (ุจุงู„ุฏู‚ุงุฆู‚)"),
gr.Dropdown(["Horror", "Sci-Fi", "Normal"], label="๐ŸŽจ ุงู„ุงุณุชุงูŠู„")
],
outputs=[
gr.Textbox(label="๐Ÿ“œ ุงู„ุณูƒุฑูŠุจุช"),
gr.Video(label="๐ŸŽฌ ุงู„ููŠุฏูŠูˆ ุงู„ู†ู‡ุงุฆูŠ")
],
title="๐Ÿ”ฅ AI Video Generator V3"
)
demo.launch()