Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from moviepy.editor import VideoFileClip
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
import whisper
|
| 6 |
model = whisper.load_model("base")
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Extract audio from video
|
| 10 |
video = VideoFileClip(video_path)
|
| 11 |
audio_path = "temp_audio.wav"
|
| 12 |
video.audio.write_audiofile(audio_path)
|
| 13 |
|
| 14 |
-
# Transcribe
|
| 15 |
-
result = model.transcribe(audio_path)
|
| 16 |
|
| 17 |
# Clean up temporary audio file
|
| 18 |
os.remove(audio_path)
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
srt_content = ""
|
| 22 |
-
for index, segment in enumerate(result['segments']):
|
| 23 |
start_time = segment['start']
|
| 24 |
end_time = segment['end']
|
| 25 |
-
|
| 26 |
-
|
| 27 |
# Convert seconds to SRT time format (HH:MM:SS,mmm)
|
| 28 |
def format_time(seconds):
|
| 29 |
hours = int(seconds // 3600)
|
|
@@ -34,29 +53,32 @@ def generate_subtitles(video_path):
|
|
| 34 |
|
| 35 |
srt_content += f"{index + 1}\n"
|
| 36 |
srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
|
| 37 |
-
srt_content += f"{
|
| 38 |
|
| 39 |
# Save the .srt file
|
| 40 |
-
srt_filename = "
|
| 41 |
with open(srt_filename, "w", encoding="utf-8") as srt_file:
|
| 42 |
srt_file.write(srt_content)
|
| 43 |
|
| 44 |
return srt_content, srt_filename
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
subtitles, srt_filename =
|
| 49 |
-
return subtitles, srt_filename #
|
| 50 |
|
| 51 |
iface = gr.Interface(
|
| 52 |
-
fn=
|
| 53 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 54 |
outputs=[
|
| 55 |
-
gr.Textbox(label="Subtitles", lines=10),
|
| 56 |
-
gr.
|
| 57 |
],
|
| 58 |
-
title="Video to Subtitles",
|
| 59 |
-
description="Upload
|
| 60 |
)
|
| 61 |
|
| 62 |
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from moviepy.editor import VideoFileClip
|
| 3 |
import os
|
| 4 |
+
from googletrans import Translator
|
| 5 |
|
| 6 |
import whisper
|
| 7 |
model = whisper.load_model("base")
|
| 8 |
|
| 9 |
+
TRANSLATION_LANGUAGES = {
|
| 10 |
+
"Urdu": "ur",
|
| 11 |
+
"French": "fr",
|
| 12 |
+
"Spanish": "es",
|
| 13 |
+
"German": "de",
|
| 14 |
+
"Chinese": "zh",
|
| 15 |
+
"Arabic": "ar",
|
| 16 |
+
"Hindi": "hi"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
translator = Translator()
|
| 20 |
+
|
| 21 |
+
def generate_translated_subtitles(video_path, target_language):
|
| 22 |
# Extract audio from video
|
| 23 |
video = VideoFileClip(video_path)
|
| 24 |
audio_path = "temp_audio.wav"
|
| 25 |
video.audio.write_audiofile(audio_path)
|
| 26 |
|
| 27 |
+
# Transcribe (but do NOT translate) using Whisper
|
| 28 |
+
result = model.transcribe(audio_path, language="en")
|
| 29 |
|
| 30 |
# Clean up temporary audio file
|
| 31 |
os.remove(audio_path)
|
| 32 |
|
| 33 |
+
# Extract all subtitle texts for batch translation
|
| 34 |
+
texts = [segment['text'] for segment in result['segments']]
|
| 35 |
+
|
| 36 |
+
# Perform batch translation (all at once)
|
| 37 |
+
translations = translator.translate(texts, dest=TRANSLATION_LANGUAGES[target_language])
|
| 38 |
+
|
| 39 |
+
# Format subtitles in .srt format
|
| 40 |
srt_content = ""
|
| 41 |
+
for index, (segment, translation) in enumerate(zip(result['segments'], translations)):
|
| 42 |
start_time = segment['start']
|
| 43 |
end_time = segment['end']
|
| 44 |
+
translated_text = translation.text
|
| 45 |
+
|
| 46 |
# Convert seconds to SRT time format (HH:MM:SS,mmm)
|
| 47 |
def format_time(seconds):
|
| 48 |
hours = int(seconds // 3600)
|
|
|
|
| 53 |
|
| 54 |
srt_content += f"{index + 1}\n"
|
| 55 |
srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
|
| 56 |
+
srt_content += f"{translated_text}\n\n"
|
| 57 |
|
| 58 |
# Save the .srt file
|
| 59 |
+
srt_filename = f"subtitles_{TRANSLATION_LANGUAGES.get(target_language, 'en')}.srt"
|
| 60 |
with open(srt_filename, "w", encoding="utf-8") as srt_file:
|
| 61 |
srt_file.write(srt_content)
|
| 62 |
|
| 63 |
return srt_content, srt_filename
|
| 64 |
|
| 65 |
|
| 66 |
+
def video_to_translated_subtitles(video, target_language):
|
| 67 |
+
subtitles, srt_filename = generate_translated_subtitles(video, target_language)
|
| 68 |
+
return subtitles, srt_filename # Return translated subtitles and file path
|
| 69 |
|
| 70 |
iface = gr.Interface(
|
| 71 |
+
fn=video_to_translated_subtitles,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Video(label="Upload English Video"),
|
| 74 |
+
gr.Dropdown(choices=list(TRANSLATION_LANGUAGES.keys()), label="Translate to", value="Urdu")
|
| 75 |
+
],
|
| 76 |
outputs=[
|
| 77 |
+
gr.Textbox(label="Translated Subtitles", lines=10),
|
| 78 |
+
gr.File(label="Download Translated .srt File") # Correct file download
|
| 79 |
],
|
| 80 |
+
title="Video to Translated Subtitles",
|
| 81 |
+
description="Upload an English video, select a language, and get translated subtitles."
|
| 82 |
)
|
| 83 |
|
| 84 |
iface.launch(share=True)
|