sreepathi-ravikumar commited on
Commit
d6ecad1
·
verified ·
1 Parent(s): 0a4daed

Update video2.py

Browse files
Files changed (1) hide show
  1. video2.py +40 -14
video2.py CHANGED
@@ -27,22 +27,48 @@ CLIPS_DIR = os.path.join(BASE_DIR, "video")
27
  # Create directories (no chmod needed)
28
  for path in [BASE_DIR, AUDIO_DIR, CLIPS_DIR]:
29
  Path(path).mkdir(parents=True, exist_ok=True)
30
- async def generate_tts(id,lines,lang):
31
- voice = "ta-IN-PallaviNeural"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  audio_name = f"audio{id}.mp3"
33
  audio_path = os.path.join(AUDIO_DIR, audio_name)
34
- if len(lang)>1:
35
- listf = lang.split("&&&")
36
- text = listf[0]
37
- langvoice = listf[1].strip()
38
- else:
39
- text=lines[id]
40
- communicate = edge_tts.Communicate(text=lang, voice=voice, rate="+0%")
41
- await communicate.save(audio_path)
42
- if os.path.exists(audio_path):
43
- audio = MP3(audio_path)
44
- duration = audio.info.length
45
- return duration, audio_path
 
 
 
 
 
 
 
 
46
  return None, None
47
  def audio_func(id,lines,lang):
48
  return asyncio.run(generate_tts(id,lines,lang))
 
27
  # Create directories (no chmod needed)
28
  for path in [BASE_DIR, AUDIO_DIR, CLIPS_DIR]:
29
  Path(path).mkdir(parents=True, exist_ok=True)
30
+ async def generate_tts(id, lang):
31
+ listf = lang.split("&&&")
32
+ text = listf[0].strip()
33
+
34
+ # Choose appropriate voice based on language detection
35
+ if len(listf) > 1:
36
+ voice = listf[1].strip()
37
+ else:
38
+ # Auto-detect language and choose appropriate voice
39
+ # For Tamil text, use a Tamil voice
40
+ if any('\u0B80' <= char <= '\u0BFF' for char in text): # Tamil Unicode range
41
+ voice = "ta-IN-PallaviNeural" # Tamil voice
42
+ else:
43
+ voice = "en-US-JennyNeural" # Default English voice
44
+
45
+ # Check for empty text
46
+ if not text:
47
+ print("Error: Text is empty.")
48
+ return None, None
49
+
50
  audio_name = f"audio{id}.mp3"
51
  audio_path = os.path.join(AUDIO_DIR, audio_name)
52
+
53
+ max_retries = 3
54
+ for attempt in range(max_retries):
55
+ try:
56
+ communicate = edge_tts.Communicate(text=text, voice=voice, rate="+0%")
57
+ await communicate.save(audio_path)
58
+ if os.path.exists(audio_path) and os.path.getsize(audio_path) > 0:
59
+ audio = MP3(audio_path)
60
+ duration = audio.info.length
61
+ print(f"Generated audio for '{text[:50]}...' with voice {voice}, duration: {duration}s")
62
+ return duration, audio_path
63
+ else:
64
+ print(f"Attempt {attempt + 1}: Audio file was not created.")
65
+ except Exception as e:
66
+ print(f"Attempt {attempt + 1} failed: {str(e)}")
67
+ if attempt < max_retries - 1:
68
+ await asyncio.sleep(1)
69
+ else:
70
+ print("All attempts failed.")
71
+ return None, None
72
  return None, None
73
  def audio_func(id,lines,lang):
74
  return asyncio.run(generate_tts(id,lines,lang))