Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import random
|
|
4 |
import numpy as np
|
5 |
import json
|
6 |
from datetime import timedelta
|
|
|
7 |
import gradio as gr
|
8 |
from groq import Groq
|
9 |
|
@@ -280,7 +281,7 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
|
|
280 |
|
281 |
# If there's an error during file check
|
282 |
if error_message:
|
283 |
-
return error_message
|
284 |
|
285 |
with open(processed_path, "rb") as file:
|
286 |
transcription_json = client.audio.transcriptions.create(
|
@@ -297,45 +298,46 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
|
|
297 |
transcription_text = transcription_json['text']
|
298 |
|
299 |
srt_content = create_srt_from_text(transcription_text)
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
|
|
339 |
|
340 |
|
341 |
with gr.Blocks() as demo:
|
@@ -467,6 +469,7 @@ with gr.Blocks() as demo:
|
|
467 |
)
|
468 |
transcribe_button_subtitles = gr.Button("Generate Subtitles")
|
469 |
srt_output = gr.Textbox(label="SRT Output")
|
|
|
470 |
video_output = gr.File(label="Output Video (with Subtitles)")
|
471 |
transcribe_button_subtitles.click(
|
472 |
generate_subtitles,
|
@@ -476,12 +479,13 @@ with gr.Blocks() as demo:
|
|
476 |
language_subtitles,
|
477 |
auto_detect_language_subtitles,
|
478 |
],
|
479 |
-
outputs=srt_output,
|
480 |
)
|
481 |
-
|
482 |
-
|
483 |
-
inputs=
|
484 |
-
outputs=
|
|
|
485 |
)
|
486 |
|
487 |
demo.launch()
|
|
|
4 |
import numpy as np
|
5 |
import json
|
6 |
from datetime import timedelta
|
7 |
+
import tempfile
|
8 |
import gradio as gr
|
9 |
from groq import Groq
|
10 |
|
|
|
281 |
|
282 |
# If there's an error during file check
|
283 |
if error_message:
|
284 |
+
return error_message, None, None
|
285 |
|
286 |
with open(processed_path, "rb") as file:
|
287 |
transcription_json = client.audio.transcriptions.create(
|
|
|
298 |
transcription_text = transcription_json['text']
|
299 |
|
300 |
srt_content = create_srt_from_text(transcription_text)
|
301 |
+
|
302 |
+
# Generate subtitles and add to video if MP4
|
303 |
+
if audio_file_path.lower().endswith(".mp4"):
|
304 |
+
try:
|
305 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_srt_file:
|
306 |
+
temp_srt_path = temp_srt_file.name
|
307 |
+
temp_srt_file.write(srt_content)
|
308 |
+
|
309 |
+
# Use temporary file with -i for subtitles
|
310 |
+
subprocess.run(
|
311 |
+
[
|
312 |
+
"ffmpeg",
|
313 |
+
"-i",
|
314 |
+
audio_file_path,
|
315 |
+
"-i",
|
316 |
+
temp_srt_path,
|
317 |
+
"-map",
|
318 |
+
"0:v",
|
319 |
+
"-map",
|
320 |
+
"0:a",
|
321 |
+
"-map",
|
322 |
+
"1:s?",
|
323 |
+
"-c:v",
|
324 |
+
"copy",
|
325 |
+
"-c:a",
|
326 |
+
"copy",
|
327 |
+
"-c:s",
|
328 |
+
"mov_text",
|
329 |
+
audio_file_path.replace(".mp4", "_with_subs.mp4"), # Create new file name
|
330 |
+
],
|
331 |
+
check=True,
|
332 |
+
)
|
333 |
+
return srt_content, audio_file_path.replace(".mp4", "_with_subs.mp4"), None
|
334 |
+
except subprocess.CalledProcessError as e:
|
335 |
+
return srt_content, None, f"Error during subtitle addition: {e}"
|
336 |
+
finally:
|
337 |
+
if os.path.exists(temp_srt_path):
|
338 |
+
os.remove(temp_srt_path)
|
339 |
+
|
340 |
+
return srt_content, None, None
|
341 |
|
342 |
|
343 |
with gr.Blocks() as demo:
|
|
|
469 |
)
|
470 |
transcribe_button_subtitles = gr.Button("Generate Subtitles")
|
471 |
srt_output = gr.Textbox(label="SRT Output")
|
472 |
+
srt_download_button = gr.Button("Download SRT")
|
473 |
video_output = gr.File(label="Output Video (with Subtitles)")
|
474 |
transcribe_button_subtitles.click(
|
475 |
generate_subtitles,
|
|
|
479 |
language_subtitles,
|
480 |
auto_detect_language_subtitles,
|
481 |
],
|
482 |
+
outputs=[srt_output, video_output, gr.Error()],
|
483 |
)
|
484 |
+
srt_download_button.click(
|
485 |
+
lambda x: x,
|
486 |
+
inputs=srt_output,
|
487 |
+
outputs=srt_output,
|
488 |
+
download=True,
|
489 |
)
|
490 |
|
491 |
demo.launch()
|