draft : code for text to mp3 convertion.
code for text to mp3 convertion.
!pip install edge-tts pydub --quiet
!apt install ffmpeg --quiet
import os
import asyncio
import edge_tts
from pydub import AudioSegment
# Constants
INPUT_FILE = "/content/input.txt"
VOICE = "te-IN-ShrutiNeural" # Telugu Female voice
CHUNK_SIZE = 4000
TEMP_DIR = "/content/temp_chunks"
FINAL_MP3 = "/content/output.mp3"
# Speed mapping: 1 (slow) to 7 (fast)
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 input text and clean it
def read_text(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return " ".join(f.read().split()) # single-line, cleaned
except Exception as e:
print(f"❌ Error reading file: {e}")
return ""
# Break large text into chunks 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 audio per 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:
communicate = edge_tts.Communicate(text=chunk, voice=VOICE, rate=rate) # ✅ Correct usage
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
def combine_segments(segments, output_file):
if not segments:
print("❌ No audio segments to combine.")
return
final_audio = sum(segments[1:], segments[0])
final_audio.export(output_file, format="mp3")
print(f"✅ Final audio saved at: {output_file}")
# Cleanup temporary 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 def main():
text = read_text(INPUT_FILE)
if not text:
print("❌ No text to process.")
return
chunks = chunk_text(text, CHUNK_SIZE)
speed_input = 5 # 1=slow, 3=normal, 7=fast
rate = map_speed_to_rate(speed_input)
segments = await generate_audio(chunks, rate)
combine_segments(segments, FINAL_MP3)
cleanup_temp()
await main()
-------------
you want to estimate based on characters and KB size per page, so that you can easily calculate and price when customers give files!
Let's calculate carefully:
📄 How much text does an average page have?
Type of Document | Approx. Characters per Page | Approx. KB per Page (only text) |
---|---|---|
Normal A4 page (12pt font, single-spaced) | 2,500 to 3,000 characters | 2 KB – 3 KB |
A4 page (double-spaced) | ~1,500 characters | ~1.5 KB |
Small story page (font large) | ~1,000 characters | ~1 KB |
✅ In your case (normal Telugu text files, paragraph writing),
➡️ 1 page ≈ 2,500–3,000 characters
➡️ 1 page ≈ 2–3 KB (file size)
🔥 Practical Example
Suppose a customer sends you a 10-page text file:
-
Estimated total characters:
→ 10 pages × 2,500 chars = 25,000 characters -
Estimated text file size:
→ 10 pages × 2.5 KB = 25 KB file size
➡️ Then you know how to charge: it falls into your ₹200 slab (5–20 pages)!
📚 Table for quick calculation
Pages | Approx. Characters | Approx. File Size (KB) | Your Suggested Price |
---|---|---|---|
1 page | 2,500–3,000 chars | 2–3 KB | ₹50 |
5 pages | 12,500–15,000 chars | 12–15 KB | ₹50 |
10 pages | 25,000–30,000 chars | 25–30 KB | ₹200 |
20 pages | 50,000–60,000 chars | 50–60 KB | ₹200–₹250 |
50 pages (small book) | 125,000–150,000 chars | 125–150 KB | ₹500–₹800 |
100+ pages (full book) | 250,000+ chars | 250+ KB | ₹1000–₹1500 |