Update app.py
Browse files
app.py
CHANGED
|
@@ -199,8 +199,29 @@ def update_translations(file, edited_table):
|
|
| 199 |
except Exception as e:
|
| 200 |
raise ValueError(f"Error updating translations: {e}")
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
def upload_and_manage(file, language):
|
| 204 |
if file is None:
|
| 205 |
return None, [], None, "No file uploaded. Please upload a video/audio file."
|
| 206 |
|
|
@@ -210,19 +231,25 @@ def upload_and_manage(file, language):
|
|
| 210 |
# Define paths for audio and output files
|
| 211 |
audio_path = "audio.wav"
|
| 212 |
output_video_path = "output_video.mp4"
|
|
|
|
| 213 |
|
| 214 |
list_available_fonts()
|
| 215 |
|
| 216 |
-
# Transcribe audio from uploaded media file and get timestamps
|
| 217 |
-
|
| 218 |
|
| 219 |
-
# Translate the transcription
|
| 220 |
-
translated_json = translate_text(
|
| 221 |
-
|
| 222 |
-
# Add transcript to video based on timestamps
|
| 223 |
add_transcript_to_video(file.name, translated_json, output_video_path)
|
| 224 |
|
| 225 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
editable_table = [
|
| 227 |
[float(entry["start"]), entry["original"], entry["translated"], float(entry["end"])]
|
| 228 |
for entry in translated_json
|
|
@@ -235,10 +262,8 @@ def upload_and_manage(file, language):
|
|
| 235 |
return translated_json, editable_table, output_video_path, elapsed_time_display
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
-
# Handle errors gracefully
|
| 239 |
return None, [], None, f"An error occurred: {str(e)}"
|
| 240 |
|
| 241 |
-
|
| 242 |
# Gradio Interface with Tabs
|
| 243 |
def build_interface():
|
| 244 |
css = """
|
|
|
|
| 199 |
except Exception as e:
|
| 200 |
raise ValueError(f"Error updating translations: {e}")
|
| 201 |
|
| 202 |
+
def generate_voiceover(translated_json, language, output_audio_path):
|
| 203 |
+
from gtts import gTTS
|
| 204 |
+
|
| 205 |
+
# Concatenate translated text into a single string
|
| 206 |
+
full_text = " ".join(entry["translated"] for entry in translated_json)
|
| 207 |
+
|
| 208 |
+
# Generate speech
|
| 209 |
+
tts = gTTS(text=full_text, lang=language)
|
| 210 |
+
tts.save(output_audio_path)
|
| 211 |
+
|
| 212 |
+
def replace_audio_in_video(video_path, new_audio_path, final_video_path):
|
| 213 |
+
import moviepy.editor as mp
|
| 214 |
+
|
| 215 |
+
video = mp.VideoFileClip(video_path)
|
| 216 |
+
new_audio = mp.AudioFileClip(new_audio_path)
|
| 217 |
+
|
| 218 |
+
# Set the new audio
|
| 219 |
+
video = video.set_audio(new_audio)
|
| 220 |
+
|
| 221 |
+
# Save the final output
|
| 222 |
+
video.write_videofile(final_video_path, codec="libx264", audio_codec="aac")
|
| 223 |
|
| 224 |
+
def upload_and_manage(file, language, mode="transcription"):
|
| 225 |
if file is None:
|
| 226 |
return None, [], None, "No file uploaded. Please upload a video/audio file."
|
| 227 |
|
|
|
|
| 231 |
# Define paths for audio and output files
|
| 232 |
audio_path = "audio.wav"
|
| 233 |
output_video_path = "output_video.mp4"
|
| 234 |
+
voiceover_path = "voiceover.wav"
|
| 235 |
|
| 236 |
list_available_fonts()
|
| 237 |
|
| 238 |
+
# Step 1: Transcribe audio from uploaded media file and get timestamps
|
| 239 |
+
transcription_json = transcribe_video(file.name)
|
| 240 |
|
| 241 |
+
# Step 2: Translate the transcription
|
| 242 |
+
translated_json = translate_text(transcription_json, language)
|
| 243 |
+
|
| 244 |
+
# Step 3: Add transcript to video based on timestamps
|
| 245 |
add_transcript_to_video(file.name, translated_json, output_video_path)
|
| 246 |
|
| 247 |
+
# Step 4 (Optional): Generate voiceover if mode is "transcription_voiceover"
|
| 248 |
+
if mode == "transcription_voiceover":
|
| 249 |
+
generate_voiceover(translated_json, language, voiceover_path)
|
| 250 |
+
replace_audio_in_video(output_video_path, voiceover_path, output_video_path)
|
| 251 |
+
|
| 252 |
+
# Convert translated JSON into a format for the editable table
|
| 253 |
editable_table = [
|
| 254 |
[float(entry["start"]), entry["original"], entry["translated"], float(entry["end"])]
|
| 255 |
for entry in translated_json
|
|
|
|
| 262 |
return translated_json, editable_table, output_video_path, elapsed_time_display
|
| 263 |
|
| 264 |
except Exception as e:
|
|
|
|
| 265 |
return None, [], None, f"An error occurred: {str(e)}"
|
| 266 |
|
|
|
|
| 267 |
# Gradio Interface with Tabs
|
| 268 |
def build_interface():
|
| 269 |
css = """
|