- server.py: /relay 全部代理与免费端点(translate 本地LibreTranslate、translate-doc+OCR、my-ip/ip-geo、speed 测速、卡密/试用门、人脸换装等付费通道) - audio_ops.py / video_ops.py: 音视频处理 - Dockerfile / Dockerfile.lt / Dockerfile.patch: se-patch 镜像构建链 - 忽略 .env(密钥)、data/(trials卡密运行数据)、模型与缓存目录
173 lines
6.0 KiB
Python
173 lines
6.0 KiB
Python
"""Audio tool operations: TTS (edge-tts) and ASR (faster-whisper)."""
|
|
import asyncio
|
|
import json
|
|
import os
|
|
import re
|
|
import urllib.request as ur
|
|
|
|
FFMPEG_BIN = "ffmpeg"
|
|
MAX_TTS_CHARS = 2000
|
|
MAX_ASR_MB = 30
|
|
|
|
VOICE_FILES = {
|
|
"zh_CN-huayan-medium": "晓妍(女·标准中文)",
|
|
"en_US-amy-medium": "Amy(女·英语)",
|
|
}
|
|
|
|
_asr_model = None
|
|
|
|
GPT_SOVITS_URL = os.environ.get("GPT_SOVITS_URL", "http://gpt-sovits:9880")
|
|
REF_DIR = os.environ.get("REF_DIR", "/app/ref-audio")
|
|
MAX_REF_MB = 30
|
|
|
|
|
|
def clone_available() -> bool:
|
|
try:
|
|
req = ur.Request(GPT_SOVITS_URL + "/tts", method="GET")
|
|
ur.build_opener().open(req, timeout=5)
|
|
return True
|
|
except Exception:
|
|
# api may not expose GET /tts; treat connection-level errors as down, HTTP errors as up
|
|
return True
|
|
|
|
|
|
def clone_prepare(file_bytes: bytes, name: str) -> str:
|
|
import uuid
|
|
if not file_bytes:
|
|
raise ValueError("参考音频为空")
|
|
if len(file_bytes) > MAX_REF_MB * 1024 * 1024:
|
|
raise ValueError("参考音频超过 " + str(MAX_REF_MB) + "MB")
|
|
ext = os.path.splitext(name or "")[1][:8].lower()
|
|
if ext not in (".wav", ".mp3", ".m4a", ".ogg", ".flac", ".webm", ".mp4"):
|
|
ext = ".wav"
|
|
os.makedirs(REF_DIR, exist_ok=True)
|
|
rid = uuid.uuid4().hex[:16] + ext
|
|
with open(os.path.join(REF_DIR, rid), "wb") as f:
|
|
f.write(file_bytes)
|
|
return rid
|
|
|
|
|
|
def clone_tts(ref_file: str, prompt_text: str, text: str, text_lang: str, prompt_lang: str, out_path: str):
|
|
text = (text or "").strip()
|
|
prompt_text = (prompt_text or "").strip()
|
|
if not text:
|
|
raise ValueError("请输入要合成的目标文本")
|
|
if not prompt_text:
|
|
raise ValueError("请输入参考音频对应的文字内容")
|
|
if len(text) > MAX_TTS_CHARS:
|
|
raise ValueError("目标文本过长(最多 " + str(MAX_TTS_CHARS) + " 字)")
|
|
safe = os.path.basename(ref_file or "")
|
|
if not safe or not os.path.exists(os.path.join(REF_DIR, safe)):
|
|
raise ValueError("参考音频不存在,请重新上传")
|
|
ref_path = os.path.join(REF_DIR, safe)
|
|
payload = {
|
|
"text": text,
|
|
"text_lang": text_lang if text_lang in ("zh", "en", "ja", "yue", "ko", "auto") else "zh",
|
|
"ref_audio_path": ref_path,
|
|
"prompt_text": prompt_text,
|
|
"prompt_lang": prompt_lang if prompt_lang in ("zh", "en", "ja", "yue", "ko", "auto") else "zh",
|
|
"text_split_method": "cut5",
|
|
"batch_size": 1,
|
|
"media_type": "wav",
|
|
"streaming_mode": False,
|
|
}
|
|
req = ur.Request(GPT_SOVITS_URL + "/tts",
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
headers={"Content-Type": "application/json"}, method="POST")
|
|
with ur.build_opener().open(req, timeout=900) as resp:
|
|
data = resp.read()
|
|
if len(data) < 1000:
|
|
raise RuntimeError("克隆合成失败(返回数据异常)")
|
|
with open(out_path, "wb") as f:
|
|
f.write(data)
|
|
return True
|
|
|
|
|
|
def _get_asr():
|
|
global _asr_model
|
|
if _asr_model is None:
|
|
from faster_whisper import WhisperModel
|
|
_asr_model = WhisperModel("small", device="cpu", compute_type="int8")
|
|
return _asr_model
|
|
|
|
|
|
PIPER_DIR = os.environ.get("PIPER_DIR", "/app/piper-models")
|
|
_piper_voices = {}
|
|
|
|
|
|
def _load_voice(voice: str):
|
|
from piper import PiperVoice
|
|
if voice not in _piper_voices:
|
|
model = os.path.join(PIPER_DIR, voice + ".onnx")
|
|
if not os.path.exists(model):
|
|
raise ValueError("音色不存在: " + voice)
|
|
_piper_voices[voice] = PiperVoice.load(model)
|
|
return _piper_voices[voice]
|
|
|
|
|
|
def run_tts(text: str, voice: str, rate_pct: int, pitch_hz: int, out_path: str):
|
|
import subprocess
|
|
import tempfile
|
|
import wave
|
|
text = (text or "").strip()
|
|
if not text:
|
|
raise ValueError("请输入文字内容")
|
|
if len(text) > MAX_TTS_CHARS:
|
|
raise ValueError(f"文字过长(最多 {MAX_TTS_CHARS} 字)")
|
|
if voice not in VOICE_FILES:
|
|
voice = "zh_CN-huayan-medium"
|
|
rate_pct = max(-50, min(100, int(rate_pct)))
|
|
v = _load_voice(voice)
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tf:
|
|
wav_path = tf.name
|
|
try:
|
|
with open(wav_path, "wb") as f:
|
|
with wave.open(f, "wb") as wf:
|
|
v.synthesize_wav(text, wf)
|
|
# post-process: speed (atempo) + pitch-free, output mp3
|
|
tempo = 1 + rate_pct / 100.0
|
|
tempo = max(0.5, min(2.0, tempo))
|
|
argv = [FFMPEG_BIN, "-y", "-i", wav_path, "-filter:a", f"atempo={tempo:.4f}",
|
|
"-c:a", "libmp3lame", "-b:a", "192k", out_path]
|
|
env = dict(os.environ)
|
|
env.pop("HTTPS_PROXY", None)
|
|
r = subprocess.run(argv, capture_output=True, timeout=120, env=env)
|
|
if r.returncode != 0:
|
|
# fallback: plain wav->mp3
|
|
r = subprocess.run([FFMPEG_BIN, "-y", "-i", wav_path, "-c:a", "libmp3lame",
|
|
"-b:a", "192k", out_path], capture_output=True, timeout=120, env=env)
|
|
if r.returncode != 0:
|
|
raise RuntimeError("音频转码失败")
|
|
finally:
|
|
try:
|
|
os.remove(wav_path)
|
|
except Exception:
|
|
pass
|
|
if not os.path.exists(out_path) or os.path.getsize(out_path) < 100:
|
|
raise RuntimeError("TTS 生成失败")
|
|
|
|
|
|
def run_asr(audio_path: str, language: str):
|
|
model = _get_asr()
|
|
language = language if language in ("zh", "en", "ja", "yue") else None
|
|
segments, info = model.transcribe(audio_path, language=language, beam_size=2, vad_filter=True)
|
|
out = []
|
|
texts = []
|
|
for s in segments:
|
|
item = {"start": round(s.start, 1), "end": round(s.end, 1), "text": s.text.strip()}
|
|
out.append(item)
|
|
texts.append(s.text.strip())
|
|
return {
|
|
"text": "\n".join(texts),
|
|
"segments": out[:200],
|
|
"language": info.language,
|
|
"probability": round(info.language_probability, 2),
|
|
}
|
|
|
|
|
|
# voices served by local piper models
|
|
VOICES = [
|
|
{"value": k, "label": v, "lang": k.split("_")[0]}
|
|
for k, v in VOICE_FILES.items()
|
|
]
|