Artificial-superintelligence commited on
Commit
12e5a2b
·
verified ·
1 Parent(s): 19707c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -1,5 +1,5 @@
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
@@ -90,17 +90,37 @@ if video_file:
90
  translated_text = translate_in_chunks(original_text, translator)
91
  st.write(f"Translated Text ({target_language}):", translated_text)
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
@@ -113,12 +133,11 @@ if video_file:
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)
 
1
  import streamlit as st
2
+ from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_audioclips
3
  import whisper
4
  from translate import Translator
5
  from gtts import gTTS
 
90
  translated_text = translate_in_chunks(original_text, translator)
91
  st.write(f"Translated Text ({target_language}):", translated_text)
92
 
93
+ # Convert translated text to speech in chunks
94
+ tts_clips = []
95
+ words = translated_text.split()
96
+ chunk = ""
97
+ max_length = 200 # Adjust as needed
98
+
99
+ for word in words:
100
+ if len(chunk) + len(word) + 1 > max_length:
101
+ tts = gTTS(text=chunk, lang=LANGUAGES[target_language])
102
+ tts_audio_path = tempfile.mktemp(suffix=".mp3")
103
+ tts.save(tts_audio_path)
104
+ tts_clips.append(AudioFileClip(tts_audio_path))
105
+ chunk = word
106
+ else:
107
+ chunk += " " + word if chunk else word
108
+
109
+ if chunk: # Process last chunk
110
+ tts = gTTS(text=chunk, lang=LANGUAGES[target_language])
111
+ tts_audio_path = tempfile.mktemp(suffix=".mp3")
112
+ tts.save(tts_audio_path)
113
+ tts_clips.append(AudioFileClip(tts_audio_path))
114
+
115
+ # Concatenate all TTS audio chunks
116
+ final_audio = concatenate_audioclips(tts_clips)
117
  translated_audio_path = tempfile.mktemp(suffix=".mp3")
118
+ final_audio.write_audiofile(translated_audio_path)
119
 
120
  # Merge translated audio with the original video
121
  final_video_path = tempfile.mktemp(suffix=".mp4")
122
  original_video = VideoFileClip(temp_video_path)
123
+ final_video = original_video.set_audio(AudioFileClip(translated_audio_path))
 
 
124
  final_video.write_videofile(final_video_path, codec='libx264', audio_codec='aac')
125
 
126
  # Display success message and provide download link
 
133
 
134
  except Exception as e:
135
  st.error(f"Error during transcription/translation: {e}")
 
136
 
137
  # Clean up temporary files
138
+ for clip in tts_clips:
139
+ os.remove(clip.filename)
140
  os.remove(temp_video_path)
141
  os.remove(audio_path)
142
+ os.remove(translated_audio_path)
143
+ os.remove(final_video_path)