import os
import subprocess
# Input folder
TEXT_FOLDER = "text_folder"
OUTPUT_FOLDER = "output"
# Voice to use
VOICE = "en-US-GuyNeural"
# Ensure main output folder exists
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Counter for subfolders
folder_index = 1
# Process all .txt files in the text folder
for file in sorted(os.listdir(TEXT_FOLDER)):
if not file.lower().endswith(".txt"):
continue
base_name = os.path.splitext(file)[0]
input_path = os.path.join(TEXT_FOLDER, file)
# Read the text content
with open(input_path, "r", encoding="utf-8") as f:
text = f.read().strip()
if not text:
print(f"⚠️ Skipping empty file: {file}")
continue
print(f"🔊 Generating TTS and SRT for: {file}")
# Create a new subfolder like output/folder1, folder2, ...
subfolder = os.path.join(OUTPUT_FOLDER, f"folder{folder_index}")
os.makedirs(subfolder, exist_ok=True)
tts_path = os.path.join(subfolder, base_name + ".mp3")
srt_path = os.path.join(subfolder, base_name + ".srt")
# Edge TTS CLI command
temp_text_path = os.path.join(subfolder, "temp.txt")
with open(temp_text_path, "w", encoding="utf-8") as tf:
tf.write(text)
command = [
"edge-tts",
"--file", temp_text_path,
"--voice", VOICE, "--rate=+5%",
"--write-media", tts_path,
"--write-subtitles", srt_path
]
try:
subprocess.run(command, check=True)
print(f"✅ Saved to: {subfolder}")
except subprocess.CalledProcessError as e:
print(f"❌ Error with {file}: {e}")
folder_index += 1
print("🎉 All done!")