Nick088 commited on
Commit
6de36d2
·
verified ·
1 Parent(s): f8a2dd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -45
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
- return srt_content
301
-
302
-
303
- def add_subtitles_to_video(video_file_path, srt_content):
304
- """Adds subtitles to a video using ffmpeg."""
305
- output_file_path = os.path.splitext(video_file_path)[0] + "_with_subs.mp4"
306
-
307
- try:
308
- with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_srt_file:
309
- temp_srt_path = temp_srt_file.name
310
- temp_srt_file.write(srt_content)
311
-
312
- # Use temporary file with -i for subtitles
313
- subprocess.run(
314
- [
315
- "ffmpeg",
316
- "-i",
317
- video_file_path,
318
- "-i",
319
- temp_srt_path,
320
- "-map",
321
- "0:v",
322
- "-map",
323
- "0:a",
324
- "-map",
325
- "1:s?",
326
- "-c:v",
327
- "copy",
328
- "-c:a",
329
- "copy",
330
- "-c:s",
331
- "mov_text",
332
- output_file_path,
333
- ],
334
- check=True,
335
- )
336
- return output_file_path
337
- except subprocess.CalledProcessError as e:
338
- return f"Error during subtitle addition: {e}"
 
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
- srt_output.change(
482
- add_subtitles_to_video,
483
- inputs=[audio_input_subtitles, srt_output],
484
- outputs=video_output,
 
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()