Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from moviepy.editor import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip
|
| 3 |
import whisper
|
| 4 |
from translate import Translator
|
| 5 |
from gtts import gTTS
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
import numpy as np
|
| 9 |
-
from datetime import timedelta
|
| 10 |
import shutil
|
| 11 |
from pathlib import Path
|
|
|
|
| 12 |
|
| 13 |
# Set page configuration
|
| 14 |
st.set_page_config(
|
|
@@ -86,7 +86,7 @@ class VideoProcessor:
|
|
| 86 |
# Extract audio to temporary file
|
| 87 |
audio_path = self.create_temp_path(".wav")
|
| 88 |
video.audio.write_audiofile(audio_path, fps=16000, verbose=False, logger=None)
|
| 89 |
-
|
| 90 |
# Check if audio file is not empty
|
| 91 |
if os.path.getsize(audio_path) == 0:
|
| 92 |
raise ValueError("Extracted audio file is empty")
|
|
@@ -128,11 +128,12 @@ class VideoProcessor:
|
|
| 128 |
return translated_segments
|
| 129 |
|
| 130 |
def generate_tamil_audio(self, text):
|
| 131 |
-
"""Generate Tamil audio using gTTS"""
|
| 132 |
try:
|
| 133 |
audio_path = self.create_temp_path(".mp3")
|
| 134 |
tts = gTTS(text=text, lang='ta', slow=False)
|
| 135 |
tts.save(audio_path)
|
|
|
|
| 136 |
return audio_path
|
| 137 |
except Exception as e:
|
| 138 |
raise Exception(f"Audio generation error: {str(e)}")
|
|
@@ -266,27 +267,33 @@ def main():
|
|
| 266 |
with col2:
|
| 267 |
generate_subtitles = st.checkbox("Generate Subtitles", value=True)
|
| 268 |
|
| 269 |
-
# Ensure subtitle size and color are always defined
|
| 270 |
-
subtitle_size = 24 # Default subtitle size
|
| 271 |
-
subtitle_color = 'white' # Default subtitle color
|
| 272 |
-
|
| 273 |
if generate_subtitles:
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
if st.button("Process Video"):
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
if __name__ == "__main__":
|
| 292 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip
|
| 3 |
import whisper
|
| 4 |
from translate import Translator
|
| 5 |
from gtts import gTTS
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
import shutil
|
| 10 |
from pathlib import Path
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
# Set page configuration
|
| 14 |
st.set_page_config(
|
|
|
|
| 86 |
# Extract audio to temporary file
|
| 87 |
audio_path = self.create_temp_path(".wav")
|
| 88 |
video.audio.write_audiofile(audio_path, fps=16000, verbose=False, logger=None)
|
| 89 |
+
|
| 90 |
# Check if audio file is not empty
|
| 91 |
if os.path.getsize(audio_path) == 0:
|
| 92 |
raise ValueError("Extracted audio file is empty")
|
|
|
|
| 128 |
return translated_segments
|
| 129 |
|
| 130 |
def generate_tamil_audio(self, text):
|
| 131 |
+
"""Generate Tamil audio using gTTS with rate limiting"""
|
| 132 |
try:
|
| 133 |
audio_path = self.create_temp_path(".mp3")
|
| 134 |
tts = gTTS(text=text, lang='ta', slow=False)
|
| 135 |
tts.save(audio_path)
|
| 136 |
+
time.sleep(1) # Adding delay to avoid rate limit issues
|
| 137 |
return audio_path
|
| 138 |
except Exception as e:
|
| 139 |
raise Exception(f"Audio generation error: {str(e)}")
|
|
|
|
| 267 |
with col2:
|
| 268 |
generate_subtitles = st.checkbox("Generate Subtitles", value=True)
|
| 269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
if generate_subtitles:
|
| 271 |
+
col3, col4 = st.columns(2)
|
| 272 |
+
with col3:
|
| 273 |
+
subtitle_size = st.slider("Subtitle Size", 16, 32, 24)
|
| 274 |
+
with col4:
|
| 275 |
+
subtitle_color = st.color_picker("Subtitle Color", "#FFFFFF")
|
| 276 |
+
|
| 277 |
+
# Process video
|
| 278 |
if st.button("Process Video"):
|
| 279 |
+
with st.spinner("Processing video..."):
|
| 280 |
+
try:
|
| 281 |
+
output_video_path = process_video(
|
| 282 |
+
video_file.read(),
|
| 283 |
+
voice_type,
|
| 284 |
+
generate_subtitles,
|
| 285 |
+
subtitle_size,
|
| 286 |
+
subtitle_color
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
st.video(output_video_path)
|
| 290 |
+
st.success("Video processed successfully!")
|
| 291 |
+
|
| 292 |
+
with open(output_video_path, "rb") as f:
|
| 293 |
+
st.download_button("Download Processed Video", f, file_name="processed_video.mp4")
|
| 294 |
+
|
| 295 |
+
except Exception as e:
|
| 296 |
+
st.error(f"Error: {str(e)}")
|
| 297 |
+
|
| 298 |
if __name__ == "__main__":
|
| 299 |
main()
|