#!/usr/bin/env python3
"""
生成前10句英文字幕的TTS音频（明亮、中性的女声）
"""
import sqlite3
import os
import asyncio
import edge_tts

# 数据库路径
db_path = "\\\\ll\\D\\xtrssvjj\\dpd.db"

# 输出目录
tts_dir = "shitu_tts"
if not os.path.exists(tts_dir):
    os.makedirs(tts_dir)

# 明亮、中性的女声
voice = "en-US-AriaNeural"  # 明亮、中性的女声

async def generate_tts(text, output_file, voice, rate="-30%"):
    """生成TTS音频，rate为语速调整，-30%表示0.7倍速"""
    communicate = edge_tts.Communicate(text, voice, rate=rate)
    await communicate.save(output_file)

async def main():
    # 查询前10句英文字幕
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()

        cursor.execute("SELECT idx, en FROM srts WHERE adid = 8281 ORDER BY idx ASC LIMIT 10")
        subtitles = cursor.fetchall()

        print(f"生成前 {len(subtitles)} 句英文TTS")

        # 生成TTS
        for idx, en in subtitles:
            output_file = os.path.join(tts_dir, f"tts_line_{idx:03d}.wav")
            
            print(f"  生成句{idx}: {en[:30]}...")
            
            try:
                await generate_tts(en, output_file, voice)
                print(f"  完成: {output_file}")
            except Exception as e:
                print(f"  错误: {e}")

        conn.close()
        print("\n所有TTS生成完成！")

    except Exception as e:
        print(f"错误: {e}")

if __name__ == "__main__":
    asyncio.run(main())
