download python code lib packages and run it colab, offline running;
first mina program run with
#to download offline
!pip download edge-tts pydub -d /content/drive/MyDrive/Lib/
offline program.
# Mount Google Drive to access your pre-downloaded wheels
from google.colab import drive
drive.mount('/content/drive')
# Install required wheel files (replace filenames as needed)
#!pip install /content/drive/MyDrive/Lib/edge_tts-1.4.0-py3-none-any.whl --quiet
#!pip install /content/drive/MyDrive/Lib/pydub-0.25.1-py2.py3-none-any.whl --quiet
!pip install /content/drive/MyDrive/Lib/*.whl
# Install ffmpeg from local .deb if downloaded OR fallback to internet-based apt
# (Optional) Uncomment below if you have ffmpeg.deb in Drive:
# !dpkg -i /content/drive/MyDrive/Lib/ffmpeg-release-i686-static.deb
# If not available locally, this will still need internet:
!apt install ffmpeg --quiet
# Now continue your original script (unchanged logic)
import os
import asyncio
import edge_tts
from pydub import AudioSegment
# Constants
INPUT_FILE = "/content/input.txt"
VOICE = "te-IN-ShrutiNeural"
CHUNK_SIZE = 4000
TEMP_DIR = "/content/temp_chunks"
FINAL_MP3 = "/content/output.mp3"
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%")
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 ""
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
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)
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
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}")
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.")
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()