Spaces:
Paused
Paused
Commit ·
fabf10e
1
Parent(s): fdbd781
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,11 +33,16 @@ def process_video(Video, target_language):
|
|
| 33 |
audio_file = f"{uuid.uuid4()}.wav"
|
| 34 |
run(["ffmpeg", "-i", Video, audio_file])
|
| 35 |
|
|
|
|
| 36 |
print("Iniciando transcrição com Whisper")
|
| 37 |
segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
|
| 38 |
segments = list(segments)
|
| 39 |
transcript_file = f"{uuid.uuid4()}.srt"
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
counter = 1
|
| 42 |
for segment in segments:
|
| 43 |
start_minutes = int(segment.start // 60)
|
|
@@ -52,33 +57,43 @@ def process_video(Video, target_language):
|
|
| 52 |
f.write(f"{formatted_start} --> {formatted_end}\n")
|
| 53 |
f.write(f"{segment.text}\n\n")
|
| 54 |
counter += 1
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
if line.strip().isnumeric() or "-->" in line:
|
| 60 |
-
|
| 61 |
elif line.strip() != "":
|
| 62 |
inputs = tokenizer(line.strip(), return_tensors="pt")
|
| 63 |
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[flores_code], max_length=100)
|
| 64 |
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 65 |
-
|
| 66 |
else:
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
output_video = "output_video.mp4"
|
| 70 |
# Debugging: Validate FFmpeg command for subtitle embedding
|
| 71 |
print("Validating FFmpeg command for subtitle embedding...")
|
| 72 |
-
print(f"Translated SRT file: {
|
| 73 |
-
with open(
|
| 74 |
print(f"First few lines of translated SRT: {f.readlines()[:10]}")
|
| 75 |
-
if os.path.exists(
|
| 76 |
-
print(f"{
|
| 77 |
else:
|
| 78 |
-
print(f"{
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
result = subprocess.run(["ffmpeg", "-i", Video, "-vf", f"subtitles={
|
| 82 |
if result.returncode == 0:
|
| 83 |
print("FFmpeg executed successfully.")
|
| 84 |
else:
|
|
@@ -104,4 +119,4 @@ iface = gr.Interface(
|
|
| 104 |
title="VIDEO TRANSCRIPTION AND TRANSLATION"
|
| 105 |
)
|
| 106 |
|
| 107 |
-
iface.launch()
|
|
|
|
| 33 |
audio_file = f"{uuid.uuid4()}.wav"
|
| 34 |
run(["ffmpeg", "-i", Video, audio_file])
|
| 35 |
|
| 36 |
+
# Transcription with Whisper.
|
| 37 |
print("Iniciando transcrição com Whisper")
|
| 38 |
segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
|
| 39 |
segments = list(segments)
|
| 40 |
transcript_file = f"{uuid.uuid4()}.srt"
|
| 41 |
+
|
| 42 |
+
# Create a list to hold the translated lines.
|
| 43 |
+
translated_lines = []
|
| 44 |
+
|
| 45 |
+
with open(transcript_file, "w+", encoding="utf-8") as f:
|
| 46 |
counter = 1
|
| 47 |
for segment in segments:
|
| 48 |
start_minutes = int(segment.start // 60)
|
|
|
|
| 57 |
f.write(f"{formatted_start} --> {formatted_end}\n")
|
| 58 |
f.write(f"{segment.text}\n\n")
|
| 59 |
counter += 1
|
| 60 |
+
|
| 61 |
+
# Move the file pointer to the beginning of the file.
|
| 62 |
+
f.seek(0)
|
| 63 |
+
|
| 64 |
+
# Translating the SRT from Whisper with NLLB.
|
| 65 |
+
flores_code = lang_codes.get(target_language, "eng_Latn")
|
| 66 |
+
for line in f:
|
| 67 |
if line.strip().isnumeric() or "-->" in line:
|
| 68 |
+
translated_lines.append(line)
|
| 69 |
elif line.strip() != "":
|
| 70 |
inputs = tokenizer(line.strip(), return_tensors="pt")
|
| 71 |
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[flores_code], max_length=100)
|
| 72 |
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 73 |
+
translated_lines.append(translated_text + "\n")
|
| 74 |
else:
|
| 75 |
+
translated_lines.append("\n")
|
| 76 |
+
|
| 77 |
+
# Move the file pointer to the beginning of the file and truncate it.
|
| 78 |
+
f.seek(0)
|
| 79 |
+
f.truncate()
|
| 80 |
+
|
| 81 |
+
# Write the translated lines back into the original file.
|
| 82 |
+
f.writelines(translated_lines)
|
| 83 |
|
| 84 |
output_video = "output_video.mp4"
|
| 85 |
# Debugging: Validate FFmpeg command for subtitle embedding
|
| 86 |
print("Validating FFmpeg command for subtitle embedding...")
|
| 87 |
+
print(f"Translated SRT file: {transcript_file}")
|
| 88 |
+
with open(transcript_file, 'r', encoding='utf-8') as f:
|
| 89 |
print(f"First few lines of translated SRT: {f.readlines()[:10]}")
|
| 90 |
+
if os.path.exists(transcript_file):
|
| 91 |
+
print(f"{transcript_file} exists.")
|
| 92 |
else:
|
| 93 |
+
print(f"{transcript_file} does not exist.")
|
| 94 |
try:
|
| 95 |
+
transcript_file_abs_path = os.path.abspath(transcript_file)
|
| 96 |
+
result = subprocess.run(["ffmpeg", "-i", Video, "-vf", f"subtitles={transcript_file_abs_path}", output_video], capture_output=True, text=True)
|
| 97 |
if result.returncode == 0:
|
| 98 |
print("FFmpeg executed successfully.")
|
| 99 |
else:
|
|
|
|
| 119 |
title="VIDEO TRANSCRIPTION AND TRANSLATION"
|
| 120 |
)
|
| 121 |
|
| 122 |
+
iface.launch()
|