Spaces:
Sleeping
Sleeping
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): | |
# Generate speech from text | |
tts = gTTS(text, lang='en') | |
audio_path = "output_audio.mp3" | |
tts.save(audio_path) | |
# Load video and audio | |
video = VideoFileClip(background_video) | |
audio = AudioFileClip(audio_path) | |
# Set video duration to match audio | |
video = video.subclip(0, min(video.duration, audio.duration)) | |
video = video.set_audio(audio) | |
# Create text overlay | |
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')) | |
# Merge text overlay with video | |
final_clip = CompositeVideoClip([video, text_clip]) | |
final_clip.write_videofile(output_video, codec='libx264', fps=24) | |
# Clean up temporary audio file | |
os.remove(audio_path) | |
# Streamlit UI | |
st.title("AI Video Creator with Subway Surfers Gameplay") | |
text_input = st.text_area("Enter text for the video") | |
background_video = "subway_surfers.mp4" # Local filename for the background video | |
output_video = "generated_video.mp4" | |
# URL of the Subway Surfers gameplay video | |
video_url = 'https://archive.org/download/rpreplay-final-1680875953/rpreplay-final-1680875953.mp4' | |
# Download the background video if it doesn't exist | |
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.") | |