You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

64 lines
1.5 KiB

#!/usr/bin/env python3
"""
TTS MP3 转 Opus 脚本
用于飞书语音消息支持
"""
import subprocess
import sys
import os
from pathlib import Path
def mp3_to_opus(mp3_path: str, output_path: str = None) -> str:
"""
将 MP3 文件转换为 Opus 格式
Args:
mp3_path: MP3 文件路径
output_path: 输出 Opus 文件路径(可选)
Returns:
生成的 Opus 文件路径
"""
if output_path is None:
output_path = str(Path(mp3_path).with_suffix('.opus'))
# 使用 ffmpeg 转换
cmd = [
'ffmpeg',
'-i', mp3_path,
'-c:a', 'libopus',
'-b:a', '64k',
'-vbr', 'on',
'-compression_level', '10',
'-y',
output_path
]
try:
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True
)
print(f"转换成功: {output_path}")
return output_path
except subprocess.CalledProcessError as e:
print(f"转换失败: {e.stderr}")
raise
if __name__ == "__main__":
if len(sys.argv) < 2:
print("使用方法: python tts_to_opus.py <mp3文件路径> [输出opus文件路径]")
sys.exit(1)
mp3_path = sys.argv[1]
output_path = sys.argv[2] if len(sys.argv) > 2 else None
try:
result = mp3_to_opus(mp3_path, output_path)
print(f"✓ 转换完成: {result}")
except Exception as e:
print(f"✗ 转换失败: {e}")
sys.exit(1)