Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
from moviepy.editor import VideoFileClip
|
3 |
import whisper
|
4 |
from translate import Translator
|
5 |
from gtts import gTTS
|
@@ -92,18 +92,33 @@ if video_file:
|
|
92 |
|
93 |
# Convert translated text to speech
|
94 |
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language])
|
95 |
-
|
96 |
-
tts.save(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
# Display translated text and audio
|
99 |
-
st.success("Translation successful!")
|
100 |
-
st.audio(audio_output_path, format="audio/mp3")
|
101 |
except Exception as e:
|
102 |
st.error(f"Error during transcription/translation: {e}")
|
103 |
-
|
104 |
|
105 |
# Clean up temporary files
|
106 |
os.remove(temp_video_path)
|
107 |
os.remove(audio_path)
|
108 |
-
if
|
109 |
-
os.remove(
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
3 |
import whisper
|
4 |
from translate import Translator
|
5 |
from gtts import gTTS
|
|
|
92 |
|
93 |
# Convert translated text to speech
|
94 |
tts = gTTS(text=translated_text, lang=LANGUAGES[target_language])
|
95 |
+
translated_audio_path = tempfile.mktemp(suffix=".mp3")
|
96 |
+
tts.save(translated_audio_path)
|
97 |
+
|
98 |
+
# Merge translated audio with the original video
|
99 |
+
final_video_path = tempfile.mktemp(suffix=".mp4")
|
100 |
+
original_video = VideoFileClip(temp_video_path)
|
101 |
+
translated_audio = AudioFileClip(translated_audio_path)
|
102 |
+
|
103 |
+
final_video = original_video.set_audio(translated_audio)
|
104 |
+
final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
|
105 |
+
|
106 |
+
# Display success message and provide download link
|
107 |
+
st.success("Translation successful! Download your translated video below:")
|
108 |
+
st.video(final_video_path)
|
109 |
+
|
110 |
+
# Provide download link
|
111 |
+
with open(final_video_path, "rb") as f:
|
112 |
+
st.download_button("Download Translated Video", f, file_name="translated_video.mp4")
|
113 |
|
|
|
|
|
|
|
114 |
except Exception as e:
|
115 |
st.error(f"Error during transcription/translation: {e}")
|
116 |
+
translated_audio_path = None # Ensure this variable is defined
|
117 |
|
118 |
# Clean up temporary files
|
119 |
os.remove(temp_video_path)
|
120 |
os.remove(audio_path)
|
121 |
+
if translated_audio_path: # Only remove if it was created
|
122 |
+
os.remove(translated_audio_path)
|
123 |
+
if final_video_path: # Only remove if it was created
|
124 |
+
os.remove(final_video_path)
|