Spaces:
Sleeping
Sleeping
File size: 53,081 Bytes
1fe9360 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
"""
Video Localization Engine
Fixed async event loop issues and added audio time-stretching
"""
import os
import asyncio
import edge_tts
from moviepy.editor import VideoFileClip, AudioFileClip
from pydub import AudioSegment
from pydub.effects import speedup
import tempfile
import logging
import requests
import shutil
from uuid import uuid4
from gtts import gTTS
from deep_translator import GoogleTranslator
# Configure logging first
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Managed output directory (keeps artifacts out of /tmp and makes cleanup predictable)
OUTPUT_DIR = os.path.join(os.getcwd(), "outputs")
os.makedirs(OUTPUT_DIR, exist_ok=True)
def prune_outputs(max_files: int = 10) -> None:
"""Keep the outputs directory from growing without bound by pruning oldest files."""
try:
video_files = [
os.path.join(OUTPUT_DIR, f)
for f in os.listdir(OUTPUT_DIR)
if f.endswith(".mp4")
]
if len(video_files) <= max_files:
return
# Sort newest first
video_files.sort(key=os.path.getmtime, reverse=True)
for stale in video_files[max_files:]:
try:
os.remove(stale)
logger.info(f"Pruned old output: {stale}")
except Exception as remove_error:
logger.debug(f"Could not prune {stale}: {remove_error}")
except Exception as e:
logger.debug(f"Output pruning skipped: {e}")
# Try to import ElevenLabs for premium TTS
try:
from elevenlabs.client import ElevenLabs
ELEVENLABS_AVAILABLE = True
except ImportError:
ELEVENLABS_AVAILABLE = False
if not hasattr(logger, '_elevenlabs_warned'):
logger.warning("ElevenLabs not installed. Install with: pip install elevenlabs")
logger._elevenlabs_warned = True
# Try to import Coqui TTS for high-quality local voices
try:
from TTS.api import TTS
COQUI_TTS_AVAILABLE = True
except ImportError:
COQUI_TTS_AVAILABLE = False
if not hasattr(logger, '_coqui_warned'):
logger.warning("Coqui TTS not installed. Install with: pip install TTS")
logger._coqui_warned = True
# Initialize HF Token (optional - only used for NLLB translation fallback)
HF_TOKEN = os.environ.get("HF_TOKEN")
# ElevenLabs API Key (environment default; UI keys are passed per request)
DEFAULT_ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY")
_elevenlabs_status = None
def set_elevenlabs_api_key(api_key: str):
"""
Deprecated: kept for backward compatibility.
Prefer passing `elevenlabs_api_key` directly to process_video/process_video_sync.
"""
global DEFAULT_ELEVENLABS_API_KEY, _elevenlabs_status
DEFAULT_ELEVENLABS_API_KEY = api_key
_elevenlabs_status = None
def validate_elevenlabs_api_key(api_key: str) -> tuple[bool, str]:
"""
Validate ElevenLabs API key format and test connection.
Returns:
(is_valid, error_message)
"""
if not api_key:
return False, "API key is empty"
# Check format: should start with "sk_" and be reasonable length
if not api_key.startswith("sk_"):
return False, "API key format invalid (should start with 'sk_')"
if len(api_key) < 40:
return False, f"API key too short (got {len(api_key)} chars, expected 40+)"
if not ELEVENLABS_AVAILABLE:
return False, "ElevenLabs package not installed (pip install elevenlabs)"
# Test connection with a simple API call
try:
test_client = ElevenLabs(api_key=api_key)
# Try to get user info - this validates the key
user_info = test_client.user.get()
return True, "API key valid"
except Exception as e:
error_str = str(e).lower()
if "unauthorized" in error_str or "401" in error_str or "invalid" in error_str:
return False, f"API key invalid or expired: {str(e)}"
elif "quota" in error_str or "limit" in error_str:
# Key is valid but quota exceeded - still valid for format
return True, "API key valid (quota exceeded)"
elif "network" in error_str or "connection" in error_str or "timeout" in error_str:
return False, f"Network error: {str(e)}"
else:
return False, f"Connection test failed: {str(e)}"
def check_elevenlabs_quota(client) -> tuple[bool, str]:
"""
Check ElevenLabs quota/credits availability.
Returns:
(has_quota, status_message)
"""
try:
user_info = client.user.get()
if hasattr(user_info, 'subscription'):
sub = user_info.subscription
tier = sub.tier if hasattr(sub, 'tier') else 'N/A'
# Check character limits
if hasattr(sub, 'character_count') and hasattr(sub, 'character_limit'):
used = sub.character_count
limit = sub.character_limit
remaining = limit - used
if remaining <= 0:
return False, f"Quota exhausted: {used}/{limit} characters used"
elif remaining < 1000:
return True, f"Low quota: {remaining}/{limit} characters remaining"
else:
return True, f"Quota available: {remaining}/{limit} characters remaining"
else:
return True, f"Subscription active (tier: {tier})"
else:
return True, "Subscription info unavailable"
except Exception as e:
error_str = str(e).lower()
if "quota" in error_str or "limit" in error_str:
return False, f"Quota check failed: {str(e)}"
else:
# Non-critical error, assume quota available
return True, f"Quota check unavailable: {str(e)}"
def _get_elevenlabs_client(api_key: str | None = None):
"""Create an ElevenLabs client for a specific API key (no global reuse to avoid cross-user leakage)."""
global _elevenlabs_status
if not ELEVENLABS_AVAILABLE:
if _elevenlabs_status is None:
logger.warning("β οΈ ElevenLabs unavailable: Package not installed. Install with: pip install elevenlabs")
_elevenlabs_status = "not_installed"
return None
active_key = api_key or DEFAULT_ELEVENLABS_API_KEY
if not active_key:
_elevenlabs_status = "no_key"
return None
# Validate API key first
is_valid, error_msg = validate_elevenlabs_api_key(active_key)
if not is_valid:
logger.warning(f"β οΈ ElevenLabs unavailable: {error_msg}")
_elevenlabs_status = "invalid_key"
return None
# Initialize client
try:
client = ElevenLabs(api_key=active_key)
logger.info("β
ElevenLabs client initialized for provided key")
# Check quota and log status
has_quota, quota_msg = check_elevenlabs_quota(client)
if has_quota:
logger.info(f"β
ElevenLabs ready: {quota_msg}")
_elevenlabs_status = "ready"
else:
logger.warning(f"β οΈ ElevenLabs quota issue: {quota_msg}")
_elevenlabs_status = "quota_exceeded"
# Still return client - let the TTS function handle quota errors
# Log subscription info for debugging
try:
user_info = client.user.get()
if hasattr(user_info, 'subscription'):
sub = user_info.subscription
tier = sub.tier if hasattr(sub, 'tier') else 'N/A'
logger.info(f"ElevenLabs subscription tier: {tier}")
except Exception as quota_check_error:
logger.debug(f"Could not get subscription details (non-critical): {quota_check_error}")
except Exception as e:
error_str = str(e).lower()
if "unauthorized" in error_str or "401" in error_str:
logger.error(f"β ElevenLabs authentication failed: Invalid API key")
elif "network" in error_str or "connection" in error_str:
logger.error(f"β ElevenLabs connection failed: Network error - {str(e)}")
else:
logger.error(f"β ElevenLabs initialization failed: {str(e)}")
_elevenlabs_status = "init_failed"
return None
return client
# Import local whisper - required for transcription
import whisper
# Cache for local whisper model (lazy-loaded)
_local_whisper_model = None
def _get_local_whisper():
"""Lazy-load local whisper model (base model ~150MB, good balance of speed/accuracy)"""
global _local_whisper_model
if _local_whisper_model is None:
logger.info("Loading local Whisper model (base)... This may take a moment on first run.")
_local_whisper_model = whisper.load_model("base")
logger.info("β
Local Whisper model loaded")
return _local_whisper_model
# Cache for Coqui TTS models
_coqui_tts_models = {}
def _get_coqui_tts(language: str):
"""Lazy-load Coqui TTS model for a language"""
global _coqui_tts_models
if not COQUI_TTS_AVAILABLE:
return None
# Use a single multilingual model for all languages (more efficient)
model_key = "multilingual"
if model_key not in _coqui_tts_models:
try:
# Use XTTS v2 - high-quality multilingual model
# Supports: en, es, fr, de, it, pt, pl, tr, ru, nl, cs, ar, zh, ja, hu, ko
logger.info(f"Loading Coqui TTS multilingual model (XTTS v2)... This may take a moment on first run.")
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False)
_coqui_tts_models[model_key] = tts
logger.info(f"β
Coqui TTS model loaded")
except Exception as e:
logger.warning(f"Failed to load Coqui TTS model: {e}")
return None
return _coqui_tts_models.get(model_key)
async def _coqui_tts_fallback(text: str, language: str, output_file: str) -> None:
"""High-quality TTS using Coqui TTS (runs in executor)."""
def _generate():
tts = _get_coqui_tts(language)
if tts is None:
raise Exception("Coqui TTS model not available")
# XTTS v2 language codes (supported languages)
lang_codes = {
"es": "es", # Spanish
"fr": "fr", # French
"de": "de", # German
"it": "it", # Italian
"ja": "ja", # Japanese
"zh": "zh", # Chinese
"ar": "ar", # Arabic
"hi": "en", # Hindi not directly supported, use English as fallback
}
lang_code = lang_codes.get(language, "en")
# Generate speech with XTTS v2
# XTTS v2 requires speaker_wav for cloning, but we can use it without for basic TTS
try:
tts.tts_to_file(text=text, file_path=output_file, language=lang_code)
except Exception as e:
# If language-specific generation fails, try with English
if lang_code != "en":
logger.warning(f"Coqui TTS failed for {language}, trying English...")
tts.tts_to_file(text=text, file_path=output_file, language="en")
else:
raise
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, _generate)
async def _elevenlabs_tts(text: str, language: str, output_file: str, api_key: str | None = None) -> None:
"""Premium TTS using ElevenLabs Voice Library (runs in executor)."""
def _generate():
client = _get_elevenlabs_client(api_key=api_key)
if client is None:
raise Exception("ElevenLabs client not available")
# Map languages to ElevenLabs voice IDs from their voice library
# Using multilingual voices that support multiple languages well
voice_map = {
"es": "pNInz6obpgDQGcFmaJgB", # Adam - works well for Spanish
"fr": "EXAVITQu4vr4xnSDxMaL", # Bella - works well for French
"de": "ErXwobaYiN019PkySvjV", # Antoni - works well for German
"it": "MF3mGyEYCl7XYWbV9V6O", # Elli - works well for Italian
"ja": "TxGEqnHWrfWFTfGW9XjX", # Josh - works well for Japanese
"zh": "VR6AewLTigWG4xSOukaG", # Arnold - works well for Chinese
"hi": "pNInz6obpgDQGcFmaJgB", # Adam - fallback for Hindi
"ar": "EXAVITQu4vr4xnSDxMaL", # Bella - fallback for Arabic
}
# Get voice ID, default to Adam if language not mapped
voice_id = voice_map.get(language, "pNInz6obpgDQGcFmaJgB")
# Use turbo model for efficiency (fewer credits) while maintaining good quality
# For longer texts, we'll chunk them to stay within quota limits
model_id = "eleven_turbo_v2_5" # Fast and credit-efficient
# Use lower quality format to minimize credits (still sounds good)
# mp3_22050_32 uses fewer credits than mp3_44100_128
output_format = "mp3_22050_32" # Lower credits, still good quality
try:
# Check text length - ElevenLabs uses character-based pricing
# The error "60 credits required" for 120 chars suggests ~0.5 credits per char
# To work within any quota limits, use small chunks
# Note: Even with 109k+ subscription credits, there may be per-request character limits
max_chars_per_request = 100 # Reasonable chunk size - should work with most quotas
# Always chunk if text is longer than threshold to minimize per-request costs
if len(text) > max_chars_per_request:
logger.info(f"Text is {len(text)} chars, chunking into small pieces for ElevenLabs (max {max_chars_per_request} chars per chunk)...")
# Split by sentences first, then by commas, then by spaces if needed
import re
# First try splitting by sentences
sentences = re.split(r'([.!?]\s+)', text)
chunks = []
current_chunk = ""
for i in range(0, len(sentences), 2):
sentence = sentences[i] + (sentences[i+1] if i+1 < len(sentences) else "")
# If single sentence is too long, split by commas, then by spaces if needed
if len(sentence) > max_chars_per_request:
parts = re.split(r'([,;]\s+)', sentence)
for j in range(0, len(parts), 2):
part = parts[j] + (parts[j+1] if j+1 < len(parts) else "")
# If part is still too long, split by spaces
if len(part) > max_chars_per_request:
words = part.split()
for word in words:
if len(current_chunk) + len(word) + 1 > max_chars_per_request:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = word + " "
else:
current_chunk += word + " "
elif len(current_chunk) + len(part) > max_chars_per_request:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = part
else:
current_chunk += part
elif len(current_chunk) + len(sentence) > max_chars_per_request:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence
else:
current_chunk += sentence
if current_chunk:
chunks.append(current_chunk.strip())
logger.info(f"Split text into {len(chunks)} chunks for efficient credit usage")
# Generate audio for each chunk and concatenate
combined = AudioSegment.empty()
for idx, chunk in enumerate(chunks):
logger.info(f"Generating ElevenLabs audio for chunk {idx+1}/{len(chunks)} ({len(chunk)} chars)...")
try:
chunk_audio_stream = client.text_to_speech.convert(
voice_id=voice_id,
text=chunk,
model_id=model_id,
output_format=output_format
)
# Save chunk to temp file
chunk_file = output_file.replace('.mp3', f'_chunk_{idx}.mp3')
with open(chunk_file, "wb") as f:
for chunk_data in chunk_audio_stream:
f.write(chunk_data)
# Validate chunk audio file
if not os.path.exists(chunk_file) or os.path.getsize(chunk_file) == 0:
raise Exception(f"Chunk {idx+1} audio file is empty or missing")
# Load and concatenate
chunk_audio = AudioSegment.from_file(chunk_file)
if len(chunk_audio) == 0:
raise Exception(f"Chunk {idx+1} audio has zero duration")
logger.debug(f"Chunk {idx+1} audio: {len(chunk_audio)}ms, {os.path.getsize(chunk_file)} bytes")
combined += chunk_audio
# Clean up temp file
os.remove(chunk_file)
except Exception as chunk_error:
# Enhanced error handling with specific error types
error_str = str(chunk_error).lower()
error_msg = str(chunk_error)
# Clean up any partial files first
for cleanup_idx in range(idx + 1):
cleanup_file = output_file.replace('.mp3', f'_chunk_{cleanup_idx}.mp3')
if os.path.exists(cleanup_file):
os.remove(cleanup_file)
# Categorize error
if 'quota' in error_str or 'credits' in error_str or 'limit' in error_str:
logger.warning(f"β οΈ ElevenLabs quota/credit limit reached on chunk {idx+1}/{len(chunks)}")
logger.warning(f" Error: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise Exception("ElevenLabs quota exceeded") from chunk_error
elif 'unauthorized' in error_str or '401' in error_str or 'invalid' in error_str:
logger.error(f"β ElevenLabs authentication failed on chunk {idx+1}")
logger.error(f" Error: {error_msg}")
logger.error(" Check your ELEVENLABS_API_KEY environment variable")
raise Exception("ElevenLabs authentication failed") from chunk_error
elif 'network' in error_str or 'connection' in error_str or 'timeout' in error_str:
logger.warning(f"β οΈ ElevenLabs network error on chunk {idx+1}: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise Exception("ElevenLabs network error") from chunk_error
else:
logger.warning(f"β οΈ ElevenLabs error on chunk {idx+1}: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise # Re-raise to trigger fallback
# Validate combined audio
if len(combined) == 0:
raise Exception("Combined audio has zero duration")
# Export combined audio
combined.export(output_file, format="mp3")
# Verify exported file
if not os.path.exists(output_file) or os.path.getsize(output_file) == 0:
raise Exception("Exported audio file is empty or missing")
logger.info(f"β
Combined {len(chunks)} ElevenLabs audio chunks ({len(combined)}ms, {os.path.getsize(output_file)} bytes)")
else:
# Generate audio with ElevenLabs for short texts (under max_chars_per_request)
logger.info(f"Generating ElevenLabs audio for short text ({len(text)} chars)...")
audio_stream = client.text_to_speech.convert(
voice_id=voice_id,
text=text,
model_id=model_id,
output_format=output_format
)
# Save to file
with open(output_file, "wb") as f:
bytes_written = 0
for chunk in audio_stream:
f.write(chunk)
bytes_written += len(chunk)
# Validate saved file
if not os.path.exists(output_file) or os.path.getsize(output_file) == 0:
raise Exception("Generated audio file is empty or missing")
# Verify audio can be loaded and has duration
file_size = os.path.getsize(output_file)
try:
test_audio = AudioSegment.from_file(output_file)
audio_duration = len(test_audio)
if audio_duration == 0:
raise Exception("Generated audio has zero duration")
logger.info(f"β
ElevenLabs audio generated successfully ({len(text)} chars, {audio_duration}ms, {file_size} bytes)")
except Exception as validation_error:
logger.error(f"β Audio validation failed: {validation_error}")
raise Exception(f"Generated audio is invalid: {validation_error}") from validation_error
except Exception as e:
error_str = str(e).lower()
error_msg = str(e)
# Enhanced error categorization
if 'quota' in error_str or 'credits' in error_str or 'limit' in error_str:
logger.warning(f"β οΈ ElevenLabs quota/credit limit reached: {error_msg}")
logger.warning(" Note: This might be a character-based quota limit, not subscription credits.")
logger.warning(" ElevenLabs uses character credits which may be separate from your subscription balance.")
logger.info(" Falling back to alternative TTS methods...")
raise Exception("ElevenLabs quota exceeded") from e
elif 'unauthorized' in error_str or '401' in error_str or 'invalid' in error_str or 'authentication' in error_str:
logger.error(f"β ElevenLabs authentication failed: {error_msg}")
logger.error(" Check your ELEVENLABS_API_KEY environment variable")
logger.error(" Get a valid API key from: https://elevenlabs.io/app/settings/api-keys")
raise Exception("ElevenLabs authentication failed") from e
elif 'network' in error_str or 'connection' in error_str or 'timeout' in error_str:
logger.warning(f"β οΈ ElevenLabs network error: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise Exception("ElevenLabs network error") from e
elif 'service' in error_str or 'unavailable' in error_str or '503' in error_str or '500' in error_str:
logger.warning(f"β οΈ ElevenLabs service unavailable: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise Exception("ElevenLabs service unavailable") from e
else:
logger.warning(f"β οΈ ElevenLabs TTS generation failed: {error_msg}")
logger.info(" Falling back to alternative TTS methods...")
raise # Re-raise to trigger fallback
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, _generate)
async def _gtts_fallback(text: str, language: str, output_file: str) -> None:
"""Last resort TTS using gTTS (runs in executor)."""
gtts_languages = {
"es": "es",
"fr": "fr",
"de": "de",
"it": "it",
"ja": "ja",
"zh": "zh-CN",
"hi": "hi",
"ar": "ar",
"en": "en"
}
lang_code = gtts_languages.get(language, "en")
def _save():
tts = gTTS(text=text, lang=lang_code)
tts.save(output_file)
loop = asyncio.get_running_loop()
await loop.run_in_executor(None, _save)
async def text_to_speech(text: str, language: str, output_file: str, elevenlabs_api_key: str | None = None) -> None:
"""Generate speech using ElevenLabs (PRIMARY), with fallbacks to Edge TTS, Coqui TTS, and gTTS"""
# Method 1: PRIMARY - ElevenLabs (Premium professional-grade TTS)
if ELEVENLABS_AVAILABLE:
try:
logger.info(f"Generating TTS with ElevenLabs (premium quality) for {language}...")
await _elevenlabs_tts(text, language, output_file, api_key=elevenlabs_api_key)
logger.info("β
TTS generated via ElevenLabs (premium quality)")
return
except Exception as elevenlabs_error:
logger.warning(f"ElevenLabs TTS failed: {elevenlabs_error}")
# Continue to fallbacks
# Method 2: Fallback - Edge TTS (High quality, free)
voices = {
"es": ["es-ES-AlvaroNeural", "es-ES-ElviraNeural"],
"fr": ["fr-FR-HenriNeural", "fr-FR-DeniseNeural"],
"de": ["de-DE-KillianNeural", "de-DE-KatjaNeural"],
"it": ["it-IT-DiegoNeural", "it-IT-ElsaNeural"],
"ja": ["ja-JP-KeitaNeural", "ja-JP-NanamiNeural"],
"zh": ["zh-CN-YunxiNeural", "zh-CN-XiaoxiaoNeural"],
"hi": ["hi-IN-MadhurNeural", "hi-IN-SwaraNeural"],
"ar": ["ar-SA-HamedNeural", "ar-SA-ZariyahNeural"]
}
voice_list = voices.get(language, ["en-US-ChristopherNeural", "en-US-AriaNeural"])
max_retries = 3
retry_delay = 2 # seconds
last_error = None
for attempt in range(max_retries):
for voice in voice_list:
try:
logger.info(f"Trying Edge TTS (attempt {attempt + 1}/{max_retries}, voice: {voice})...")
# Create communicate object with timeout
communicate = edge_tts.Communicate(text, voice)
# Save with timeout protection
try:
await asyncio.wait_for(
communicate.save(output_file),
timeout=60.0 # 60 second timeout
)
logger.info(f"β
TTS generated via Edge TTS: {language} (voice: {voice})")
return # Success!
except asyncio.TimeoutError:
logger.warning(f"TTS timeout for voice {voice}, trying next...")
continue
except Exception as e:
error_msg = str(e)
last_error = e # Capture the error
# Check if it's a 403 or connection error
if "403" in error_msg or "Invalid response status" in error_msg:
logger.warning(f"Edge TTS 403/connection error with voice {voice}: {e}")
# Wait before trying next voice
await asyncio.sleep(retry_delay)
continue
else:
raise # Re-raise if it's a different error
except Exception as e:
last_error = e # Always capture the error
error_msg = str(e)
if "403" in error_msg or "Invalid response status" in error_msg:
logger.warning(f"Edge TTS error (attempt {attempt + 1}): {e}")
if attempt < max_retries - 1:
# Exponential backoff
wait_time = retry_delay * (2 ** attempt)
logger.info(f"Waiting {wait_time}s before retry...")
await asyncio.sleep(wait_time)
continue
else:
# For other errors, try next voice immediately
continue
# Method 3: Fallback - Coqui TTS (high-quality local neural TTS)
if COQUI_TTS_AVAILABLE:
try:
logger.warning("Edge TTS failed. Trying Coqui TTS (high-quality local)...")
await _coqui_tts_fallback(text, language, output_file)
logger.info("β
TTS generated via Coqui TTS (high quality)")
return
except Exception as coqui_error:
logger.warning(f"Coqui TTS failed: {coqui_error}")
last_error = last_error or coqui_error
# Method 4: Last resort - gTTS (mechanical but reliable)
try:
logger.warning("Falling back to gTTS (mechanical quality)...")
await _gtts_fallback(text, language, output_file)
logger.info("β
TTS generated via gTTS fallback")
return
except Exception as fallback_error:
logger.error(f"gTTS fallback failed: {fallback_error}")
last_error = last_error or fallback_error
error_details = str(last_error) if last_error else "Unknown error (all TTS methods failed)"
error_msg = (
f"Failed to generate TTS with all methods (ElevenLabs, Edge TTS, Coqui TTS, gTTS). "
f"Last error: {error_details}. "
f"This might be due to network issues or TTS service unavailability."
)
logger.error(error_msg)
raise Exception(error_msg)
def transcribe_audio(audio_path: str) -> str:
"""Transcribe audio using local Whisper model (primary method)"""
try:
logger.info("Transcribing audio with local Whisper...")
# Use local Whisper as the primary and only method
# This is more reliable than cloud APIs which are frequently unavailable
model = _get_local_whisper()
result = model.transcribe(audio_path)
text = result.get("text", "").strip()
if text:
logger.info(f"β
Transcribed: {len(text)} characters")
return text
else:
logger.warning("Whisper returned empty transcription")
return "Error identifying speech."
except Exception as e:
logger.error(f"Transcription error: {e}")
return "Error identifying speech."
def translate_text(text: str, target_lang: str) -> str:
"""Translate text using deep-translator (primary) with NLLB API fallback"""
# Don't translate error messages or empty text
if text == "Error identifying speech." or not text.strip():
return text
try:
logger.info(f"Translating to {target_lang}...")
# Method 1: Primary - deep-translator (local, reliable, no API key needed)
try:
# Map language codes for deep-translator
translator_lang_map = {
"es": "es",
"fr": "fr",
"de": "de",
"it": "it",
"ja": "ja",
"zh": "zh-CN", # Chinese simplified
"hi": "hi",
"ar": "ar"
}
translator_target = translator_lang_map.get(target_lang, target_lang)
translator = GoogleTranslator(source='en', target=translator_target)
translated = translator.translate(text)
if translated and translated != text and translated.strip():
logger.info(f"β
Translated via deep-translator: {len(translated)} characters")
return translated.strip()
else:
logger.warning("deep-translator returned empty or same text")
except Exception as e:
logger.warning(f"deep-translator failed: {e}")
# Method 2: Fallback - NLLB via HF API (only if HF_TOKEN is available)
if HF_TOKEN:
try:
# NLLB language codes mapping
nllb_codes = {
"es": "spa_Latn",
"fr": "fra_Latn",
"de": "deu_Latn",
"it": "ita_Latn",
"ja": "jpn_Jpan",
"zh": "zho_Hans",
"hi": "hin_Deva",
"ar": "arb_Arab"
}
tgt_lang = nllb_codes.get(target_lang, "spa_Latn")
api_url = "https://router.huggingface.co/hf-inference/models/facebook/nllb-200-distilled-600M"
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"inputs": text,
"parameters": {"src_lang": "eng_Latn", "tgt_lang": tgt_lang}
}
response = requests.post(api_url, headers=headers, json=payload, timeout=30)
if response.status_code == 200:
data = response.json()
translated = ""
if isinstance(data, list) and data:
translated = data[0].get("translation_text", "")
elif isinstance(data, dict):
translated = data.get("translation_text", "")
translated = (translated or "").strip()
if translated and translated != text:
logger.info(f"β
Translated via NLLB: {len(translated)} characters")
return translated
else:
logger.warning("NLLB returned empty or same text")
else:
logger.warning(f"NLLB translation returned {response.status_code}: {response.text[:100]}")
except requests.exceptions.Timeout:
logger.warning("NLLB translation timed out")
except Exception as e:
logger.warning(f"NLLB translation failed: {e}")
else:
logger.debug("HF_TOKEN not set, skipping NLLB translation fallback")
# Last resort: return original text with warning
logger.error("All translation methods failed, using original text")
return text
except Exception as e:
logger.error(f"Translation error: {e}")
return text # Return original if translation fails
def adjust_audio_duration(audio_path: str, target_duration_ms: int, output_path: str) -> bool:
"""
Adjust audio duration to match video using time-stretching.
Args:
audio_path: Input audio file
target_duration_ms: Target duration in milliseconds
output_path: Output audio file
Returns:
Success boolean
"""
try:
logger.info(f"Adjusting audio duration to {target_duration_ms}ms...")
# Load audio
audio = AudioSegment.from_file(audio_path)
current_duration = len(audio)
if current_duration == 0:
logger.error("Audio has zero duration")
return False
# Calculate speed ratio (how much to speed up/slow down)
# If current is 10s and target is 8s, we need to speed up by 10/8 = 1.25x
speed_ratio = current_duration / target_duration_ms
logger.info(f"Current audio: {current_duration}ms, target: {target_duration_ms}ms, ratio: {speed_ratio:.2f}x")
# Apply speed change (limit to reasonable range to avoid distortion)
# Only adjust if ratio is between 0.7 and 1.5 (more conservative to avoid corruption)
if 0.7 <= speed_ratio <= 1.5:
try:
# Use speedup function - it handles both speeding up and slowing down
adjusted = speedup(audio, playback_speed=speed_ratio)
# Verify adjusted duration is reasonable (should be close to target)
adjusted_duration = len(adjusted)
if adjusted_duration == 0:
logger.error("Adjusted audio has zero duration")
return False
# Check if adjusted duration is reasonable (within 30% of target)
duration_diff = abs(adjusted_duration - target_duration_ms) / target_duration_ms
if duration_diff > 0.3:
logger.warning(f"Adjusted duration ({adjusted_duration}ms) too far from target ({target_duration_ms}ms), using original")
audio.export(output_path, format="mp3", bitrate="128k")
return True
# Export with proper parameters
adjusted.export(output_path, format="mp3", bitrate="128k")
# Verify output file exists and has reasonable size
if not os.path.exists(output_path):
logger.error("Adjusted audio file was not created")
return False
output_size = os.path.getsize(output_path)
input_size = os.path.getsize(audio_path)
# Check if output is suspiciously small (less than 20% of input)
if output_size < input_size * 0.2:
logger.warning(f"Adjusted audio file too small ({output_size} bytes vs {input_size} bytes), using original")
audio.export(output_path, format="mp3", bitrate="128k")
return True
logger.info(f"β
Audio adjusted: {current_duration}ms β {adjusted_duration}ms ({speed_ratio:.2f}x, {output_size} bytes)")
return True
except Exception as adjust_error:
logger.warning(f"Audio adjustment failed: {adjust_error}, using original")
audio.export(output_path, format="mp3", bitrate="128k")
return True
else:
logger.warning(f"Speed ratio {speed_ratio:.2f}x out of safe range (0.7-1.5), using original audio")
# Just copy original audio
audio.export(output_path, format="mp3", bitrate="128k")
return True
except Exception as e:
logger.error(f"Audio adjustment failed: {e}")
# Try to copy original as fallback
try:
audio = AudioSegment.from_file(audio_path)
audio.export(output_path, format="mp3", bitrate="128k")
logger.warning("Using original audio as fallback")
return True
except:
return False
async def process_video_async(
video_path: str,
target_lang: str = "es",
elevenlabs_api_key: str | None = None,
progress_callback=None,
) -> tuple:
"""
Main async pipeline: Video -> Audio -> Text -> Trans-Text -> Audio -> Video
Returns:
(output_path, original_text, translated_text)
"""
temp_dir = tempfile.mkdtemp(prefix="localizer_")
audio_path = os.path.join(temp_dir, "extracted_audio.mp3")
tts_path = os.path.join(temp_dir, "tts_audio.mp3")
adjusted_audio_path = os.path.join(temp_dir, "adjusted_audio.mp3")
output_video_path = os.path.join(
OUTPUT_DIR, f"localized_{target_lang}_{uuid4().hex}.mp4"
)
progress = progress_callback or (lambda *args, **kwargs: None)
video = None
new_audio = None
try:
logger.info(f"Starting video localization to {target_lang}...")
progress(0.02, desc="Extracting audio...")
# 1. Extract Audio
video = VideoFileClip(video_path)
video_duration_ms = int(video.duration * 1000)
video.audio.write_audiofile(audio_path, verbose=False, logger=None)
logger.info(f"β
Audio extracted ({video.duration:.1f}s)")
progress(0.15, desc="Transcribing with Whisper...")
# 2. Transcribe
original_text = transcribe_audio(audio_path)
progress(0.35, desc="Translating text...")
# 3. Translate
translated_text = translate_text(original_text, target_lang)
progress(0.5, desc="Generating voice...")
# 4. Generate TTS
# Split long text into chunks to avoid rate limiting
if len(translated_text) > 500:
logger.info(f"Text is long ({len(translated_text)} chars), splitting into chunks...")
# Split by sentences if possible
import re
sentences = re.split(r'([.!?]\s+)', translated_text)
chunks = []
current_chunk = ""
for i in range(0, len(sentences), 2):
sentence = sentences[i] + (sentences[i+1] if i+1 < len(sentences) else "")
if len(current_chunk) + len(sentence) > 500:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence
else:
current_chunk += sentence
if current_chunk:
chunks.append(current_chunk.strip())
# Generate TTS for each chunk and concatenate
chunk_files = []
for idx, chunk in enumerate(chunks):
chunk_file = os.path.join(temp_dir, f"tts_chunk_{idx}.mp3")
await text_to_speech(
chunk, target_lang, chunk_file, elevenlabs_api_key=elevenlabs_api_key
)
chunk_files.append(chunk_file)
# Concatenate audio chunks
combined = AudioSegment.empty()
for chunk_file in chunk_files:
chunk_audio = AudioSegment.from_file(chunk_file)
combined += chunk_audio
combined.export(tts_path, format="mp3")
logger.info(f"β
Combined {len(chunks)} TTS chunks")
else:
await text_to_speech(
translated_text, target_lang, tts_path, elevenlabs_api_key=elevenlabs_api_key
)
# 5. Validate TTS audio file before processing
if not os.path.exists(tts_path):
raise Exception(f"TTS audio file not found: {tts_path}")
file_size = os.path.getsize(tts_path)
if file_size == 0:
raise Exception(f"TTS audio file is empty: {tts_path}")
# Basic validation - just check file exists and has content
logger.info(f"β
TTS audio file ready: {file_size} bytes")
progress(0.65, desc="Aligning audio to video...")
# 5. Adjust audio duration to match video (with validation)
# First, check original audio duration
try:
original_audio = AudioSegment.from_file(tts_path)
original_duration_ms = len(original_audio)
logger.info(f"Original TTS audio duration: {original_duration_ms}ms, target: {video_duration_ms}ms")
# Only adjust if there's a significant difference (>20%)
duration_diff = abs(original_duration_ms - video_duration_ms) / video_duration_ms
if duration_diff > 0.2:
success = adjust_audio_duration(tts_path, video_duration_ms, adjusted_audio_path)
# Validate adjusted audio before using it
if success and os.path.exists(adjusted_audio_path):
adjusted_size = os.path.getsize(adjusted_audio_path)
original_size = os.path.getsize(tts_path)
# Verify adjusted audio duration is reasonable (within 50% of target)
try:
test_audio = AudioSegment.from_file(adjusted_audio_path)
adjusted_duration_ms = len(test_audio)
# Check if adjusted duration is reasonable (at least 50% of target, max 150%)
if adjusted_duration_ms >= video_duration_ms * 0.5 and adjusted_duration_ms <= video_duration_ms * 1.5:
audio_to_use = adjusted_audio_path
logger.info(f"β
Using adjusted audio: {adjusted_duration_ms}ms (target: {video_duration_ms}ms), {adjusted_size} bytes")
else:
logger.warning(f"β οΈ Adjusted audio duration ({adjusted_duration_ms}ms) not reasonable for target ({video_duration_ms}ms), using original")
audio_to_use = tts_path
except Exception as validation_error:
logger.warning(f"β οΈ Could not validate adjusted audio: {validation_error}, using original")
audio_to_use = tts_path
else:
logger.warning("β οΈ Audio adjustment failed, using original")
audio_to_use = tts_path
else:
logger.info(f"Audio duration close enough ({duration_diff*100:.1f}% difference), using original")
audio_to_use = tts_path
except Exception as e:
logger.warning(f"β οΈ Could not check audio duration: {e}, using original")
audio_to_use = tts_path
logger.info(f"β
Final audio to use: {os.path.getsize(audio_to_use)} bytes")
progress(0.75, desc="Merging audio with video...")
# 6. Merge audio with video - validate audio file first
if not os.path.exists(audio_to_use) or os.path.getsize(audio_to_use) == 0:
raise Exception(f"Audio file for merging is missing or empty: {audio_to_use}")
logger.info(f"Merging audio ({os.path.getsize(audio_to_use)} bytes) with video...")
new_audio = AudioFileClip(audio_to_use)
# Verify audio clip is valid and has reasonable duration
audio_duration = new_audio.duration
if audio_duration == 0:
logger.error(f"β Audio clip has zero duration")
raise Exception("Audio clip has zero duration - cannot merge with video")
# CRITICAL: If audio is much shorter than video, it will be mostly silent
# Fall back to original TTS audio if adjusted one is too short
if audio_duration < video.duration * 0.3:
logger.warning(f"β οΈ Audio duration ({audio_duration:.2f}s) is too short for video ({video.duration:.2f}s)")
logger.warning(" This would create a mostly silent video. Trying original TTS audio...")
# Try original TTS audio
if audio_to_use != tts_path and os.path.exists(tts_path):
new_audio.close()
try:
new_audio = AudioFileClip(tts_path)
audio_duration = new_audio.duration
if audio_duration > video.duration * 0.3:
logger.info(f"β
Using original TTS audio: {audio_duration:.2f}s")
else:
logger.error(f"β Original TTS audio also too short: {audio_duration:.2f}s")
raise Exception(f"TTS audio too short ({audio_duration:.2f}s) for video ({video.duration:.2f}s)")
except Exception as e:
logger.error(f"β Could not use original TTS audio: {e}")
raise Exception(f"Cannot create video with valid audio: {e}")
else:
raise Exception(f"Audio too short ({audio_duration:.2f}s) for video ({video.duration:.2f}s) - would be mostly silent")
logger.info(f"β
Audio clip loaded: {audio_duration:.2f}s (video: {video.duration:.2f}s)")
final_video = video.set_audio(new_audio)
# 7. Write output
logger.info("Writing output video...")
progress(0.9, desc="Rendering output video...")
final_video.write_videofile(
output_video_path,
codec='libx264',
audio_codec='aac',
verbose=False,
logger=None
)
prune_outputs()
progress(1.0, desc="Done")
logger.info(f"β
Video localization complete! Saved to {output_video_path}")
return output_video_path, original_text, translated_text
except Exception as e:
logger.error(f"Pipeline Error: {e}")
return None, str(e), "Error"
finally:
# Cleanup
if video: video.close()
if new_audio: new_audio.close()
try:
shutil.rmtree(temp_dir, ignore_errors=True)
except Exception as cleanup_error:
logger.debug(f"Temp cleanup skipped: {cleanup_error}")
def process_video_sync(
video_path: str,
target_lang: str = "es",
elevenlabs_api_key: str | None = None,
progress_callback=None,
) -> tuple:
"""
Synchronous wrapper for async video processing.
Handles event loop creation safely.
Returns:
(output_path, original_text, translated_text)
"""
try:
# Try to get existing event loop
loop = asyncio.get_event_loop()
if loop.is_running():
# We're already in an async context, create a new loop
import nest_asyncio
nest_asyncio.apply()
return loop.run_until_complete(
process_video_async(
video_path,
target_lang,
elevenlabs_api_key=elevenlabs_api_key,
progress_callback=progress_callback,
)
)
else:
# No running loop, safe to use asyncio.run()
return asyncio.run(
process_video_async(
video_path,
target_lang,
elevenlabs_api_key=elevenlabs_api_key,
progress_callback=progress_callback,
)
)
except RuntimeError:
# No event loop exists, create one
return asyncio.run(
process_video_async(
video_path,
target_lang,
elevenlabs_api_key=elevenlabs_api_key,
progress_callback=progress_callback,
)
)
# Convenience alias for backward compatibility
def process_video(
video_path: str,
target_lang: str = "es",
elevenlabs_api_key: str | None = None,
progress_callback=None,
) -> tuple:
"""
Main entry point for video localization.
Wrapper around process_video_sync for convenience.
Returns:
(output_path, original_text, translated_text)
"""
return process_video_sync(
video_path,
target_lang,
elevenlabs_api_key=elevenlabs_api_key,
progress_callback=progress_callback,
)
# ==========================
# Startup Validation
# ==========================
# Validate ElevenLabs on module import
def _validate_elevenlabs_on_startup():
"""Validate ElevenLabs on module import."""
global ELEVENLABS_AVAILABLE, _elevenlabs_status
logger.info("Initializing Video Localization Engine...")
if not ELEVENLABS_AVAILABLE:
logger.info("ElevenLabs not installed. Using open source models (EdgeTTS, Coqui, gTTS)")
_elevenlabs_status = "not_installed"
return
if DEFAULT_ELEVENLABS_API_KEY:
is_valid, message = validate_elevenlabs_api_key(DEFAULT_ELEVENLABS_API_KEY)
if is_valid:
logger.info("ElevenLabs API key found and validated")
_elevenlabs_status = "ready"
else:
logger.info(f"ElevenLabs API key not valid. Using open source models: {message}")
_elevenlabs_status = "invalid_key"
else:
logger.info("No ElevenLabs API key found. Using open source models (EdgeTTS, Coqui, gTTS)")
logger.info("Add your API key in the UI for premium voice quality")
_elevenlabs_status = "no_key"
# Run validation on import
_validate_elevenlabs_on_startup()
|