|
import streamlit as st |
|
import os |
|
import requests |
|
from gtts import gTTS |
|
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip |
|
|
|
def download_video(url, filename): |
|
""" |
|
Downloads a video from the specified URL and saves it as the given filename. |
|
""" |
|
response = requests.get(url, stream=True) |
|
if response.status_code == 200: |
|
with open(filename, 'wb') as file: |
|
for chunk in response.iter_content(chunk_size=1024): |
|
if chunk: |
|
file.write(chunk) |
|
print(f"Video downloaded successfully as {filename}") |
|
else: |
|
print("Failed to download video.") |
|
|
|
def generate_video(text, background_video, output_video): |
|
|
|
tts = gTTS(text, lang='en') |
|
audio_path = "output_audio.mp3" |
|
tts.save(audio_path) |
|
|
|
|
|
video = VideoFileClip(background_video) |
|
audio = AudioFileClip(audio_path) |
|
|
|
|
|
video = video.subclip(0, min(video.duration, audio.duration)) |
|
video = video.set_audio(audio) |
|
|
|
|
|
text_clip = TextClip(text, fontsize=50, color='white', font='Arial-Bold', bg_color='black') |
|
text_clip = text_clip.set_duration(video.duration).set_position(('center', 'bottom')) |
|
|
|
|
|
final_clip = CompositeVideoClip([video, text_clip]) |
|
final_clip.write_videofile(output_video, codec='libx264', fps=24) |
|
|
|
|
|
os.remove(audio_path) |
|
|
|
|
|
st.title("AI Video Creator with Subway Surfers Gameplay") |
|
text_input = st.text_area("Enter text for the video") |
|
background_video = "subway_surfers.mp4" |
|
output_video = "generated_video.mp4" |
|
|
|
|
|
video_url = 'https://archive.org/download/rpreplay-final-1680875953/rpreplay-final-1680875953.mp4' |
|
|
|
|
|
if not os.path.exists(background_video): |
|
st.write("Downloading background video...") |
|
download_video(video_url, background_video) |
|
|
|
if st.button("Generate Video"): |
|
if text_input: |
|
generate_video(text_input, background_video, output_video) |
|
st.video(output_video) |
|
with open(output_video, "rb") as file: |
|
st.download_button(label="Download Video", data=file, file_name="ai_generated_video.mp4", mime="video/mp4") |
|
else: |
|
st.error("Please enter some text.") |
|
|