qqwjq1981 commited on
Commit
7c42ba0
Β·
verified Β·
1 Parent(s): 6b66c9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -17
app.py CHANGED
@@ -430,35 +430,69 @@ def add_transcript_voiceover(video_path, translated_json, output_path, add_voice
430
 
431
  logger.info("Video processing completed successfully.")
432
 
433
- # Voice cloning function with debug and error handling
 
 
 
 
 
 
 
 
 
 
434
  def generate_voiceover_clone(translated_json, desired_duration, target_language, speaker_wav_path, output_audio_path):
435
  try:
436
- full_text = " ".join(entry["translated"] for entry in translated_json)
437
- speed_tts = calculate_speed(full_text, desired_duration)
 
 
 
 
 
 
438
  if not speaker_wav_path or not os.path.exists(speaker_wav_path):
439
- return None, "❌ Please upload a valid speaker audio file."
 
 
 
 
 
 
 
 
 
440
 
441
- print(f"πŸ“₯ Received text: {full_text}")
442
- print(f"πŸ“ Speaker audio path: {speaker_wav_path}")
443
- print(f"🌐 Selected language: {target_language}")
444
- print(f"⏱️ Target speed: {speed_tts}")
 
 
445
 
446
- # Run TTS with speed control (if supported by model)
447
  tts.tts_to_file(
448
  text=full_text,
449
  speaker_wav=speaker_wav_path,
450
- language=language,
451
- file_path=output_audio_path,
452
- speed=speed_tts # <- add speed control
 
453
  )
454
- print("βœ… Voice cloning completed.")
 
 
 
 
 
 
455
  return output_audio_path, "βœ… Voice cloning completed successfully."
456
 
457
  except Exception as e:
458
- print("❌ Error during voice cloning:")
459
- traceback.print_exc()
460
- error_msg = f"❌ An error occurred: {str(e)}"
461
- return None, error_msg
462
 
463
  def truncated_linear(x):
464
  if x < 15:
 
430
 
431
  logger.info("Video processing completed successfully.")
432
 
433
+ import os
434
+ import traceback
435
+ from TTS.api import TTS
436
+ import wave
437
+ import logging
438
+
439
+ logger = logging.getLogger(__name__)
440
+
441
+ # Initialize TTS model only once (outside the function)
442
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
443
+
444
  def generate_voiceover_clone(translated_json, desired_duration, target_language, speaker_wav_path, output_audio_path):
445
  try:
446
+ # 1. Assemble full text
447
+ full_text = " ".join(entry["translated"] for entry in translated_json if "translated" in entry and entry["translated"].strip())
448
+
449
+ if not full_text.strip():
450
+ logger.error("❌ Translated text is empty. Skipping TTS generation.")
451
+ return None, "❌ Translated text is empty."
452
+
453
+ # 2. Check speaker file path
454
  if not speaker_wav_path or not os.path.exists(speaker_wav_path):
455
+ logger.error(f"❌ Speaker WAV path not found: {speaker_wav_path}")
456
+ return None, f"❌ Speaker audio not found: {speaker_wav_path}"
457
+
458
+ # Optional: Print speaker audio duration
459
+ try:
460
+ with wave.open(speaker_wav_path, 'rb') as wav_file:
461
+ duration = wav_file.getnframes() / wav_file.getframerate()
462
+ logger.info(f"πŸ”Š Speaker WAV Duration: {duration:.2f}s")
463
+ except Exception as e:
464
+ logger.warning(f"⚠️ Could not read speaker WAV duration: {e}")
465
 
466
+ # 3. Log key inputs
467
+ logger.info(f"πŸ“₯ Received Text: {full_text}")
468
+ logger.info(f"πŸ“ Speaker WAV Path: {speaker_wav_path}")
469
+ logger.info(f"🌐 Target Language: {target_language}")
470
+ logger.info(f"πŸ’Ύ Output Path: {output_audio_path}")
471
+ logger.info(f"⏱️ Target Duration: {desired_duration:.2f}s")
472
 
473
+ # 4. Call TTS to generate audio
474
  tts.tts_to_file(
475
  text=full_text,
476
  speaker_wav=speaker_wav_path,
477
+ language=target_language,
478
+ file_path=output_audio_path
479
+ # Uncomment if your model supports speed:
480
+ # speed=speed_tts
481
  )
482
+
483
+ # 5. Confirm file was written
484
+ if not os.path.exists(output_audio_path):
485
+ logger.error(f"❌ File NOT generated after tts_to_file: {output_audio_path}")
486
+ return None, f"❌ Voiceover file not generated at: {output_audio_path}"
487
+
488
+ logger.info("βœ… Voice cloning completed successfully.")
489
  return output_audio_path, "βœ… Voice cloning completed successfully."
490
 
491
  except Exception as e:
492
+ logger.error("❌ Error during voice cloning:")
493
+ logger.error(traceback.format_exc())
494
+ return None, f"❌ An error occurred: {str(e)}"
495
+
496
 
497
  def truncated_linear(x):
498
  if x < 15: