把 pydub 探针绑到当前解释器同目录的完整版 ffmpeg (#3444)

edge_tts 升 7.3.1;edge.py 撤 WSS_URL patch 改默认
(4.36 跑默认 URL 稳定)。util.py 加 PATH 临时注入,
绕开 Mac 上 Trae CN 注入的精简版 ffmpeg/ffprobe,
让 pydub 拿 conda env 完整版。引用计数加锁防多线
程串。
This commit is contained in:
hrz 2026-10-15 15:43:39 +08:00 committed by GitHub
parent 27399a2fb6
commit eb9dc8ca0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 69 additions and 31 deletions

View file

@ -1,24 +1,9 @@
import os
import uuid
import edge_tts
import edge_tts.communicate # 显式 import,下方要给本模块的 WSS_URL 重新赋值
from datetime import datetime
from core.providers.tts.base import TTSProviderBase
# 微软 2026-10 起对 legacy 消费者接口返回 403,edge-tts 7.2.8 仍走
# speech.platform.bing.com 走不通。上游 PR rany2/edge-tts#491 切到
# api.msedgeservices.com 修好,但 7.2.8 仍是 PyPI 最新版本,临时把 WSS_URL 顶住。
# TODO(7.2.9): 升 edge-tts ≥ 7.2.9 后删掉这段 monkey patch。
_NEW_WSS_URL = (
"wss://api.msedgeservices.com/tts/cognitiveservices/websocket/v1"
f"?Ocp-Apim-Subscription-Key={edge_tts.constants.TRUSTED_CLIENT_TOKEN}"
)
# communicate.py 第 35 行 `from .constants import WSS_URL` 会在 import 时把
# WSS_URL 复制成本模块局部名,只改 edge_tts.constants.WSS_URL 不会生效。
# __stream() 走 LEGB 找的是 edge_tts.communicate.WSS_URL,所以这里两个都要改。
edge_tts.constants.WSS_URL = _NEW_WSS_URL
edge_tts.communicate.WSS_URL = _NEW_WSS_URL
class TTSProvider(TTSProviderBase):
TTS_PARAM_CONFIG = [
@ -86,4 +71,4 @@ class TTSProvider(TTSProviderBase):
return audio_bytes
except Exception as e:
error_msg = f"Edge TTS请求失败: {e}"
raise Exception(error_msg) # 抛出异常,让调用方捕获
raise Exception(error_msg) # 抛出异常,让调用方捕获

View file

@ -4,12 +4,15 @@ import json
import copy
import wave
import socket
import sys
import asyncio
import threading
import requests
import subprocess
import numpy as np
import opuslib_next
from io import BytesIO
from contextlib import contextmanager
from core.utils import p3
from pydub import AudioSegment
from typing import Callable, Any
@ -17,6 +20,51 @@ from typing import Callable, Any
TAG = __name__
# 绕开 IDE(Trae CN 等)注入的精简版 ffmpeg/ffprobe PATH 占位:调用期间把
# Python 解释器同目录的 bin 加到 PATH 前缀,pydub.utils.which() 会优先找到
# conda env 自带的完整版,调用结束恢复原 PATH,不污染 import 后的全局。
# 环境变量 XIAOZHI_PYDUB_OVERRIDE_FFMPEG=0 时关闭。
#
# 引用计数 + 锁:多线程并发进出 with 块时,只在第一个进入时记下原始 PATH
# 并改写,ref_count 归零时才恢复。这样 finally 不会因多次保存/恢复错位
# 导致 bin 前缀永久残留,也避免 try/finally 契约被多 thread 互相覆盖。
_ffmpeg_path_lock = threading.Lock()
_ffmpeg_ref_count = 0
_ffmpeg_original_path: str | None = None
@contextmanager
def _ffmpeg_path_override():
if os.environ.get("XIAOZHI_PYDUB_OVERRIDE_FFMPEG", "1") != "1":
yield
return
bin_dir = os.path.dirname(sys.executable)
ffprobe = os.path.join(bin_dir, "ffprobe")
ffmpeg = os.path.join(bin_dir, "ffmpeg")
if not (os.path.isfile(ffprobe) or os.path.isfile(ffmpeg)):
yield
return
global _ffmpeg_ref_count, _ffmpeg_original_path
with _ffmpeg_path_lock:
if _ffmpeg_ref_count == 0:
_ffmpeg_original_path = os.environ.get("PATH", "")
os.environ["PATH"] = bin_dir + os.pathsep + _ffmpeg_original_path
_ffmpeg_ref_count += 1
try:
yield
finally:
with _ffmpeg_path_lock:
_ffmpeg_ref_count -= 1
if _ffmpeg_ref_count == 0 and _ffmpeg_original_path is not None:
os.environ["PATH"] = _ffmpeg_original_path
_ffmpeg_original_path = None
# 委托给 util.ffmpeg_path_override,import util 后调用方一行就能用
ffmpeg_path_override = _ffmpeg_path_override
def get_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@ -229,21 +277,24 @@ def extract_json_from_string(input_string):
def audio_to_data_stream(
audio_file_path, is_opus=True, callback: Callable[[Any], Any] = None, sample_rate=16000, opus_encoder=None
) -> None:
# 获取文件后缀名
with _ffmpeg_path_override():
_audio_to_data_stream_inner(
audio_file_path, is_opus, callback, sample_rate, opus_encoder
)
def _audio_to_data_stream_inner(
audio_file_path, is_opus, callback, sample_rate, opus_encoder
):
file_type = os.path.splitext(audio_file_path)[1]
if file_type:
file_type = file_type.lstrip(".")
# 读取音频文件,-nostdin 参数:不要从标准输入读取数据,否则FFmpeg会阻塞
# -nostdin:避免 ffmpeg 阻塞等 stdin
audio = AudioSegment.from_file(
audio_file_path, format=file_type, parameters=["-nostdin"]
)
# 转换为单声道/指定采样率/16位小端编码(确保与编码器匹配)
audio = audio.set_channels(1).set_frame_rate(sample_rate).set_sample_width(2)
# 获取原始PCM数据(16位小端)
raw_data = audio.raw_data
pcm_to_data_stream(raw_data, is_opus, callback, sample_rate, opus_encoder)
pcm_to_data_stream(audio.raw_data, is_opus, callback, sample_rate, opus_encoder)
async def audio_to_data(
@ -274,9 +325,10 @@ async def audio_to_data(
if file_type:
file_type = file_type.lstrip(".")
# 读取音频文件,-nostdin 参数:不要从标准输入读取数据,否则FFmpeg会阻塞
audio = AudioSegment.from_file(
audio_file_path, format=file_type, parameters=["-nostdin"]
)
with _ffmpeg_path_override():
audio = AudioSegment.from_file(
audio_file_path, format=file_type, parameters=["-nostdin"]
)
# 转换为单声道/16kHz采样率/16位小端编码(确保与编码器匹配)
audio = audio.set_channels(1).set_frame_rate(16000).set_sample_width(2)
@ -335,9 +387,10 @@ def audio_bytes_to_data_stream(
return p3.decode_opus_from_bytes_stream(audio_bytes, callback)
else:
# 其他格式用pydub
audio = AudioSegment.from_file(
BytesIO(audio_bytes), format=file_type, parameters=["-nostdin"]
)
with _ffmpeg_path_override():
audio = AudioSegment.from_file(
BytesIO(audio_bytes), format=file_type, parameters=["-nostdin"]
)
audio = audio.set_channels(1).set_frame_rate(sample_rate).set_sample_width(2)
raw_data = audio.raw_data
pcm_to_data_stream(raw_data, is_opus, callback, sample_rate, opus_encoder)

View file

@ -13,7 +13,7 @@ pydub==0.25.1
funasr==1.2.7
openai==2.8.1
google-generativeai==0.8.5
edge_tts==7.2.8
edge_tts==7.3.1
httpx==0.28.1
aiohttp==3.13.2
aiohttp_cors==0.8.1