Jose Manuel Perez commited on
Commit
84c604a
·
unverified ·
1 Parent(s): 768ca67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -95
app.py CHANGED
@@ -308,10 +308,12 @@ def generate_subtitles(input_mode, input_file, link_input, prompt, language, aut
308
  segment_id_offset = 0
309
 
310
  if split_status == "split":
 
311
  srt_chunks = []
312
  video_chunks = []
313
  for i, chunk_path in enumerate(processed_path):
314
  try:
 
315
  with open(chunk_path, "rb") as file:
316
  transcription_json_response = client.audio.transcriptions.create(
317
  file=(os.path.basename(chunk_path), file.read()),
@@ -323,7 +325,7 @@ def generate_subtitles(input_mode, input_file, link_input, prompt, language, aut
323
  )
324
  transcription_json = transcription_json_response.segments
325
 
326
- # Adjust timestamps and segment IDs
327
  for segment in transcription_json:
328
  segment['start'] += total_duration
329
  segment['end'] += total_duration
@@ -336,73 +338,47 @@ def generate_subtitles(input_mode, input_file, link_input, prompt, language, aut
336
  temp_srt_path = f"{os.path.splitext(chunk_path)[0]}.srt"
337
  with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
338
  temp_srt_file.write(srt_content)
339
- temp_srt_file.write("\n") # add a new line at the end of the srt chunk file to fix format when merged
340
  srt_chunks.append(temp_srt_path)
341
 
 
342
  if include_video and input_file_path.lower().endswith((".mp4", ".webm")):
343
- try:
344
- output_file_path = chunk_path.replace(os.path.splitext(chunk_path)[1], "_with_subs" + os.path.splitext(chunk_path)[1])
345
- # Handle font selection
346
- if font_selection == "Custom Font File" and font_file:
347
- font_name = os.path.splitext(os.path.basename(font_file.name))[0] # Get font filename without extension
348
- font_dir = os.path.dirname(font_file.name) # Get font directory path
349
- elif font_selection == "Custom Font File" and not font_file:
350
- font_name = None # Let FFmpeg use its default Arial
351
- font_dir = None # No font directory
352
- gr.Warning(f"You want to use a Custom Font File, but uploaded none. Using the default Arial font.")
353
- elif font_selection == "Arial":
354
- font_name = None # Let FFmpeg use its default Arial
355
- font_dir = None # No font directory
356
-
357
- # FFmpeg command
358
- subprocess.run(
359
- [
360
- "ffmpeg",
361
- "-y",
362
- "-i",
363
- chunk_path,
364
- "-vf",
365
- f"subtitles={temp_srt_path}:fontsdir={font_dir}:force_style='Fontname={font_name},Fontsize={int(font_size)},PrimaryColour=&H{font_color[1:]}&,OutlineColour=&H{outline_color[1:]}&,BorderStyle={int(outline_thickness)},Outline=1'",
366
- "-preset", "fast",
367
- output_file_path,
368
- ],
369
- check=True,
370
- )
371
- video_chunks.append(output_file_path)
372
- except subprocess.CalledProcessError as e:
373
- raise gr.Error(f"Error during subtitle addition: {e}")
374
- elif include_video and not input_file_path.lower().endswith((".mp4", ".webm")):
375
- gr.Warning(f"You have checked on the 'Include Video with Subtitles', but the input file {input_file_path} isn't a video (.mp4 or .webm). Returning only the SRT File.", duration=15)
376
  except groq.AuthenticationError as e:
377
  handle_groq_error(e, model)
378
  except groq.RateLimitError as e:
379
  handle_groq_error(e, model)
380
- gr.Warning(f"API limit reached during chunk {i+1}. Returning processed chunks only.")
381
- if srt_chunks and video_chunks:
382
- merge_audio(video_chunks, 'merged_output_video.mp4')
383
- with open('merged_output.srt', 'w', encoding="utf-8") as outfile:
384
- for chunk_srt in srt_chunks:
385
- with open(chunk_srt, 'r', encoding="utf-8") as infile:
386
- outfile.write(infile.read())
387
- return 'merged_output.srt', 'merged_output_video.mp4'
388
- else:
389
- raise gr.Error("Subtitle generation failed due to API limits.")
390
-
391
- # Merge SRT chunks
392
- final_srt_path = os.path.splitext(input_file_path)[0] + "_final.srt"
393
- with open(final_srt_path, 'w', encoding="utf-8") as outfile:
394
- for chunk_srt in srt_chunks:
395
- with open(chunk_srt, 'r', encoding="utf-8") as infile:
396
- outfile.write(infile.read())
397
-
398
- # Merge video chunks
399
  if video_chunks:
400
  merge_audio(video_chunks, 'merged_output_video.mp4')
401
- return final_srt_path, 'merged_output_video.mp4'
402
  else:
403
- return final_srt_path, None
404
-
405
- else: # Single file processing (no splitting)
406
  try:
407
  with open(processed_path, "rb") as file:
408
  transcription_json_response = client.audio.transcriptions.create(
@@ -414,49 +390,37 @@ def generate_subtitles(input_mode, input_file, link_input, prompt, language, aut
414
  temperature=0.0,
415
  )
416
  transcription_json = transcription_json_response.segments
417
-
418
  srt_content = json_to_srt(transcription_json)
419
  temp_srt_path = os.path.splitext(input_file_path)[0] + ".srt"
420
  with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
421
  temp_srt_file.write(srt_content)
422
 
 
423
  if include_video and input_file_path.lower().endswith((".mp4", ".webm")):
424
- try:
425
- output_file_path = input_file_path.replace(
426
- os.path.splitext(input_file_path)[1], "_with_subs" + os.path.splitext(input_file_path)[1]
427
- )
428
- # Handle font selection
429
- if font_selection == "Custom Font File" and font_file:
430
- font_name = os.path.splitext(os.path.basename(font_file.name))[0] # Get font filename without extension
431
- font_dir = os.path.dirname(font_file.name) # Get font directory path
432
- elif font_selection == "Custom Font File" and not font_file:
433
- font_name = None # Let FFmpeg use its default Arial
434
- font_dir = None # No font directory
435
- gr.Warning(f"You want to use a Custom Font File, but uploaded none. Using the default Arial font.")
436
- elif font_selection == "Arial":
437
- font_name = None # Let FFmpeg use its default Arial
438
- font_dir = None # No font directory
439
-
440
- # FFmpeg command
441
- subprocess.run(
442
- [
443
- "ffmpeg",
444
- "-y",
445
- "-i",
446
- input_file_path,
447
- "-vf",
448
- f"subtitles={temp_srt_path}:fontsdir={font_dir}:force_style='FontName={font_name},Fontsize={int(font_size)},PrimaryColour=&H{font_color[1:]}&,OutlineColour=&H{outline_color[1:]}&,BorderStyle={int(outline_thickness)},Outline=1'",
449
- "-preset", "fast",
450
- output_file_path,
451
- ],
452
- check=True,
453
- )
454
- return temp_srt_path, output_file_path
455
- except subprocess.CalledProcessError as e:
456
- raise gr.Error(f"Error during subtitle addition: {e}")
457
- elif include_video and not input_file_path.lower().endswith((".mp4", ".webm")):
458
- gr.Warning(f"You have checked on the 'Include Video with Subtitles', but the input file {input_file_path} isn't a video (.mp4 or .webm). Returning only the SRT File.", duration=15)
459
-
460
  return temp_srt_path, None
461
  except groq.AuthenticationError as e:
462
  handle_groq_error(e, model)
@@ -466,6 +430,7 @@ def generate_subtitles(input_mode, input_file, link_input, prompt, language, aut
466
  raise gr.Error(f"Error creating SRT file: {e}")
467
 
468
 
 
469
  theme = gr.themes.Soft(
470
  primary_hue="indigo",
471
  secondary_hue="orange",
 
308
  segment_id_offset = 0
309
 
310
  if split_status == "split":
311
+ # Processing chunks when file is split
312
  srt_chunks = []
313
  video_chunks = []
314
  for i, chunk_path in enumerate(processed_path):
315
  try:
316
+ # Transcribe each chunk using the model
317
  with open(chunk_path, "rb") as file:
318
  transcription_json_response = client.audio.transcriptions.create(
319
  file=(os.path.basename(chunk_path), file.read()),
 
325
  )
326
  transcription_json = transcription_json_response.segments
327
 
328
+ # Adjust timestamps and IDs
329
  for segment in transcription_json:
330
  segment['start'] += total_duration
331
  segment['end'] += total_duration
 
338
  temp_srt_path = f"{os.path.splitext(chunk_path)[0]}.srt"
339
  with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
340
  temp_srt_file.write(srt_content)
341
+ temp_srt_file.write("\n") # Newline at end for proper merging
342
  srt_chunks.append(temp_srt_path)
343
 
344
+ # If the user requested video output with subtitles
345
  if include_video and input_file_path.lower().endswith((".mp4", ".webm")):
346
+ output_file_path = chunk_path.replace(os.path.splitext(chunk_path)[1], "_with_subs" + os.path.splitext(chunk_path)[1])
347
+
348
+ # Handle font selection
349
+ font_name, font_dir = None, None
350
+ if font_selection == "Custom Font File" and font_file:
351
+ font_name = os.path.splitext(os.path.basename(font_file.name))[0]
352
+ font_dir = os.path.dirname(font_file.name)
353
+
354
+ # FFmpeg command for adding subtitles with customization
355
+ ffmpeg_command = [
356
+ "ffmpeg",
357
+ "-y",
358
+ "-i",
359
+ chunk_path,
360
+ "-vf",
361
+ f"subtitles={temp_srt_path}:fontsdir={font_dir if font_selection == 'Custom Font File' else ''}:force_style='Fontname={font_name if font_selection == 'Custom Font File' else 'Arial'},Fontsize={font_size},PrimaryColour=&H{font_color[1:]}&,OutlineColour=&H{outline_color[1:]}&,Outline={outline_thickness}'",
362
+ "-preset", "fast",
363
+ output_file_path,
364
+ ]
365
+
366
+ subprocess.run(ffmpeg_command, check=True)
367
+ video_chunks.append(output_file_path)
368
+
 
 
 
 
 
 
 
 
 
 
369
  except groq.AuthenticationError as e:
370
  handle_groq_error(e, model)
371
  except groq.RateLimitError as e:
372
  handle_groq_error(e, model)
373
+
374
+ # Merge the chunks and return the results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
  if video_chunks:
376
  merge_audio(video_chunks, 'merged_output_video.mp4')
377
+ return 'merged_output_video.mp4', None
378
  else:
379
+ return full_srt_content, None
380
+ else:
381
+ # Process single file (no splitting)
382
  try:
383
  with open(processed_path, "rb") as file:
384
  transcription_json_response = client.audio.transcriptions.create(
 
390
  temperature=0.0,
391
  )
392
  transcription_json = transcription_json_response.segments
 
393
  srt_content = json_to_srt(transcription_json)
394
  temp_srt_path = os.path.splitext(input_file_path)[0] + ".srt"
395
  with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
396
  temp_srt_file.write(srt_content)
397
 
398
+ # If including the video with subtitles
399
  if include_video and input_file_path.lower().endswith((".mp4", ".webm")):
400
+ output_file_path = input_file_path.replace(
401
+ os.path.splitext(input_file_path)[1], "_with_subs" + os.path.splitext(input_file_path)[1]
402
+ )
403
+
404
+ font_name, font_dir = None, None
405
+ if font_selection == "Custom Font File" and font_file:
406
+ font_name = os.path.splitext(os.path.basename(font_file.name))[0]
407
+ font_dir = os.path.dirname(font_file.name)
408
+
409
+ # Add subtitles using FFmpeg
410
+ ffmpeg_command = [
411
+ "ffmpeg",
412
+ "-y",
413
+ "-i",
414
+ input_file_path,
415
+ "-vf",
416
+ f"subtitles={temp_srt_path}:fontsdir={font_dir if font_selection == 'Custom Font File' else ''}:force_style='Fontname={font_name if font_selection == 'Custom Font File' else 'Arial'},Fontsize={font_size},PrimaryColour=&H{font_color[1:]}&,OutlineColour=&H{outline_color[1:]}&,Outline={outline_thickness}'",
417
+ "-preset", "fast",
418
+ output_file_path,
419
+ ]
420
+
421
+ subprocess.run(ffmpeg_command, check=True)
422
+ return temp_srt_path, output_file_path
423
+
 
 
 
 
 
 
 
 
 
 
 
 
424
  return temp_srt_path, None
425
  except groq.AuthenticationError as e:
426
  handle_groq_error(e, model)
 
430
  raise gr.Error(f"Error creating SRT file: {e}")
431
 
432
 
433
+
434
  theme = gr.themes.Soft(
435
  primary_hue="indigo",
436
  secondary_hue="orange",