shaheerawan3 commited on
Commit
bac2847
·
verified ·
1 Parent(s): f250ec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -13
app.py CHANGED
@@ -457,17 +457,51 @@ class ImageScraper:
457
  return False
458
 
459
  def generate_fallback_audio(self, script: str) -> AudioFileClip:
460
- """Generate fallback audio using gTTS"""
461
  try:
462
  audio_path = self.temp_dir / "voice.mp3"
463
- tts = gTTS(text=script, lang='en', slow=False)
464
- tts.save(str(audio_path))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  return AudioFileClip(str(audio_path))
 
466
  except Exception as e:
467
- print(f"Fallback audio generation failed: {e}")
468
- # Create silent audio clip
469
- return AudioFileClip(str(audio_path)) if os.path.exists(str(audio_path)) else None
470
-
 
471
  def scrape_pexels(self, query: str) -> List[str]:
472
  urls = []
473
  try:
@@ -518,16 +552,50 @@ class EnhancedVideoGenerator:
518
 
519
 
520
  def generate_fallback_audio(self, script: str) -> AudioFileClip:
521
- """Generate fallback audio using gTTS"""
522
  try:
523
  audio_path = self.temp_dir / "voice.mp3"
524
- tts = gTTS(text=script, lang='en', slow=False)
525
- tts.save(str(audio_path))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  return AudioFileClip(str(audio_path))
 
527
  except Exception as e:
528
- print(f"Fallback audio generation failed: {e}")
529
- # Create silent audio clip
530
- return AudioFileClip(str(audio_path)) if os.path.exists(str(audio_path)) else None
 
531
 
532
  def apply_video_effects(self, frame: np.ndarray, effect_params: dict) -> np.ndarray:
533
  """Apply various video effects to a frame"""
 
457
  return False
458
 
459
  def generate_fallback_audio(self, script: str) -> AudioFileClip:
460
+ """Generate fallback audio using gTTS with improved error handling"""
461
  try:
462
  audio_path = self.temp_dir / "voice.mp3"
463
+
464
+ # Split long text into chunks to avoid gTTS limitations
465
+ max_chars = 5000
466
+ text_chunks = [script[i:i+max_chars] for i in range(0, len(script), max_chars)]
467
+
468
+ # Create temporary files for each chunk
469
+ temp_files = []
470
+ for i, chunk in enumerate(text_chunks):
471
+ temp_path = self.temp_dir / f"voice_chunk_{i}.mp3"
472
+ tts = gTTS(text=chunk, lang='en', slow=False)
473
+ tts.save(str(temp_path))
474
+ temp_files.append(temp_path)
475
+
476
+ # Concatenate audio files if there are multiple chunks
477
+ if len(temp_files) > 1:
478
+ clips = [AudioFileClip(str(f)) for f in temp_files]
479
+ final_clip = concatenate_audioclips(clips)
480
+ final_clip.write_audiofile(str(audio_path), codec='mp3')
481
+ final_clip.close()
482
+ for clip in clips:
483
+ clip.close()
484
+ else:
485
+ # Just rename the single chunk file
486
+ os.rename(temp_files[0], audio_path)
487
+
488
+ # Clean up temporary files
489
+ for temp_file in temp_files:
490
+ if temp_file.exists():
491
+ temp_file.unlink()
492
+
493
+ # Verify the audio file
494
+ if not audio_path.exists() or audio_path.stat().st_size == 0:
495
+ raise Exception("Audio file generation failed")
496
+
497
  return AudioFileClip(str(audio_path))
498
+
499
  except Exception as e:
500
+ logging.error(f"Audio generation failed: {str(e)}")
501
+ # Create a silent audio clip as last resort
502
+ silence_duration = len(script.split()) / 3 # Approximate duration based on words
503
+ return AudioFileClip(duration=silence_duration)
504
+
505
  def scrape_pexels(self, query: str) -> List[str]:
506
  urls = []
507
  try:
 
552
 
553
 
554
  def generate_fallback_audio(self, script: str) -> AudioFileClip:
555
+ """Generate fallback audio using gTTS with improved error handling"""
556
  try:
557
  audio_path = self.temp_dir / "voice.mp3"
558
+
559
+ # Split long text into chunks to avoid gTTS limitations
560
+ max_chars = 5000
561
+ text_chunks = [script[i:i+max_chars] for i in range(0, len(script), max_chars)]
562
+
563
+ # Create temporary files for each chunk
564
+ temp_files = []
565
+ for i, chunk in enumerate(text_chunks):
566
+ temp_path = self.temp_dir / f"voice_chunk_{i}.mp3"
567
+ tts = gTTS(text=chunk, lang='en', slow=False)
568
+ tts.save(str(temp_path))
569
+ temp_files.append(temp_path)
570
+
571
+ # Concatenate audio files if there are multiple chunks
572
+ if len(temp_files) > 1:
573
+ clips = [AudioFileClip(str(f)) for f in temp_files]
574
+ final_clip = concatenate_audioclips(clips)
575
+ final_clip.write_audiofile(str(audio_path), codec='mp3')
576
+ final_clip.close()
577
+ for clip in clips:
578
+ clip.close()
579
+ else:
580
+ # Just rename the single chunk file
581
+ os.rename(temp_files[0], audio_path)
582
+
583
+ # Clean up temporary files
584
+ for temp_file in temp_files:
585
+ if temp_file.exists():
586
+ temp_file.unlink()
587
+
588
+ # Verify the audio file
589
+ if not audio_path.exists() or audio_path.stat().st_size == 0:
590
+ raise Exception("Audio file generation failed")
591
+
592
  return AudioFileClip(str(audio_path))
593
+
594
  except Exception as e:
595
+ logging.error(f"Audio generation failed: {str(e)}")
596
+ # Create a silent audio clip as last resort
597
+ silence_duration = len(script.split()) / 3 # Approximate duration based on words
598
+ return AudioFileClip(duration=silence_duration)
599
 
600
  def apply_video_effects(self, frame: np.ndarray, effect_params: dict) -> np.ndarray:
601
  """Apply various video effects to a frame"""