71 lines
2.0 KiB
Python
Executable File
71 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
批量转换脚本 - 转换整个文件夹
|
|
"""
|
|
|
|
import argparse
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
from tts_convert import convert_file, VOICES
|
|
|
|
|
|
async def batch_convert(input_dir: str, output_dir: str, lang: str = 'auto'):
|
|
"""批量转换文件夹"""
|
|
input_path = Path(input_dir)
|
|
output_path = Path(output_dir)
|
|
|
|
if not input_path.exists():
|
|
print(f"❌ 输入目录不存在: {input_dir}")
|
|
return
|
|
|
|
# 创建输出目录
|
|
output_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 查找所有文本文件
|
|
text_files = list(input_path.glob('*.txt')) + list(input_path.glob('*.md'))
|
|
|
|
if not text_files:
|
|
print(f"⚠️ 未找到文本文件: {input_dir}")
|
|
return
|
|
|
|
print(f"📝 找到 {len(text_files)} 个文件")
|
|
print('=' * 60)
|
|
|
|
# 逐个转换
|
|
success = 0
|
|
failed = 0
|
|
|
|
for i, file in enumerate(text_files, 1):
|
|
output_file = output_path / file.with_suffix('.mp3').name
|
|
|
|
print(f"\n[{i}/{len(text_files)}] {file.name}")
|
|
|
|
try:
|
|
await convert_file(str(file), lang, str(output_file))
|
|
success += 1
|
|
except Exception as e:
|
|
print(f"❌ 失败: {e}")
|
|
failed += 1
|
|
|
|
print('\n' + '=' * 60)
|
|
print(f"✅ 完成: {success} 成功, {failed} 失败")
|
|
print(f"📁 输出目录: {output_path.absolute()}")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='批量 TTS 转换')
|
|
parser.add_argument('input_dir', help='输入文件夹')
|
|
parser.add_argument('--output', '-o', default='output', help='输出文件夹')
|
|
parser.add_argument('--lang', '-l', default='auto',
|
|
choices=['auto', 'zh', 'zh-hk', 'en', 'en-gb', 'jp', 'kr'],
|
|
help='语言代码')
|
|
|
|
args = parser.parse_args()
|
|
|
|
asyncio.run(batch_convert(args.input_dir, args.output, args.lang))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|