Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import subprocess
|
|
| 7 |
import base64
|
| 8 |
import json
|
| 9 |
import shutil
|
|
|
|
| 10 |
from typing import Optional
|
| 11 |
|
| 12 |
# 🎯 DÜZELTME 1: Google API için v1beta sürümünü zorunlu kılıyoruz.
|
|
@@ -365,14 +366,15 @@ def run_production_pipeline(job: TranslationJob, gemini_key: str, elevenlabs_key
|
|
| 365 |
if file_ext not in ['mp4', 'mov', 'webm']:
|
| 366 |
file_ext = 'mp4'
|
| 367 |
|
| 368 |
-
#
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
|
|
|
| 376 |
|
| 377 |
try:
|
| 378 |
# 1. Download original video
|
|
@@ -559,9 +561,9 @@ def run_production_pipeline(job: TranslationJob, gemini_key: str, elevenlabs_key
|
|
| 559 |
cmd_lipsync = [
|
| 560 |
"python", "inference.py",
|
| 561 |
"--checkpoint_path", "checkpoints/wav2lip_gan.pth",
|
| 562 |
-
"--face",
|
| 563 |
-
"--audio",
|
| 564 |
-
"--outfile",
|
| 565 |
]
|
| 566 |
|
| 567 |
# Execute in the Wav2Lip directory to resolve relative imports
|
|
@@ -578,6 +580,11 @@ def run_production_pipeline(job: TranslationJob, gemini_key: str, elevenlabs_key
|
|
| 578 |
|
| 579 |
if job.has_captions:
|
| 580 |
print(f"[{video_id}] Burning subtitles with style '{job.caption_style}' using FFmpeg...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
style_str = get_ffmpeg_style(
|
| 582 |
job.caption_style,
|
| 583 |
custom_font=job.custom_font,
|
|
@@ -585,12 +592,23 @@ def run_production_pipeline(job: TranslationJob, gemini_key: str, elevenlabs_key
|
|
| 585 |
custom_color=job.custom_color
|
| 586 |
)
|
| 587 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
if job.has_lip_sync:
|
| 589 |
# Lip-synced video already has the synthesized audio merged inside it.
|
| 590 |
# So we map audio track from the same file (0:a)
|
| 591 |
cmd_merge = [
|
| 592 |
"ffmpeg", "-y", "-i", video_src_for_subtitles,
|
| 593 |
-
"-vf",
|
| 594 |
"-map", "0:v", "-map", "0:a",
|
| 595 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest",
|
| 596 |
output_video_path
|
|
@@ -599,7 +617,7 @@ def run_production_pipeline(job: TranslationJob, gemini_key: str, elevenlabs_key
|
|
| 599 |
# Map audio from input 1 (synthesized audio path)
|
| 600 |
cmd_merge = [
|
| 601 |
"ffmpeg", "-y", "-i", video_src_for_subtitles, "-i", synthesized_audio_path,
|
| 602 |
-
"-vf",
|
| 603 |
"-map", "0:v", "-map", "1:a",
|
| 604 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest",
|
| 605 |
output_video_path
|
|
|
|
| 7 |
import base64
|
| 8 |
import json
|
| 9 |
import shutil
|
| 10 |
+
import tempfile
|
| 11 |
from typing import Optional
|
| 12 |
|
| 13 |
# 🎯 DÜZELTME 1: Google API için v1beta sürümünü zorunlu kılıyoruz.
|
|
|
|
| 366 |
if file_ext not in ['mp4', 'mov', 'webm']:
|
| 367 |
file_ext = 'mp4'
|
| 368 |
|
| 369 |
+
# Use /tmp for temp files (reliable on Linux/HuggingFace Space, avoids CWD issues with FFmpeg)
|
| 370 |
+
tmp_dir = tempfile.gettempdir()
|
| 371 |
+
downloaded_video_path = os.path.join(tmp_dir, f"temp_{video_id}_downloaded.{file_ext}")
|
| 372 |
+
input_video_path = os.path.join(tmp_dir, f"temp_{video_id}_input.mp4") # standardized version
|
| 373 |
+
extracted_audio_path = os.path.join(tmp_dir, f"temp_{video_id}_audio.mp3")
|
| 374 |
+
synthesized_audio_path = os.path.join(tmp_dir, f"temp_{video_id}_tts.mp3")
|
| 375 |
+
srt_file_path = os.path.join(tmp_dir, f"temp_{video_id}.srt")
|
| 376 |
+
output_video_path = os.path.join(tmp_dir, f"temp_{video_id}_output.mp4")
|
| 377 |
+
lipsync_output_path = os.path.join(tmp_dir, f"temp_{video_id}_lipsync.mp4")
|
| 378 |
|
| 379 |
try:
|
| 380 |
# 1. Download original video
|
|
|
|
| 561 |
cmd_lipsync = [
|
| 562 |
"python", "inference.py",
|
| 563 |
"--checkpoint_path", "checkpoints/wav2lip_gan.pth",
|
| 564 |
+
"--face", input_video_path,
|
| 565 |
+
"--audio", synthesized_audio_path,
|
| 566 |
+
"--outfile", lipsync_output_path
|
| 567 |
]
|
| 568 |
|
| 569 |
# Execute in the Wav2Lip directory to resolve relative imports
|
|
|
|
| 580 |
|
| 581 |
if job.has_captions:
|
| 582 |
print(f"[{video_id}] Burning subtitles with style '{job.caption_style}' using FFmpeg...")
|
| 583 |
+
|
| 584 |
+
# Guard: verify SRT file was actually written before trying to burn it
|
| 585 |
+
if not os.path.exists(srt_file_path) or os.path.getsize(srt_file_path) == 0:
|
| 586 |
+
raise Exception(f"SRT subtitle file is missing or empty: {srt_file_path}")
|
| 587 |
+
|
| 588 |
style_str = get_ffmpeg_style(
|
| 589 |
job.caption_style,
|
| 590 |
custom_font=job.custom_font,
|
|
|
|
| 592 |
custom_color=job.custom_color
|
| 593 |
)
|
| 594 |
|
| 595 |
+
# FFmpeg subtitles filter: path must use forward slashes.
|
| 596 |
+
# On Windows, the drive-letter colon (C:) must be escaped as C\:
|
| 597 |
+
# On Linux (HuggingFace Space), paths start with / so no escaping needed.
|
| 598 |
+
srt_filter_path = srt_file_path.replace("\\", "/")
|
| 599 |
+
if len(srt_filter_path) >= 2 and srt_filter_path[1] == ":":
|
| 600 |
+
# Windows drive letter colon escape
|
| 601 |
+
srt_filter_path = srt_filter_path[0] + "\\:" + srt_filter_path[2:]
|
| 602 |
+
|
| 603 |
+
subtitles_filter = f"subtitles={srt_filter_path}:force_style='{style_str}'"
|
| 604 |
+
print(f"[{video_id}] FFmpeg subtitle filter: {subtitles_filter}")
|
| 605 |
+
|
| 606 |
if job.has_lip_sync:
|
| 607 |
# Lip-synced video already has the synthesized audio merged inside it.
|
| 608 |
# So we map audio track from the same file (0:a)
|
| 609 |
cmd_merge = [
|
| 610 |
"ffmpeg", "-y", "-i", video_src_for_subtitles,
|
| 611 |
+
"-vf", subtitles_filter,
|
| 612 |
"-map", "0:v", "-map", "0:a",
|
| 613 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest",
|
| 614 |
output_video_path
|
|
|
|
| 617 |
# Map audio from input 1 (synthesized audio path)
|
| 618 |
cmd_merge = [
|
| 619 |
"ffmpeg", "-y", "-i", video_src_for_subtitles, "-i", synthesized_audio_path,
|
| 620 |
+
"-vf", subtitles_filter,
|
| 621 |
"-map", "0:v", "-map", "1:a",
|
| 622 |
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest",
|
| 623 |
output_video_path
|