Spaces:
Build error
Build error
Jose Manuel Perez commited on
Update app.py
Browse files
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
|
| 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")
|
| 340 |
srt_chunks.append(temp_srt_path)
|
| 341 |
|
|
|
|
| 342 |
if include_video and input_file_path.lower().endswith((".mp4", ".webm")):
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 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 |
-
|
| 381 |
-
|
| 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
|
| 402 |
else:
|
| 403 |
-
return
|
| 404 |
-
|
| 405 |
-
|
| 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 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 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",
|