xiaowei-system/scripts/to_opus.py

33 lines
1011 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
mp3 → opus 转码脚本(飞书语音消息专用)
飞书可点播放的语音只支持 .ogg/.opusOgg Opus 容器),
text_to_speech 生成的 .mp3 只能发成文件附件。
用法: python3 to_opus.py input.mp3 [output.ogg]
"""
import sys, os, subprocess
def to_opus(src: str, dst: str = None) -> str:
if not dst:
dst = os.path.splitext(src)[0] + '.opus'
cmd = [
'ffmpeg', '-y', '-i', src,
'-c:a', 'libopus', '-b:a', '24k',
'-ac', '1',
dst
]
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode != 0:
raise RuntimeError(f'转码失败: {r.stderr[-300:]}')
return dst
if __name__ == '__main__':
if len(sys.argv) < 2:
print('用法: python3 to_opus.py input.mp3 [output.opus]')
sys.exit(1)
src = sys.argv[1]
dst = sys.argv[2] if len(sys.argv) > 2 else None
out = to_opus(src, dst)
print(f'✅ 转码成功: {out}')
print(f' MEDIA:{out}')