chore: 首次纳管 IT-TOOLS-PRO relay 服务源码

- 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卡密运行数据)、模型与缓存目录
This commit is contained in:
personal827 committed 2026-11-01 02:21:52 +08:00
commit 885d7b3686
7 files changed
+4170

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
.env
data/
__pycache__/
*.pyc
hf-cache/
piper-models/
ai-models/
waifu2x/
*.log
.venv-hf/
+19
View File
@@ -0,0 +1,19 @@
FROM python:3.10-slim-bookworm
# calibre provides ebook-convert: docx/pdf/txt/rtf/odt/epub/mobi/azw3/html interop
RUN apt-get update \
&& apt-get install -y --no-install-recommends calibre ffmpeg fonts-wqy-zenhei \
&& rm -rf /var/lib/apt/lists/*
# audio tools: Piper (local neural TTS) + faster-whisper (local ASR)
# models are pre-downloaded on the host (build env has no internet)
RUN pip install --no-cache-dir piper-tts faster-whisper
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple "rembg[cpu]"
COPY hf-cache /root/.cache/huggingface
COPY piper-models /app/piper-models
# AI 图片: u2net 抠图模型(预下载) + waifu2x-ncnn-vulkan 无损放大(CPU)
COPY ai-models/u2net.onnx /root/.u2net/u2net.onnx
COPY waifu2x/waifu2x-ncnn-vulkan-20220728-ubuntu /app/waifu2x
WORKDIR /app
COPY server.py video_ops.py audio_ops.py .
ENV HF_HOME=/root/.cache/huggingface
EXPOSE 8091
CMD ["python", "server.py"]
+2
View File
@@ -0,0 +1,2 @@
FROM it-tools-relay:se-patch
COPY server.py /app/server.py
+2
View File
@@ -0,0 +1,2 @@
FROM it-tools-relay:latest
COPY server.py /app/server.py
+172
View File
@@ -0,0 +1,172 @@
"""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()
]
+3770
View File
File diff suppressed because it is too large. Load diff
+195
View File
@@ -0,0 +1,195 @@
"""Video tool operations: each op builds an ffmpeg argv given (in_path, out_path, params)."""
import os
import re
import subprocess
FFMPEG = "ffmpeg"
MAX_INPUT = 2 * 1024 * 1024 * 1024 # 2GB
ALLOWED_EXT = {
".mp4", ".webm", ".mkv", ".mov", ".avi", ".flv", ".wmv", ".ts", ".m4v",
".gif", ".mp3", ".wav", ".aac", ".ogg", ".flac", ".m4a", ".wma", ".weba",
}
AUDIO_EXT = {"mp3": ".mp3", "wav": ".wav", "aac": ".m4a", "ogg": ".ogg"}
def _num(v, lo, hi, default):
try:
x = float(v)
except Exception:
return default
return max(lo, min(hi, x))
def _int(v, lo, hi, default):
return int(_num(v, lo, hi, default))
def build_cmd(op: str, fin: str, fout: str, p: dict):
"""returns (argv, out_name_hint) or raises ValueError"""
if op == "convert":
fmt = p.get("fmt", "mp4")
if fmt == "mp4":
return [FFMPEG, "-y", "-i", fin, "-c:v", "libx264", "-preset", "fast", "-crf", "21",
"-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", fout], "mp4"
if fmt == "webm":
return [FFMPEG, "-y", "-i", fin, "-c:v", "libvpx-vp9", "-b:v", "0", "-crf", "34",
"-cpu-used", "4", "-c:a", "libopus", "-b:a", "96k", fout], "webm"
if fmt == "mkv":
return [FFMPEG, "-y", "-i", fin, "-c:v", "libx264", "-preset", "fast", "-crf", "21",
"-c:a", "aac", fout], "mkv"
if fmt == "mov":
return [FFMPEG, "-y", "-i", fin, "-c:v", "libx264", "-preset", "fast", "-crf", "21",
"-c:a", "aac", fout], "mov"
if fmt == "avi":
return [FFMPEG, "-y", "-i", fin, "-c:v", "libx264", "-preset", "fast", "-crf", "21",
"-c:a", "libmp3lame", "-b:a", "128k", fout], "avi"
raise ValueError("不支持的格式")
if op == "gif":
w = _int(p.get("width"), 160, 1920, 480)
fps = _int(p.get("fps"), 5, 30, 12)
return [FFMPEG, "-y", "-i", fin, "-vf", f"fps={fps},scale={w}:-2:flags=lanczos",
"-loop", "0", fout], "gif"
if op == "gif2video":
return [FFMPEG, "-y", "-i", fin, "-movflags", "+faststart", "-pix_fmt", "yuv420p",
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2", fout], "mp4"
if op == "compress":
crf = _int(p.get("crf"), 18, 45, 30)
scale = _int(p.get("scale"), 0, 2160, 0)
vf = f"scale=-2:{scale}" if scale > 0 else "null"
return [FFMPEG, "-y", "-i", fin, "-c:v", "libx264", "-preset", "fast", "-crf", str(crf),
"-vf", vf, "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", fout], "mp4"
if op == "trim":
start = max(0.0, _num(p.get("start"), 0, 86400, 0))
dur = max(0.1, _num(p.get("duration"), 0.1, 86400, 10))
return [FFMPEG, "-y", "-ss", f"{start:.3f}", "-i", fin, "-t", f"{dur:.3f}",
"-c:v", "libx264", "-preset", "fast", "-crf", "21", "-c:a", "aac",
"-movflags", "+faststart", fout], "mp4"
if op == "rotate":
deg = p.get("deg", "90")
tf = {"90": "transpose=1", "180": "transpose=1,transpose=1", "270": "transpose=2"}.get(str(deg))
if not tf:
raise ValueError("角度无效")
return [FFMPEG, "-y", "-i", fin, "-vf", tf, "-c:v", "libx264", "-preset", "fast",
"-crf", "21", "-c:a", "copy", "-movflags", "+faststart", fout], "mp4"
if op == "flip":
mode = p.get("mode", "h")
tf = "hflip" if mode == "h" else "vflip"
return [FFMPEG, "-y", "-i", fin, "-vf", tf, "-c:v", "libx264", "-preset", "fast",
"-crf", "21", "-c:a", "copy", "-movflags", "+faststart", fout], "mp4"
if op == "speed":
factor = _num(p.get("factor"), 0.25, 4, 1)
# video: setpts, audio: atempo chain (atempo limited to [0.5, 2] per node)
chain = []
f = factor
while f > 2.0:
chain.append("atempo=2.0")
f /= 2.0
while f < 0.5:
chain.append("atempo=0.5")
f /= 0.5
chain.append(f"atempo={f:.4f}")
af = ",".join(chain)
return [FFMPEG, "-y", "-i", fin, "-filter:v", f"setpts={1/factor:.6f}*PTS",
"-filter:a", af, "-c:v", "libx264", "-preset", "fast", "-crf", "21",
"-movflags", "+faststart", fout], "mp4"
if op == "mute":
return [FFMPEG, "-y", "-i", fin, "-an", "-c:v", "copy", "-movflags", "+faststart", fout], "mp4"
if op == "audio":
fmt = p.get("fmt", "mp3")
ext = AUDIO_EXT.get(fmt)
if not ext:
raise ValueError("不支持的音频格式")
if fmt == "mp3":
ac = ["-c:a", "libmp3lame", "-b:a", "192k"]
elif fmt == "wav":
ac = ["-c:a", "pcm_s16le"]
elif fmt == "aac":
ac = ["-c:a", "aac", "-b:a", "192k"]
else: # ogg
ac = ["-c:a", "libvorbis", "-q:a", "5"]
return [FFMPEG, "-y", "-i", fin, "-vn"] + ac + [fout], ext.lstrip(".")
if op == "watermark":
# text watermark via drawtext
text = str(p.get("text") or "").strip()
if not text:
raise ValueError("请输入水印文字")
text = text.replace("'", "").replace(":", ":").replace("%", "")
pos = p.get("position", "br")
fs_size = _int(p.get("fontsize"), 12, 200, 36)
color = p.get("color", "white")
if color not in ("white", "black", "red", "yellow", "cyan", "gray"):
color = "white"
opacity = max(10, min(100, _int(p.get("opacity"), 10, 100, 70))) / 100.0
margin = _int(p.get("margin"), 0, 300, 24)
if pos == "tl":
xy = f"x={margin}:y={margin}"
elif pos == "tr":
xy = f"x=w-tw-{margin}:y={margin}"
elif pos == "bl":
xy = f"x={margin}:y=h-th-{margin}"
elif pos == "br":
xy = f"x=w-tw-{margin}:y=h-th-{margin}"
else: # center
xy = "x=(w-tw)/2:y=(h-th)/2"
font = "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc"
vf = (f"drawtext=fontfile={font}:text={text}:fontsize={fs_size}"
f":fontcolor={color}@{opacity:.2f}:{xy}")
return [FFMPEG, "-y", "-i", fin, "-vf", vf, "-c:v", "libx264", "-preset", "fast",
"-crf", "20", "-c:a", "copy", "-movflags", "+faststart", fout], "mp4"
if op == "unwatermark":
# remove watermark region: blur overlay (any background) or delogo fill
x = _int(p.get("x"), 0, 7680, 0)
y = _int(p.get("y"), 0, 4320, 0)
w = _int(p.get("w"), 8, 7680, 100)
h = _int(p.get("h"), 8, 4320, 100)
sigma = _int(p.get("sigma"), 5, 100, 30)
fc = (f"[0:v]crop={w}:{h}:{x}:{y},gblur=sigma={sigma}[ck];"
f"[0:v][ck]overlay={x}:{y}")
return [FFMPEG, "-y", "-i", fin, "-filter_complex", fc, "-c:v", "libx264",
"-preset", "fast", "-crf", "20", "-c:a", "copy",
"-movflags", "+faststart", fout], "mp4"
if op == "screenshot":
t = max(0.0, _num(p.get("time"), 0, 86400, 0))
return [FFMPEG, "-y", "-ss", f"{t:.3f}", "-i", fin, "-frames:v", "1", fout], "png"
if op == "thumbnail":
# extract 3 evenly spaced frames -> handled by caller running 3 cmds? keep single-frame
t = max(0.0, _num(p.get("time"), 0, 86400, 0))
return [FFMPEG, "-y", "-ss", f"{t:.3f}", "-i", fin, "-frames:v", "1",
"-vf", "scale=640:-2", fout], "png"
raise ValueError("未知操作")
def probe_duration(fin: str) -> float:
try:
r = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", fin],
capture_output=True, text=True, timeout=30,
)
return float(r.stdout.strip() or 0)
except Exception:
return 0
def run_ffmpeg(argv, timeout=900):
env = dict(os.environ)
env.pop("HTTPS_PROXY", None)
env.pop("HTTP_PROXY", None)
r = subprocess.run(argv, capture_output=True, timeout=timeout, env=env)
if r.returncode != 0:
err = r.stderr.decode("utf-8", "ignore")[-500:]
raise RuntimeError("ffmpeg 处理失败: " + err)
return True