sreepathi-ravikumar commited on
Commit
46401d5
·
verified ·
1 Parent(s): d6ecad1

Update video2.py

Browse files
Files changed (1) hide show
  1. video2.py +47 -40
video2.py CHANGED
@@ -27,48 +27,55 @@ 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, 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))
 
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={
32
+ "English": "en-US-JennyNeural",
33
+ "Tamil": "ta-IN-PallaviNeural",
34
+ "Hindi": "hi-IN-SwaraNeural",
35
+ "Malayalam": "ml-IN-SobhanaNeural",
36
+ "Kannada": "kn-IN-SapnaNeural",
37
+ "Telugu": "te-IN-ShrutiNeural",
38
+ "Bengali": "bn-IN-TanishaaNeural",
39
+ "Marathi": "mr-IN-AarohiNeural",
40
+ "Gujarati": "gu-IN-DhwaniNeural",
41
+ "Punjabi": "pa-IN-VaaniNeural",
42
+ "Urdu": "ur-IN-GulNeural",
43
+ "French": "fr-FR-DeniseNeural",
44
+ "German": "de-DE-KatjaNeural",
45
+ "Spanish": "es-ES-ElviraNeural",
46
+ "Italian": "it-IT-IsabellaNeural",
47
+ "Russian": "ru-RU-SvetlanaNeural",
48
+ "Japanese": "ja-JP-NanamiNeural",
49
+ "Korean": "ko-KR-SunHiNeural",
50
+ "Chinese": "zh-CN-XiaoxiaoNeural",
51
+ "Arabic": "ar-SA-ZariyahNeural",
52
+ "Portuguese": "pt-BR-FranciscaNeural",
53
+ "Dutch": "nl-NL-FennaNeural",
54
+ "Greek": "el-GR-AthinaNeural",
55
+ "Hebrew": "he-IL-HilaNeural",
56
+ "Turkish": "tr-TR-EmelNeural",
57
+ "Polish": "pl-PL-AgnieszkaNeural",
58
+ "Thai": "th-TH-AcharaNeural",
59
+ "Vietnamese": "vi-VN-HoaiMyNeural",
60
+ "Swedish": "sv-SE-SofieNeural",
61
+ "Finnish": "fi-FI-NooraNeural",
62
+ "Czech": "cs-CZ-VlastaNeural",
63
+ "Hungarian": "hu-HU-NoemiNeural"
64
+ }
65
  audio_name = f"audio{id}.mp3"
66
  audio_path = os.path.join(AUDIO_DIR, audio_name)
67
+ if len(lang)>1:
68
+ listf = lang.split("&&&")
69
+ text = listf[0].strip()
70
+ langvoice = voice[listf[1].strip()]
71
+ else:
72
+ text=lines[id]
73
+ communicate = edge_tts.Communicate(text=text, voice=langvoice, rate="+0%")
74
+ await communicate.save(audio_path)
75
+ if os.path.exists(audio_path):
76
+ audio = MP3(audio_path)
77
+ duration = audio.info.length
78
+ return duration, audio_path
 
 
 
 
 
 
 
 
79
  return None, None
80
  def audio_func(id,lines,lang):
81
  return asyncio.run(generate_tts(id,lines,lang))