Nick088 commited on
Commit
1b66d6b
·
verified ·
1 Parent(s): e0eccf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py CHANGED
@@ -252,6 +252,75 @@ def translate_audio(audio_file_path, prompt):
252
  return translation.text
253
 
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
  with gr.Blocks() as demo:
256
  gr.Markdown(
257
  """
@@ -360,5 +429,42 @@ with gr.Blocks() as demo:
360
  inputs=[audio_input_translate, translate_prompt],
361
  outputs=translation_output,
362
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
 
364
  demo.launch()
 
252
  return translation.text
253
 
254
 
255
+ # subtitles maker
256
+
257
+ def create_srt_from_json(transcription_json):
258
+ """Converts Whisper JSON transcription to SRT format."""
259
+ srt_lines = []
260
+ for i, segment in enumerate(transcription_json["segments"]):
261
+ start_time = timedelta(seconds=segment["start"])
262
+ end_time = timedelta(seconds=segment["end"])
263
+ text = segment["text"]
264
+
265
+ # Format SRT timestamp
266
+ start_timestamp = f"{start_time.seconds}:{start_time.microseconds // 1000:03}"
267
+ end_timestamp = f"{end_time.seconds}:{end_time.microseconds // 1000:03}"
268
+
269
+ srt_lines.append(f"{i+1}\n{start_timestamp} --> {end_timestamp}\n{text}\n\n")
270
+
271
+ return "".join(srt_lines)
272
+
273
+
274
+ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
275
+ # ... (file check and error handling using check_file function) ...
276
+
277
+ with open(processed_path, "rb") as file:
278
+ transcription_json = client.audio.transcriptions.create(
279
+ file=(os.path.basename(processed_path), file.read()),
280
+ model="whisper-large-v3",
281
+ prompt=prompt,
282
+ response_format="json",
283
+ language=None if auto_detect_language else language, # Conditional language parameter
284
+ temperature=0.0,
285
+ )
286
+
287
+ srt_content = create_srt_from_json(transcription_json)
288
+ return srt_content
289
+
290
+
291
+ def add_subtitles_to_video(video_file_path, srt_content):
292
+ """Adds subtitles to a video using ffmpeg."""
293
+ output_file_path = os.path.splitext(video_file_path)[0] + "_with_subs.mp4"
294
+ try:
295
+ subprocess.run(
296
+ [
297
+ "ffmpeg",
298
+ "-i",
299
+ video_file_path,
300
+ "-i",
301
+ "-", # Input for subtitles from stdin
302
+ "-map",
303
+ "0:v",
304
+ "-map",
305
+ "1:a?",
306
+ "-map",
307
+ "1:s?", # Map subtitles
308
+ "-c:v",
309
+ "copy",
310
+ "-c:a",
311
+ "copy",
312
+ "-c:s",
313
+ "mov_text", # Subtitle codec
314
+ output_file_path,
315
+ ],
316
+ input=srt_content.encode("utf-8"), # Pass SRT content to ffmpeg
317
+ check=True,
318
+ )
319
+ return output_file_path
320
+ except subprocess.CalledProcessError as e:
321
+ return f"Error during subtitle addition: {e}"
322
+
323
+
324
  with gr.Blocks() as demo:
325
  gr.Markdown(
326
  """
 
429
  inputs=[audio_input_translate, translate_prompt],
430
  outputs=translation_output,
431
  )
432
+ with gr.TabItem("Subtitle Maker"):
433
+ with gr.Row():
434
+ audio_input_subtitles = gr.File(
435
+ label="Upload Audio/Video",
436
+ file_types=[f".{ext}" for ext in ALLOWED_FILE_EXTENSIONS],
437
+ )
438
+ transcribe_prompt_subtitles = gr.Textbox(
439
+ label="Prompt (Optional)",
440
+ info="Specify any context or spelling corrections.",
441
+ )
442
+ with gr.Row():
443
+ language_subtitles = gr.Dropdown(
444
+ choices=[(lang, code) for lang, code in LANGUAGE_CODES.items()],
445
+ value="en",
446
+ label="Language",
447
+ )
448
+ auto_detect_language_subtitles = gr.Checkbox(
449
+ label="Auto Detect Language"
450
+ )
451
+ transcribe_button_subtitles = gr.Button("Generate Subtitles")
452
+ srt_output = gr.Textbox(label="SRT Output")
453
+ video_output = gr.File(label="Output Video (with Subtitles)")
454
+ transcribe_button_subtitles.click(
455
+ generate_subtitles,
456
+ inputs=[
457
+ audio_input_subtitles,
458
+ transcribe_prompt_subtitles,
459
+ language_subtitles,
460
+ auto_detect_language_subtitles,
461
+ ],
462
+ outputs=srt_output,
463
+ )
464
+ srt_output.change(
465
+ add_subtitles_to_video,
466
+ inputs=[audio_input_subtitles, srt_output],
467
+ outputs=video_output,
468
+ )
469
 
470
  demo.launch()