txt to voice, as per my own speed and male female or language.
updated code.
value = 7 # 🔄 Change value from 1 (slowest) to 7 (fastest)# *****
-----------
# Install necessary libraries
#code generated 29th july 25.
!pip install edge-tts pydub --quiet
!apt install ffmpeg --quiet
# Optional: Download libraries offline
!pip download edge-tts pydub -d /content/drive/MyDrive/Lib/
import os
import asyncio
from datetime import datetime
import edge_tts
from pydub import AudioSegment
# Constants
INPUT_FILE = "/content/rent_house.txt" # Change to your text file
VOICE = "te-IN-ShrutiNeural" # Telugu female voice; use "te-IN-MohanNeural" for male
CHUNK_SIZE = 4000
TEMP_DIR = "/content/temp_chunks"
FINAL_MP3 = f"/content/output_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
## search below for value = 3 ; we can set any value from 1 to 7;
# Speed mapping (1 = slowest, 7 = fastest)
def map_speed_to_rate(value):
mapping = {
1: "-75%", 2: "-50%", 3: "0%",
4: "+25%", 5: "+50%", 6: "+75%", 7: "+100%"
}
return mapping.get(value, "0%")
# Read and clean input text
def read_text(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return " ".join(f.read().split())
except Exception as e:
print(f"❌ Error reading file: {e}")
return ""
# Chunk text without breaking words
def chunk_text(text, chunk_size):
chunks = []
while text:
if len(text) <= chunk_size:
chunks.append(text)
break
split_idx = text.rfind(' ', 0, chunk_size)
if split_idx == -1:
split_idx = chunk_size
chunks.append(text[:split_idx])
text = text[split_idx:].lstrip()
return chunks
# Generate TTS audio for each chunk
async def generate_audio(chunks, rate):
os.makedirs(TEMP_DIR, exist_ok=True)
segments = []
for idx, chunk in enumerate(chunks):
mp3_file = os.path.join(TEMP_DIR, f"chunk_{idx}.mp3")
print(f"🎙️ Generating Chunk {idx+1}/{len(chunks)} at Speed: {rate}")
try:
if rate == "0%":
communicate = edge_tts.Communicate(text=chunk, voice=VOICE)
else:
communicate = edge_tts.Communicate(text=chunk, voice=VOICE, rate=rate)
await communicate.save(mp3_file)
segments.append(AudioSegment.from_file(mp3_file))
except Exception as e:
print(f"⚠️ Error generating chunk {idx}: {e}")
return segments
# Combine all audio chunks into final MP3
def combine_segments(segments, output_file):
if not segments:
print("❌ No audio segments to combine.")
return
final_audio = segments[0] if len(segments) == 1 else sum(segments[1:], segments[0])
final_audio.export(output_file, format="mp3")
print(f"✅ Final audio saved at: {output_file}")
# Delete temporary audio files
def cleanup_temp():
for f in os.listdir(TEMP_DIR):
os.remove(os.path.join(TEMP_DIR, f))
os.rmdir(TEMP_DIR)
print("🧹 Temp folder cleaned.")
# Main async function
async def main():
value = 7 # 🔄 Change value from 1 (slowest) to 7 (fastest)# *****
rate = map_speed_to_rate(value)
text = read_text(INPUT_FILE)
if not text:
print("❌ No text to process.")
return
chunks = chunk_text(text, CHUNK_SIZE)
if not chunks:
print("❌ No valid text chunks generated.")
return
segments = await generate_audio(chunks, rate)
combine_segments(segments, FINAL_MP3)
cleanup_temp()
# Run the async function
await main()
----------