Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
from gtts import gTTS
|
5 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip
|
6 |
+
|
7 |
+
def download_video(url, filename):
|
8 |
+
"""
|
9 |
+
Downloads a video from the specified URL and saves it as the given filename.
|
10 |
+
"""
|
11 |
+
response = requests.get(url, stream=True)
|
12 |
+
if response.status_code == 200:
|
13 |
+
with open(filename, 'wb') as file:
|
14 |
+
for chunk in response.iter_content(chunk_size=1024):
|
15 |
+
if chunk:
|
16 |
+
file.write(chunk)
|
17 |
+
print(f"Video downloaded successfully as {filename}")
|
18 |
+
else:
|
19 |
+
print("Failed to download video.")
|
20 |
+
|
21 |
+
def generate_video(text, background_video, output_video):
|
22 |
+
# Generate speech from text
|
23 |
+
tts = gTTS(text, lang='en')
|
24 |
+
audio_path = "output_audio.mp3"
|
25 |
+
tts.save(audio_path)
|
26 |
+
|
27 |
+
# Load video and audio
|
28 |
+
video = VideoFileClip(background_video)
|
29 |
+
audio = AudioFileClip(audio_path)
|
30 |
+
|
31 |
+
# Set video duration to match audio
|
32 |
+
video = video.subclip(0, min(video.duration, audio.duration))
|
33 |
+
video = video.set_audio(audio)
|
34 |
+
|
35 |
+
# Create text overlay
|
36 |
+
text_clip = TextClip(text, fontsize=50, color='white', font='Arial-Bold', bg_color='black')
|
37 |
+
text_clip = text_clip.set_duration(video.duration).set_position(('center', 'bottom'))
|
38 |
+
|
39 |
+
# Merge text overlay with video
|
40 |
+
final_clip = CompositeVideoClip([video, text_clip])
|
41 |
+
final_clip.write_videofile(output_video, codec='libx264', fps=24)
|
42 |
+
|
43 |
+
# Clean up temporary audio file
|
44 |
+
os.remove(audio_path)
|
45 |
+
|
46 |
+
# Streamlit UI
|
47 |
+
st.title("AI Video Creator with Subway Surfers Gameplay")
|
48 |
+
text_input = st.text_area("Enter text for the video")
|
49 |
+
background_video = "subway_surfers.mp4" # Local filename for the background video
|
50 |
+
output_video = "generated_video.mp4"
|
51 |
+
|
52 |
+
# URL of the Subway Surfers gameplay video
|
53 |
+
video_url = 'https://archive.org/download/rpreplay-final-1680875953/rpreplay-final-1680875953.mp4'
|
54 |
+
|
55 |
+
# Download the background video if it doesn't exist
|
56 |
+
if not os.path.exists(background_video):
|
57 |
+
st.write("Downloading background video...")
|
58 |
+
download_video(video_url, background_video)
|
59 |
+
|
60 |
+
if st.button("Generate Video"):
|
61 |
+
if text_input:
|
62 |
+
generate_video(text_input, background_video, output_video)
|
63 |
+
st.video(output_video)
|
64 |
+
with open(output_video, "rb") as file:
|
65 |
+
st.download_button(label="Download Video", data=file, file_name="ai_generated_video.mp4", mime="video/mp4")
|
66 |
+
else:
|
67 |
+
st.error("Please enter some text.")
|