Files
unicut/mcp/server.py
T
3970260761 f2ac228849 素材面板菜单新增「下载更多素材」直达入口;收口 unicut-mcp 服务等未提交改动
- 上传素材旁下拉菜单新增第三项「下载更多素材」,新窗口打开素材下载文档站 doc.88531.cn/docs/mrfx/mrfx-1g99qum57n5qk(zhCN/enUS 文案 my_download_more,Download 图标)
- sourceImport:showDirectoryPicker 不可用时降级,非 FS 浏览器可直接进编辑器
- 收口今日未提交改动:unicut-mcp 服务与 nginx/compose 反代、README/CHANGELOG 更新日志
2026-11-11 19:45:54 +08:00

839 lines
30 KiB
Python
Raw 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
# -*- coding: utf-8 -*-
"""
米剪(Unicut)MCP 服务
======================
让 AI 客户端(Claude Desktop / Claude Code / Cursor / Hermes 等)以 MCP 工具的形式
调用米剪的创作能力:
- grab_url 抓取网页正文与配图(公众号文章等,复用前端 extractFromHtml 规则)
- generate_script 大模型生成短视频口播文案
- tts / list_voices edge-tts 配音合成(unicut-tts)
- create_video_project 组装米剪工程 ZIP(project.json + materials/),在米剪「工程文件」一键导入
- list_projects / read_project 工作文件夹(文件夹模式)下的工程读写
- service_status 各依赖服务可用性自检
传输:Streamable HTTP,端点 /mcp(容器 8765,nginx 同源反代 https://<站点>/mcp)。
工程格式:与米剪 projectExport.ts 导出的 ZIP 完全同构——
project.json MovieData(title/poster/width/height/background/transitions/
captions/elements/resouces 九键齐全,过 checkNdata 门禁)
materials/{type}/{文件名} 素材相对路径
"""
from __future__ import annotations
import io
import ipaddress
import json
import os
import re
import socket
import time
import unicodedata
import uuid
import zipfile
from pathlib import Path
from urllib.parse import urljoin, urlparse
import httpx
from bs4 import BeautifulSoup
from mutagen.mp3 import MP3
from PIL import Image
from mcp.server.fastmcp import FastMCP
# ---------------------------------------------------------------- 环境配置
UNICUT_URL = os.getenv("UNICUT_URL", "http://unicut") # nginx 80(回环到 443)
TTS_URL = os.getenv("TTS_URL", "http://unicut-tts:8000")
LLM_BASE = os.getenv("MCP_LLM_BASE", "http://192.168.68.19:3099/v1").rstrip("/")
LLM_KEY = os.getenv("MCP_LLM_KEY", "")
LLM_MODEL = os.getenv("MCP_LLM_MODEL", "mimo-v2.6-flash")
PUBLIC_URL = os.getenv("MCP_PUBLIC_URL", "https://127.0.0.1:3005").rstrip("/")
PROJECTS_DIR = os.getenv("MCP_PROJECTS_DIR", "") # 文件夹模式工作目录(可空)
OUT_DIR = Path(os.getenv("MCP_OUT_DIR", "/data/out"))
IMG_MIN_W, IMG_MIN_H = 160, 120 # 与前端 sourceImport.ts 常量一致
IMG_MAX_BYTES = 10 * 1024 * 1024
HTTP_HEADERS = {
# 无 Referer:公众号图床 mmbiz.qpic.cn 带异源 Referer 时只给降级缩略图(实测)
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
),
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9",
}
mcp = FastMCP(
"unicut",
instructions=(
"米剪(Unicut)在线视频剪辑器的 MCP 服务。"
"典型工作流:grab_url 抓文章 → generate_script 写口播 → tts 配音 → "
"create_video_project 生成米剪工程 ZIP → 用户在米剪「工程文件」导入即可出片。"
),
host="0.0.0.0",
port=8765,
)
def _client(timeout: float = 30.0) -> httpx.AsyncClient:
# trust_env=False:不读宿主代理变量,容器网络行为可预期
return httpx.AsyncClient(timeout=timeout, follow_redirects=True, trust_env=False)
# ---------------------------------------------------------------- 安全与小工具
def assert_public_url(url: str) -> str:
"""拒绝非 http(s) 与内网/回环目标(与 nginx /grab/ 的断言同规则,防 SSRF 打内网)"""
p = urlparse(url)
if p.scheme not in ("http", "https"):
raise ValueError(f"只支持 http/https URL:{url}")
host = p.hostname
if not host:
raise ValueError(f"URL 缺少主机名:{url}")
if host == "localhost" or host.endswith(".local"):
raise ValueError(f"拒绝内网/回环目标:{host}")
try:
infos = socket.getaddrinfo(host, None, proto=socket.IPPROTO_TCP)
except socket.gaierror as e:
raise ValueError(f"域名解析失败:{host}({e})")
for info in infos:
ip = ipaddress.ip_address(info[4][0])
if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved or ip.is_multicast:
raise ValueError(f"拒绝内网地址目标:{host} → {ip}")
return str(p)
def _uid() -> str:
return uuid.uuid4().hex[:10]
def _slug(name: str) -> str:
"""文件名安全化:保留中英文数字,替换掉路径/控制字符"""
s = unicodedata.normalize("NFKC", name or "project")
s = re.sub(r'[\\/:*?"<>|\r\n\t]', "-", s).strip(" .")
return (s or "project")[:60]
def _ext_for(content_type: str, url: str, default: str = "bin") -> str:
ct = (content_type or "").split(";")[0].strip().lower()
table = {
"image/jpeg": "jpg", "image/jpg": "jpg", "image/png": "png",
"image/webp": "webp", "image/gif": "gif", "audio/mpeg": "mp3",
"audio/mp3": "mp3", "audio/wav": "wav", "audio/x-wav": "wav",
"audio/mp4": "m4a", "audio/aac": "aac",
}
if ct in table:
return table[ct]
suffix = Path(urlparse(url).path).suffix.lstrip(".").lower()
return suffix or default
def _is_local_path(s: str) -> bool:
return s.startswith("/") and not s.startswith("//")
# ---------------------------------------------------------------- 抓取(对齐前端 extractFromHtml)
_BLOCK_TAGS = [
"p", "h1", "h2", "h3", "h4", "h5", "h6", "li", "blockquote", "pre",
"td", "th", "figcaption", "tr", "div", "br",
]
_NOISE_TAGS = [
"script", "style", "noscript", "svg", "iframe", "canvas", "form",
"nav", "footer", "header", "aside", "button",
]
def extract_from_html(html: str, base_url: str) -> dict:
"""标题 / 正文 / 配图(相对转绝对、懒加载属性、去重)——与前端规则一致"""
soup = BeautifulSoup(html, "html.parser")
og = soup.find("meta", attrs={"property": "og:title"})
title = ((og.get("content") if og else None) or soup.title.get_text() if soup.title else "").strip()
for el in soup.find_all(_NOISE_TAGS):
el.decompose()
root = (
soup.find(id="js_content") # 公众号正文(微信页无 article/main)
or soup.find("article")
or soup.find("main")
or soup.find(attrs={"role": "main"})
or soup.body
or soup
)
# 图片:og:image 优先 + img 常见懒加载属性
seen: set[str] = set()
images: list[str] = []
def push(v: str | None) -> None:
if not v:
return
s = v.strip()
if not s or s.startswith("data:") or s.startswith("blob:"):
return
if re.search(r"\.svg(\?|#|$)", s, re.I):
return
try:
absu = urljoin(base_url, s)
except Exception:
return
if not re.match(r"^https?:", absu, re.I) or absu in seen:
return
seen.add(absu)
images.append(absu)
og_img = soup.find("meta", attrs={"property": "og:image"})
push(og_img.get("content") if og_img else None)
for img in root.find_all("img"):
for attr in ("src", "data-src", "data-original", "data-lazy-src", "data-actualsrc"):
push(img.get(attr))
# 正文:块级边界补换行 → textContent → 压空白(照抄前端清洗链)
for el in root.find_all(_BLOCK_TAGS):
el.insert(0, "\n")
el.append("\n")
text = (
root.get_text()
.replace("\r", "")
.replace("\t", " ")
.replace(" ", " ")
)
lines = [seg.strip() for seg in text.split("\n")]
kept: list[str] = []
for i, seg in enumerate(lines):
if seg or (i > 0 and lines[i - 1]):
kept.append(seg)
text = re.sub(r"\n{3,}", "\n\n", "\n".join(kept)).strip()[:12000]
return {"title": title, "text": text, "images": images[:20]}
async def fetch_bytes(url: str, *, allow_local_file: bool = True, timeout: float = 25.0) -> tuple[bytes, str]:
"""下载字节。url 可为公网 URL,或(默认允许)服务器本地文件路径。"""
if _is_local_path(url):
if not allow_local_file:
raise ValueError("此上下文不允许本地路径")
p = Path(url)
if not p.is_file():
raise ValueError(f"本地文件不存在:{url}")
return p.read_bytes(), ""
assert_public_url(url)
async with _client(timeout) as c:
r = await c.get(url, headers=HTTP_HEADERS)
r.raise_for_status()
return r.content, r.headers.get("content-type", "")
# ---------------------------------------------------------------- 大模型
async def _llm_chat(messages: list[dict], *, temperature: float = 0.7, max_tokens: int = 8000) -> str:
if not LLM_KEY:
raise ValueError("未配置 MCP_LLM_KEY(compose .env),无法调用大模型")
async with _client(timeout=180.0) as c:
r = await c.post(
f"{LLM_BASE}/chat/completions",
headers={"Authorization": f"Bearer {LLM_KEY}"},
json={
"model": LLM_MODEL,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
},
)
if r.status_code >= 400:
raise ValueError(f"大模型网关 {r.status_code}: {r.text[:300]}")
data = r.json()
return data["choices"][0]["message"]["content"]
def _parse_json_block(s: str) -> dict | None:
s = s.strip()
m = re.search(r"\{.*\}", s, re.S)
if not m:
return None
try:
return json.loads(m.group(0))
except json.JSONDecodeError:
# 容忍 ```json 围栏 / 尾逗号
cleaned = re.sub(r"^```(?:json)?|```$", "", m.group(0), flags=re.M).strip()
cleaned = re.sub(r",\s*([}\]])", r"\1", cleaned)
try:
return json.loads(cleaned)
except json.JSONDecodeError:
return None
# ---------------------------------------------------------------- 工程组装(MovieData)
def _split_captions(script: str, total: float, audio_dur: float | None) -> list[dict]:
"""口播文案 → 字幕段(按字符数加权切分时间轴;有配音时铺满配音时长)"""
raw = [ln.strip() for ln in re.split(r"\n+", script) if ln.strip()]
lines: list[str] = []
for ln in raw:
if len(ln) <= 24:
lines.append(ln)
else: # 长段按句切(保留标点)
parts = [s for s in re.split(r"(?<=[。!?!?;;])", ln) if s.strip()]
lines.extend([p.strip() for p in parts] if parts else [ln])
if not lines:
return []
timeline = audio_dur if audio_dur else total
if timeline <= 0:
return []
weights = [max(len(x), 1) for x in lines]
s = sum(weights)
caps: list[dict] = []
t = 0.0
for i, (line, w) in enumerate(zip(lines, weights)):
dur = timeline * w / s
if i == len(lines) - 1:
dur = max(timeline - t, 0.3) # 末段补到结尾,避免浮点欠尾
caps.append(_caption_element(line, t, max(dur, 0.3)))
t += dur
return caps
def _caption_element(text: str, start: float, dur: float, height: int = 1920, fill: str = "rgba(255, 255, 255, 1)") -> dict:
"""字幕元素:结构照抄 demo/test-app.json 的真实字幕(字号缩放随画布高度)"""
scale = round(height / 1920, 4) or 1
return {
"id": _uid(),
"name": text,
"text": text,
"textStyle": {
"radius": [0, 0, 0, 0],
"type": "newTextStyle",
"direction": "horizontal",
"fillType": "normal",
"fill": fill,
"padding": [5, 5, 5, 5],
"fontSize": 200,
"letterSpacing": 0,
"lineHeight": 200,
"align": "center",
"fontFamily": "抖音美好体",
"stroke": {"color": "rgba(51, 51, 51, 1)", "lineJoin": "round", "lineWidth": 18},
"fontFamilyURL": "/assets/fonts/抖音美好体.otf",
},
"style": {
"x": 540,
"y": round(height * 0.77, 2),
"alpha": 1,
"width": 392.96,
"height": 67.2,
},
"type": "caption",
"trackIndex": 0.5,
"startTime": round(start, 3),
"duration": round(dur, 3),
"textScale": round(0.32 * scale, 4),
"scale": 1,
"templateEnable": True,
"sceneIndex": 1,
}
def _image_element(resource_id: str, name: str, start: float, dur: float, w: int, h: int, idx: int) -> dict:
"""图片元素:字段集对齐 demo 视频元素(同一 BaseElement 血统)+ ImageElement 特有 matting"""
return {
"id": _uid(),
"name": name,
"type": "image",
"trackIndex": 0,
"resourceId": resource_id,
"startTime": round(start, 3),
"duration": round(dur, 3),
"style": {"width": w, "height": h, "x": w / 2, "y": h / 2}, # x/y 是中心点(demo 实证)
"blendMode": 0,
"flipx": False,
"scale": 1,
"filters": [],
"matting": {
"enabled": False,
"color": "#00FF00",
"similarityThreshold": 0.5,
"emergence": 0.5,
},
"sceneIndex": idx + 1,
"templateEnable": False,
}
def _audio_element(resource_id: str, dur: float, volume: float, name: str = "配音.mp3") -> dict:
return {
"id": _uid(),
"name": name,
"type": "audio",
"trackIndex": 1,
"resourceId": resource_id,
"startTime": 0,
"duration": round(dur, 3),
"muted": False,
"volume": volume,
"speed": 1,
"curveSpeed": False,
"clipTime": 0,
"fadeInTime": 0,
"fadeOutTime": 0,
}
def build_movie_data(
title: str,
w: int,
h: int,
images: list[dict], # [{res_id, zip_rel, name, iw, ih, mime, size}]
audio: dict | None, # {res_id, zip_rel, name, dur, mime, size}
caption_lines: str,
with_caption: bool,
caption_fill: str,
voice_volume: float,
image_duration: float,
) -> dict:
now = int(time.time() * 1000)
resouces: list[dict] = []
elements: list[dict] = []
n = len(images)
if audio:
total = float(audio["dur"])
span = total / n if n else 0.0
else:
span = float(image_duration)
total = n * span
for i, im in enumerate(images):
resouces.append({
"id": im["res_id"],
"name": im["name"],
"type": "image",
"url": im["zip_rel"],
"originId": im["res_id"],
"thumb": "",
"styleSize": {"width": im["iw"], "height": im["ih"]},
"fileType": im["mime"],
"from": "user",
"mustFetch": False,
"attrs": {"width": im["iw"], "height": im["ih"], "size": im["size"], "fileType": im["mime"]},
})
elements.append(_image_element(im["res_id"], im["name"], i * span, span, w, h, i))
if audio:
resouces.append({
"id": audio["res_id"],
"name": audio["name"],
"type": "audio",
"url": audio["zip_rel"],
"originId": audio["res_id"],
"thumb": "",
"styleSize": {"width": 0, "height": 0},
"fileType": audio["mime"],
"from": "user",
"mustFetch": False,
"duration": audio["dur"],
"attrs": {"size": audio["size"], "fileType": audio["mime"], "duration": audio["dur"]},
})
elements.append(_audio_element(audio["res_id"], audio["dur"], voice_volume, audio["name"]))
captions = _split_captions(caption_lines, total, audio["dur"] if audio else None) if with_caption else []
# checkNdata 门禁九键 + demo 同款元数据
return {
"title": title,
"createTime": now,
"updateTime": now,
"poster": "",
"version": "1.1.0",
"width": w,
"height": h,
"duration": round(total, 3),
"background": {},
"_hideLock": {},
"transitions": [],
"cameras": [],
"captions": captions,
"elements": elements,
"resouces": resouces,
}
def write_project_zip(movie: dict, files: dict[str, bytes], zip_path: Path) -> None:
"""project.json + materials/{type}/{file} —— 与 projectExport.ts 导出同构"""
zip_path.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
z.writestr("project.json", json.dumps(movie, ensure_ascii=False))
for rel, blob in files.items():
z.writestr(rel, blob)
# ---------------------------------------------------------------- MCP 工具
@mcp.tool()
async def service_status() -> dict:
"""自检:抓取反代 / 配音服务 / 大模型网关 / 工程目录 的可用性"""
out: dict[str, object] = {"time": time.strftime("%Y-%m-%d %H:%M:%S")}
try:
async with _client(8) as c:
r = await c.get(f"{UNICUT_URL}/")
out["unicut_nginx"] = f"OK({r.status_code})"
except Exception as e:
out["unicut_nginx"] = f"FAIL: {e}"
try:
async with _client(8) as c:
r = await c.get(f"{TTS_URL}/voices")
v = r.json()
n = len(v) if isinstance(v, list) else len(v.get("voices", [])) if isinstance(v, dict) else 0
out["tts"] = f"OK, {n} 个音色"
except Exception as e:
out["tts"] = f"FAIL: {e}"
out["llm"] = {
"base": LLM_BASE,
"model": LLM_MODEL,
"key": f"已配置({len(LLM_KEY)}位)" if LLM_KEY else "未配置 MCP_LLM_KEY",
}
out["projects_dir"] = PROJECTS_DIR or "未配置(文件夹模式工具不可用)"
out["out_dir"] = str(OUT_DIR)
out["public_url"] = PUBLIC_URL
return out
@mcp.tool()
async def grab_url(url: str, max_images: int = 20) -> dict:
"""抓取网页正文与配图(支持公众号 mp.weixin.qq.com 文章)。
返回 {ok, url, title, text(≤12000字), images[](绝对地址, 最多 max_images 张)}。
图片只是候选地址;create_video_project 下载时会再校验尺寸(≥160x120)与大小(≤10MB)。
"""
assert_public_url(url)
async with _client(30) as c:
r = await c.get(url, headers=HTTP_HEADERS)
r.raise_for_status()
final_url = str(r.url)
ctype = r.headers.get("content-type", "")
if "html" not in ctype and "xml" not in ctype:
# 非 HTML(如直接是文本/JSON):原样返回文本
return {"ok": True, "url": final_url, "title": final_url, "text": r.text[:12000], "images": []}
ext = extract_from_html(r.text, final_url)
return {
"ok": True,
"url": final_url,
"title": ext["title"],
"text": ext["text"],
"images": ext["images"][: max(0, min(int(max_images), 20))],
}
@mcp.tool()
async def generate_script(
source: str,
requirement: str = "",
style: str = "口播解说",
target_seconds: int = 60,
paragraph_count: int = 6,
) -> dict:
"""大模型生成短视频口播文案。
source:素材文本(抓取的文章正文)或主题描述;requirement:额外要求(受众/卖点/关键词)。
返回 {ok, title, paragraphs[], text};paragraphs 每段一句,直接对应字幕与时间轴分段。
"""
sys_prompt = (
"你是短视频口播文案编剧。只输出一个 JSON 对象,不要任何解释、不要 markdown 围栏:\n"
'{"title": "视频标题", "paragraphs": ["第一段口播", "第二段口播", ...]}\n'
"要求:口语化、有钩子、每段 1~2 句话适合做字幕;总字数 ≈ 目标秒数 × 4.5 字。"
)
user_prompt = (
f"风格:{style}\n目标时长:约 {target_seconds} 秒\n段落数:约 {paragraph_count} 段\n"
)
if requirement:
user_prompt += f"补充要求:{requirement}\n"
user_prompt += f"\n素材:\n{source[:8000]}"
content = await _llm_chat([
{"role": "system", "content": sys_prompt},
{"role": "user", "content": user_prompt},
])
data = _parse_json_block(content)
if data and isinstance(data.get("paragraphs"), list):
paras = [str(p).strip() for p in data["paragraphs"] if str(p).strip()]
title = str(data.get("title") or "").strip()
else: # 兜底:按行切
paras = [ln.strip() for ln in content.splitlines() if ln.strip()]
title = ""
if not paras:
raise ValueError(f"模型未返回可用文案:{content[:300]}")
return {"ok": True, "title": title, "paragraphs": paras, "text": "\n".join(paras)}
@mcp.tool()
async def list_voices() -> dict:
"""列出配音(edge-tts)可用音色 → {ok, count, voices[]}"""
async with _client(15) as c:
r = await c.get(f"{TTS_URL}/voices")
r.raise_for_status()
v = r.json()
voices = v if isinstance(v, list) else v.get("voices", [])
return {"ok": True, "count": len(voices), "voices": voices}
@mcp.tool()
async def tts(
text: str,
voice: str = "zh-CN-XiaoxiaoNeural",
rate: str = "+0%",
pitch: str = "+0Hz",
volume: str = "+0%",
) -> dict:
"""edge-tts 配音合成。返回 {ok, url(站点可直接播放), path(服务器文件), duration(秒), voice}。
text 建议 ≤2000 字;传给 create_video_project 的 audio 用 path。
"""
if not text.strip():
raise ValueError("text 不能为空")
async with _client(180) as c:
r = await c.post(
f"{TTS_URL}/tts",
json={"text": text, "voice": voice, "rate": rate, "pitch": pitch, "volume": volume},
)
if r.status_code >= 400:
raise ValueError(f"TTS {r.status_code}: {r.text[:300]}")
j = r.json()
if not isinstance(j, dict) or not j.get("url"):
raise ValueError(f"TTS 返回异常:{str(j)[:300]}")
u = str(j["url"])
internal = TTS_URL + (u[len("/localtts"):] if u.startswith("/localtts") else u)
ar = await c.get(internal)
ar.raise_for_status()
blob = ar.content
if not blob:
raise ValueError("TTS 音频为空")
audio_dir = OUT_DIR / "audio"
audio_dir.mkdir(parents=True, exist_ok=True)
name = Path(urlparse(u).path).name or f"{_uid()}.mp3"
dest = audio_dir / name
dest.write_bytes(blob)
try:
dur = float(MP3(io.BytesIO(blob)).info.length)
except Exception:
dur = 0.0
public = PUBLIC_URL + (u if u.startswith("/") else "/" + u)
return {"ok": True, "url": public, "path": str(dest), "duration": round(dur, 3), "voice": voice}
@mcp.tool()
async def create_video_project(
title: str,
images: list[str] | None = None,
script: str = "",
audio: str = "",
width: int = 1080,
height: int = 1920,
image_duration: float = 3.0,
caption: bool = True,
caption_fill: str = "rgba(255, 255, 255, 1)",
voice_volume: float = 1.0,
) -> dict:
"""生成米剪工程 ZIP(project.json + materials/),在米剪顶部「工程文件」导入即可出片。
images:图片 URL 或服务器本地路径(tts 工具产出的 path 也行);script:口播文案→自动做字幕
(按\\n分段,单段过长自动按句拆);audio:配音 URL/path(通常来自 tts 工具)。
有配音时图片自动拉伸铺满配音时长,否则每图 image_duration 秒。
返回 {ok, zip_path, zip_url(可直接下载), id, duration, images_used, captions, elements}。
"""
images = images or []
warnings: list[str] = []
# ---- 图片收集与校验
im_items: list[dict] = []
files: dict[str, bytes] = {}
for raw in images:
if len(im_items) >= 30:
warnings.append("图片超过 30 张,多余已丢弃")
break
try:
blob, ctype = await fetch_bytes(raw)
except Exception as e:
warnings.append(f"图片下载失败(跳过){raw[:80]}: {e}")
continue
if len(blob) > IMG_MAX_BYTES:
warnings.append(f"图片超 10MB(跳过){raw[:80]}")
continue
try:
im = Image.open(io.BytesIO(blob))
iw, ih = im.size
except Exception as e:
warnings.append(f"图片无法识别(跳过){raw[:80]}: {e}")
continue
if iw < IMG_MIN_W or ih < IMG_MIN_H:
warnings.append(f"图片太小 {iw}x{ih}(跳过){raw[:80]}")
continue
ext = _ext_for(ctype, raw, "jpg")
if ext not in ("jpg", "png", "webp", "gif"):
ext = "jpg" if im.format in ("JPEG", "PNG", "WEBP", "GIF") else "jpg"
fname = f"{uuid.uuid4().hex}.{ext}"
rel = f"materials/image/{fname}"
files[rel] = blob
im_items.append({
"res_id": _uid(),
"name": Path(urlparse(raw).path).name or fname,
"zip_rel": rel,
"iw": iw, "ih": ih,
"mime": f"image/{'jpeg' if ext == 'jpg' else ext}",
"size": len(blob),
})
# ---- 配音收集
aud: dict | None = None
if audio:
try:
blob, ctype = await fetch_bytes(audio)
ext = _ext_for(ctype, audio, "mp3")
fname = f"voice_{uuid.uuid4().hex[:8]}.{ext}"
rel = f"materials/audio/{fname}"
files[rel] = blob
try:
dur = float(MP3(io.BytesIO(blob)).info.length)
except Exception:
dur = 0.0
if dur <= 0:
warnings.append("配音时长无法识别,时间轴将按图片时长排布")
aud = None
else:
aud = {
"res_id": _uid(),
"name": Path(urlparse(audio).path).name or fname,
"zip_rel": rel,
"dur": dur,
"mime": f"audio/{'mpeg' if ext == 'mp3' else ext}",
"size": len(blob),
}
except Exception as e:
warnings.append(f"配音获取失败(忽略): {e}")
if not im_items and not aud:
raise ValueError("至少需要 1 张可用图片或 1 段配音")
# ---- 组装 MovieData
movie = build_movie_data(
title=title, w=int(width), h=int(height),
images=im_items, audio=aud,
caption_lines=script, with_caption=bool(caption and script.strip()),
caption_fill=caption_fill, voice_volume=float(voice_volume),
image_duration=float(image_duration),
)
# ---- 落盘 ZIP
ts = time.strftime("%Y%m%d-%H%M%S")
zip_name = f"{_slug(title)}-{ts}.zip"
zip_path = OUT_DIR / zip_name
write_project_zip(movie, files, zip_path)
# ---- 门禁自检:checkNdata 九键
required = ["title", "poster", "width", "height", "background", "transitions", "captions", "elements", "resouces"]
missing = [k for k in required if k not in movie]
if missing:
raise ValueError(f"工程缺少必需键:{missing}")
return {
"ok": True,
"id": f"mcp-{ts}-{_uid()[:6]}",
"zip_path": str(zip_path),
"zip_url": f"{PUBLIC_URL}/mcp-out/{zip_name}",
"duration": movie["duration"],
"size": zip_path.stat().st_size,
"images_used": len(im_items),
"has_audio": bool(aud),
"captions": len(movie["captions"]),
"elements": len(movie["elements"]),
"warnings": warnings,
"import_hint": "下载该 ZIP → 米剪顶部「工程文件」→ 导入(需先选择过工作文件夹)",
}
@mcp.tool()
async def list_projects() -> dict:
"""列出工作文件夹(文件夹模式 PROJECTS_DIR)里的米剪工程 → {ok, dir, projects[]}"""
if not PROJECTS_DIR:
return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR(compose .env)。文件夹模式需把它指到米剪里选择的同一目录"}
pdir = Path(PROJECTS_DIR) / "projects"
if not pdir.is_dir():
return {"ok": True, "dir": str(pdir), "projects": []}
items = []
for f in sorted(pdir.glob("*.json")):
try:
d = json.loads(f.read_text(encoding="utf-8"))
items.append({
"file": f.name,
"id": d.get("id") or f.stem,
"title": d.get("title") or f.stem,
"width": d.get("width"),
"height": d.get("height"),
"updatedAt": d.get("updatedAt"),
})
except Exception as e:
items.append({"file": f.name, "error": str(e)})
return {"ok": True, "dir": str(pdir), "projects": items}
@mcp.tool()
async def read_project(project_id: str) -> dict:
"""读取工作文件夹里的工程 JSON(folder 形态 {id,title,width,height,data})"""
if not PROJECTS_DIR:
return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR"}
f = Path(PROJECTS_DIR) / "projects" / f"{_slug(project_id)}.json"
if not f.is_file():
return {"ok": False, "error": f"工程不存在:{f}"}
data = json.loads(f.read_text(encoding="utf-8"))
return {"ok": True, "path": str(f), "project": data}
@mcp.tool()
async def write_project(project: dict) -> dict:
"""写入/更新工作文件夹工程(folder 形态:{id,title,width,height,data})。谨慎调用:会覆盖同 id 文件"""
if not PROJECTS_DIR:
return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR"}
if not isinstance(project, dict) or not project.get("data"):
return {"ok": False, "error": "需要 {id,title,width,height,data(MovieData)} 结构"}
pid = _slug(str(project.get("id") or _uid()))
project["id"] = pid
project["updatedAt"] = int(time.time() * 1000)
pdir = Path(PROJECTS_DIR) / "projects"
pdir.mkdir(parents=True, exist_ok=True)
f = pdir / f"{pid}.json"
f.write_text(json.dumps(project, ensure_ascii=False), encoding="utf-8")
return {"ok": True, "path": str(f), "id": pid}
# ---------------------------------------------------------------- 静态产物与健康检查路由
try:
from starlette.responses import FileResponse, JSONResponse
@mcp.custom_route("/health", methods=["GET"])
async def _health(_req):
return JSONResponse({"ok": True, "service": "unicut-mcp"})
@mcp.custom_route("/out/{name}", methods=["GET"])
async def _out(req):
name = Path(req.path_params["name"]).name # 防目录穿越
f = OUT_DIR / name
if not f.is_file():
return JSONResponse({"ok": False, "error": "not found"}, status_code=404)
return FileResponse(f, filename=name)
except AttributeError: # 老版本 SDK 无 custom_route,静默降级
pass
if __name__ == "__main__":
OUT_DIR.mkdir(parents=True, exist_ok=True)
mcp.run(transport="streamable-http")