Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import whisper
|
| 3 |
+
from TTS.api import TTS
|
| 4 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 5 |
+
import os
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
| 7 |
+
|
| 8 |
+
# Page config
|
| 9 |
+
st.set_page_config(page_title="AI Voiceover Generator", layout="centered")
|
| 10 |
+
st.title("🎤 AI Voiceover + Subtitle Enhancer")
|
| 11 |
+
|
| 12 |
+
# Load models
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_whisper_model():
|
| 15 |
+
return whisper.load_model("small")
|
| 16 |
+
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_tts_model():
|
| 19 |
+
return TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 20 |
+
|
| 21 |
+
whisper_model = load_whisper_model()
|
| 22 |
+
tts = load_tts_model()
|
| 23 |
+
|
| 24 |
+
# Upload video
|
| 25 |
+
video_file = st.file_uploader("Upload a short video clip (MP4 preferred)", type=["mp4", "mov", "avi"])
|
| 26 |
+
|
| 27 |
+
if video_file:
|
| 28 |
+
with NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
| 29 |
+
tmp_video.write(video_file.read())
|
| 30 |
+
tmp_video_path = tmp_video.name
|
| 31 |
+
|
| 32 |
+
st.video(tmp_video_path)
|
| 33 |
+
|
| 34 |
+
# Extract audio
|
| 35 |
+
video = VideoFileClip(tmp_video_path)
|
| 36 |
+
audio_path = tmp_video_path.replace(".mp4", ".wav")
|
| 37 |
+
video.audio.write_audiofile(audio_path)
|
| 38 |
+
|
| 39 |
+
# Transcribe
|
| 40 |
+
st.info("Transcribing using Whisper...")
|
| 41 |
+
result = whisper_model.transcribe(audio_path)
|
| 42 |
+
st.subheader("📝 Detected Speech")
|
| 43 |
+
st.write(result["text"])
|
| 44 |
+
|
| 45 |
+
# User input for voiceover
|
| 46 |
+
custom_text = st.text_area("Enter your custom voiceover text:", "Here’s my voiceover explaining the video...")
|
| 47 |
+
|
| 48 |
+
if st.button("Generate AI Voiceover"):
|
| 49 |
+
voice_output_path = audio_path.replace(".wav", "_ai_voice.wav")
|
| 50 |
+
tts.tts_to_file(text=custom_text, file_path=voice_output_path)
|
| 51 |
+
st.audio(voice_output_path)
|
| 52 |
+
|
| 53 |
+
# Replace original audio with new one
|
| 54 |
+
final_video = video.set_audio(AudioFileClip(voice_output_path))
|
| 55 |
+
final_path = tmp_video_path.replace(".mp4", "_final.mp4")
|
| 56 |
+
final_video.write_videofile(final_path, codec="libx264", audio_codec="aac")
|
| 57 |
+
|
| 58 |
+
with open(final_path, "rb") as f:
|
| 59 |
+
st.download_button(label="📥 Download Final Video", data=f, file_name="final_ai_video.mp4")
|