zenstory-ai/video-recap-skills / skills/video-recap
video-recap
Verified把一段或多集视频做成带画框字幕的中文解说成片或纯原声高光短片。用户提供 .mp4 / .mov / .mkv / .webm,要求做影视解说、短剧二创、加旁白配音、跨集混剪或导出剪映草稿时使用;串联完成看片识台词、定主线与留原声、按台词停顿切镜头、写词检查、TTS 配音与混音加字幕。触发词:视频解说、影视解说、视频旁白、生成解说、视频 recap、video recap、voiceover、narration、recap。
scripts/providers/index_tts.py
View on GitHub"""Self-hosted index-tts JSON-to-WAV transport with no redirect forwarding."""
import contextlib
import io
import json
import os
import socket
import struct
import urllib.error
import urllib.parse
import urllib.request
import wave
from pathlib import Path
MAX_WAV_BYTES = 64 * 1024 * 1024
MAX_ERROR_BYTES = 512
SPEED_POLICY = "provider-default-no-rate-control"
class _SafeResponseError(RuntimeError):
pass
class _NoRedirectHandler(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
def _open_without_redirects(request, timeout):
return urllib.request.build_opener(_NoRedirectHandler()).open(request, timeout=timeout)
def validate_index_tts_config(endpoint, voice):
"""Validate explicit private settings without returning a credential-bearing URL."""
endpoint = str(endpoint or "")
voice = str(voice or "").strip()
if not endpoint:
raise ValueError("INDEX_TTS_ENDPOINT 必须显式配置")
if any(ord(char) < 32 or ord(char) == 127 for char in endpoint):
raise ValueError("INDEX_TTS_ENDPOINT 配置无效或包含禁止字符")
try:
parsed = urllib.parse.urlsplit(endpoint)
port = parsed.port
except ValueError:
raise ValueError("INDEX_TTS_ENDPOINT 配置无效") from None
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
raise ValueError("INDEX_TTS_ENDPOINT 必须是带主机名的 http/https URL")
if "@" in parsed.netloc or "?" in endpoint or "#" in endpoint:
raise ValueError("INDEX_TTS_ENDPOINT 禁止 userinfo、query 或 fragment 携带凭证")
if port is not None and not 1 <= port <= 65535:
raise ValueError("INDEX_TTS_ENDPOINT 配置无效")
if not voice:
raise ValueError("INDEX_TTS_VOICE 必须显式配置")
return endpoint, voice
def load_private_config(config, environ):
"""Read the private index-tts settings; validate them once, only when that provider is selected."""
endpoint = environ.get("INDEX_TTS_ENDPOINT", "").strip()
voice = environ.get("INDEX_TTS_VOICE", "").strip()
if config["tts_provider"] == "index-tts":
endpoint, voice = validate_index_tts_config(endpoint, voice)
config["index_tts_endpoint"] = endpoint
config["index_tts_voice"] = voice
def cache_settings(config):
"""Non-secret settings that decide reuse; the endpoint is never written to disk."""
return {
"index_tts_voice": config["index_tts_voice"],
"index_tts_speed_policy": SPEED_POLICY,
}
def default_controls(segment):
unsupported = []
for key, default in (("emotion", None), ("style", None), ("rate", "+0%"), ("pitch", "+0Hz")):
if segment.get(key) not in (None, "", default):
unsupported.append(key)
if unsupported:
raise RuntimeError(f"已配置的端点不接受段级控制字段: {', '.join(unsupported)}")
return "+0%", "+0Hz"
def synthesize_configured(text, output_path, config):
return synthesize_index_tts(
text, output_path, endpoint=config["index_tts_endpoint"],
voice=config["index_tts_voice"], timeout=config["tts_timeout"],
)
def valid_cached_receipt(cache_data, config):
"""A cached index-tts segment must carry the receipt of a request for the current voice."""
receipt = cache_data.get("provider_receipt")
return isinstance(receipt, dict) and receipt == {
"provider": "index-tts",
"requested_voice": config["index_tts_voice"],
}
def _discard_http_error_body(error):
"""Drain a bounded slice and close; the body is never surfaced (it may echo private URLs)."""
with contextlib.suppress(Exception):
try:
error.read(MAX_ERROR_BYTES)
finally:
error.close()
def _validate_wav(audio):
if len(audio) <= 44 or audio[:4] != b"RIFF" or audio[8:12] != b"WAVE":
raise RuntimeError("IndexTTS 返回的内容不是有效 WAV")
if int.from_bytes(audio[4:8], "little") + 8 > len(audio):
raise RuntimeError("IndexTTS 返回的 RIFF 声明长度超过响应,音频被截断")
try:
with wave.open(io.BytesIO(audio), "rb") as wav:
channels = wav.getnchannels()
sample_width = wav.getsampwidth()
sample_rate = wav.getframerate()
frames = wav.getnframes()
decoded = wav.readframes(frames)
except (EOFError, wave.Error) as exc:
# Support IEEE float (tag 3) or WAVE_FORMAT_EXTENSIBLE (0xFFFE -> 1/3) WAVs
offset, fmt, decoded = 12, None, None
while offset + 8 <= len(audio):
cid = audio[offset:offset + 4]
csize = int.from_bytes(audio[offset + 4:offset + 8], "little")
bstart, bend = offset + 8, offset + 8 + csize
if bend > len(audio):
raise RuntimeError("IndexTTS 返回的 WAV 音频数据被截断") from exc
if cid == b"fmt " and csize >= 16:
ftag, channels, sample_rate, _, _, bits_per_sample = struct.unpack_from(
"<HHIIHH", audio, bstart
)
if ftag == 0xFFFE and csize >= 26:
ftag = struct.unpack_from("<H", audio, bstart + 24)[0]
valid_fmt = (
(ftag == 1 and bits_per_sample in (8, 16, 24, 32))
or (ftag == 3 and bits_per_sample in (32, 64))
)
if not valid_fmt or channels < 1 or sample_rate < 1:
raise RuntimeError("IndexTTS 返回的 WAV 结构损坏或不受支持") from exc
sample_width = bits_per_sample // 8
fmt = True
elif cid == b"data":
decoded = audio[bstart:bend]
break
offset = bend + (csize & 1)
if not fmt or decoded is None or channels < 1 or sample_rate < 1 or sample_width <= 0:
raise RuntimeError("IndexTTS 返回的 WAV 结构损坏或不受支持") from exc
frames = len(decoded) // (channels * sample_width)
if not (1 <= channels <= 8 and 1 <= sample_width <= 8 and 8000 <= sample_rate <= 192000):
raise RuntimeError("IndexTTS 返回的 WAV 音频参数越界")
if frames <= 0:
raise RuntimeError("IndexTTS 返回的 WAV 没有音频帧")
if len(decoded) != frames * channels * sample_width:
raise RuntimeError("IndexTTS 返回的 WAV 音频数据被截断")
def synthesize_index_tts(text, output_path, *, endpoint, voice, timeout):
"""POST the exact {voice,text} contract and atomically persist an authentic WAV.
endpoint/voice were validated by load_private_config; timeout by lib's env_int."""
body = json.dumps({"voice": voice, "text": text}, ensure_ascii=False).encode("utf-8")
request = urllib.request.Request(
endpoint,
data=body,
headers={
"Content-Type": "application/json",
"Accept": "audio/wav",
"User-Agent": "video-recap/1.0",
},
method="POST",
)
try:
with _open_without_redirects(request, timeout) as response:
content_type = response.headers.get("Content-Type", "").lower()
content_length = response.headers.get("Content-Length")
if content_length and int(content_length) > MAX_WAV_BYTES:
raise _SafeResponseError("IndexTTS 返回 WAV 过大,已拒绝读取")
audio = response.read(MAX_WAV_BYTES + 1)
except urllib.error.HTTPError as exc:
_discard_http_error_body(exc)
if 300 <= exc.code < 400:
raise RuntimeError("IndexTTS 禁止 HTTP 重定向,未向其他主机发送待合成文本") from None
raise RuntimeError(f"IndexTTS 请求失败 (HTTP {exc.code}),远端返回错误状态") from None
except urllib.error.URLError as exc:
kind = "超时" if isinstance(exc.reason, (TimeoutError, socket.timeout)) else "网络"
raise RuntimeError(f"IndexTTS {kind}错误,未返回音频") from None
except (TimeoutError, socket.timeout):
raise RuntimeError("IndexTTS 超时错误,未返回音频") from None
except ValueError:
raise RuntimeError("IndexTTS 返回了无效的 Content-Length") from None
except _SafeResponseError as exc:
raise RuntimeError(str(exc)) from None
except Exception:
raise RuntimeError("IndexTTS 响应读取或关闭失败,未保存音频") from None
if len(audio) > MAX_WAV_BYTES:
raise RuntimeError("IndexTTS 返回 WAV 过大,已拒绝保存")
if "json" in content_type:
raise RuntimeError("IndexTTS 返回了 JSON 错误响应而不是音频;正文已隐藏")
_validate_wav(audio)
output = Path(output_path)
partial = Path(str(output) + ".part")
try:
partial.write_bytes(audio)
os.replace(partial, output)
finally:
partial.unlink(missing_ok=True)
return {"provider": "index-tts", "requested_voice": voice}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |