AIStoreHub Package Vault: hotclip
Package Slug: hotclip
Archetype: mcp_server
Upstream Repository: https://github.com/xixihhhh/hotclip
Workspace Files & Specifications (68 files)
- aistorehub.json (1187 bytes)
{ "name": "hotclip", "slug": "hotclip", "version": "1.0.0", "type": "mcp_server", "description": "免费开源的 AI 剪辑 / 直播切片工具:长视频、直播回放、播客一键切成爆款竖屏短视频,直发抖音/快手/B站/小红书/视频号——AI 找高光金句、弹幕热度进爆点判断、自动加字幕、横屏转竖屏,本地运行无水印不上传 | Free open-source Opus Clip alternative, 100% local: AI clips long videos & livestream VODs into viral 9:16 shorts for TikTok & YouTube Shorts. No credits, no watermark, no uploads. Win/macOS/Linux.", "owner": "community", "repository": "catalog", "github_repo_url": "https://github.com/xixihhhh/hotclip", "tags": [ "ai", "desktop-app", "video-editing", "long-video-to-shorts", "local-first", "ai-clip-generator", "claude-skill", "mcp-server", "tiktok", "bilibili", "auto-subtitles", "youtube-shorts", "electron", "speech-recognition", "opus-clip-alternative", "douyin", "podcast-clipper", "short-form-video", "video-clipping", "mcp-servers", "livestream" ], "imported_at": "2026-11-22T00:08:39.877Z" } - electron-builder.yml (1280 bytes)
appId: com.hotclip.app productName: HotClip directories: buildResources: build files: - out/**/* - "!**/*.map" # ffmpeg/ffprobe/sherpa/onnxruntime native addons must run from the real filesystem, not inside asar asarUnpack: - "**/node_modules/ffmpeg-static/**" - "**/node_modules/@ffprobe-installer/**" - "**/node_modules/sherpa-onnx-*/**" - "**/node_modules/onnxruntime-node/**" # bundled caption font (OFL) — libass reads it via the subtitles filter's fontsdir extraResources: - from: tools/qwen-speech-server.py to: speech/qwen-speech-server.py - from: resources/fonts to: fonts # web-caption overlay templates (rendered by the offscreen BrowserWindow) - from: resources/caption-templates to: caption-templates win: target: - nsis - zip # 绿色版 portable zip for Chinese users artifactName: HotClip-${version}-win-${arch}.${ext} nsis: oneClick: false allowToChangeInstallationDirectory: true createDesktopShortcut: true mac: identity: "-" hardenedRuntime: false notarize: false target: - dmg artifactName: HotClip-${version}-mac-${arch}.${ext} category: public.app-category.video linux: target: - AppImage artifactName: HotClip-${version}-linux-${arch}.${ext} category: Video npmRebuild: false - electron.vite.config.ts (1092 bytes)
import { defineConfig, externalizeDepsPlugin } from "electron-vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; import { resolve } from "path"; import packageJson from "./package.json"; const runtimeDependencies = ["electron", ...Object.keys(packageJson.dependencies)]; export default defineConfig({ main: { build: { rollupOptions: { external: (id) => runtimeDependencies.some((name) => id === name || id.startsWith(`${name}/`)), input: { index: resolve(__dirname, "src/main/index.ts"), "speech-worker": resolve(__dirname, "src/main/speech-worker.ts") }, output: { format: "cjs", entryFileNames: "[name].js", chunkFileNames: "chunks/[name]-[hash].js" }, } }, plugins: [externalizeDepsPlugin()], resolve: { alias: { "@core": resolve(__dirname, "src/core"), }, }, }, preload: { plugins: [externalizeDepsPlugin()], }, renderer: { plugins: [react(), tailwindcss()], resolve: { alias: { "@": resolve(__dirname, "src/renderer/src"), }, }, }, }); - INSTRUCTIONS.md (4646 bytes)
--- name: hotclip description: Turn long videos & livestream VODs into viral vertical shorts, 100% locally — on-device transcription, LLM highlight detection, 9:16 reframe with karaoke captions, and a per-clip render-QA report. Use when the user asks to clip / cut / 切片 / 剪 a long video, podcast or stream replay into short clips, find highlights / 爆点 in a video, or transcribe a media file. Footage never leaves the machine. --- # HotClip — local AI clipping pipeline HotClip(AGPL-3.0 开源)把几小时的直播回放/播客/课程切成可直接发布的竖屏短视频,全程本地运行。You drive the same pipeline the desktop app uses, via a headless CLI (or the bundled MCP server). Everything — ASR, highlight detection, cutting, caption burn-in — runs on this machine; the footage is never uploaded. ## Setup (once) 1. Repo + deps (skip what already exists): ```bash git clone https://github.com/xixihhhh/hotclip.git && cd hotclip pnpm install # ffmpeg/ffprobe are bundled npm binaries — nothing else to install ``` 2. LLM config for highlight detection, via env vars (any OpenAI-compatible endpoint): ```bash export HOTCLIP_LLM_BASE_URL=http://localhost:11434/v1 # local Ollama (no key needed) export HOTCLIP_LLM_MODEL=qwen3:8b # cloud endpoints additionally need: export HOTCLIP_LLM_API_KEY=sk-... ``` If these are missing, ask the user which LLM endpoint to use — do not guess keys. 3. First `transcribe`/`clip` run auto-downloads the ASR model (~1GB, CN mirror first). Warn the user it may take a few minutes once. ## Commands (run from the repo root) ```bash pnpm cli transcribe <video> # on-device word-level ASR (cached; instant on re-run) pnpm cli highlights <video> [--max-clips N] [--reference REF] [--json] # AI highlight candidates — review before cutting pnpm cli clip <video> [--max-clips N] [--reference REF] [--no-vertical] [--no-captions] [--out DIR] [--json] # fully managed: transcribe → detect → export + render-QA ``` `--reference <video>`: hand in a viral clip to model after — its pacing (duration / speech rate / shot-cut frequency / hook shape) is measured locally and steers candidate selection as a preference (never a hard rule). Use when the user says "切得像这条" / "learn from this clip". Input: MP4 / MKV / MOV / FLV / TS, or audio-only (podcasts get an auto-generated waveform video). Paths with spaces need quoting. ## Recommended workflow 1. **Review-first (default for interactive sessions)**: run `highlights --json`, show the user the candidates (title / score / hook / recommended), let them pick, then run `clip` — the pipeline re-detects from cache so this is cheap. For "just do it" requests, run `clip` directly. 2. **Read the output receipt**: the export folder contains one mp4 + cover JPG + `.post.txt` (publish copy) per clip, plus `clips.json` — per-clip evidence chain (`render`: what the pipeline did) and **`qa`: render-QA report** (`status: pass|warn` with issues like black frames, long silences, loudness deviation, duration mismatch, mid-word cuts, and **platform-risk words** in title/copy/captions via a local rule lint — `qa.contentHits` lists each term and where it appeared). 3. **Self-repair is automatic**: fixable warnings (leading/trailing silence or black frames → edge-trim; loudness deviation → second normalize pass) are repaired and re-checked in one pass; `qa.repair` records what was done (`actions`, `applied`). A repair is only kept when the re-check strictly improves. 4. **Surface QA warnings**: if any clip still has `qa.status === "warn"`, tell the user which clip and why; content-lint hits mean the copy/captions may be throttled or rejected by 抖音/小红书/视频号 — suggest rewording before publishing. Never silently ship a warned clip. ## Hard rules - **Never guess timestamps.** Cut points come from word-aligned transcription (`highlights` output); do not invent start/end seconds or hand-roll ffmpeg cuts when the pipeline covers it. - **Local-first**: do not upload the footage or transcript to any service beyond the user-configured LLM endpoint (which only ever receives transcript text, never media). - Exports land next to the source video in `<name>-hotclip/` unless `--out` is given; tell the user the absolute path when done. ## MCP alternative The same pipeline is exposed as a local stdio MCP server (`pnpm mcp`, tools: `clip_video` / `detect_highlights` / `transcribe_video`) — prefer it when the host supports MCP registration; the CLI is equivalent otherwise. - LICENSE (34523 bytes)
GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU Affero General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a cons - package.json (2239 bytes)
{ "name": "hotclip", "version": "0.35.2", "description": "Turn long videos & livestream replays into viral vertical shorts — AI highlight detection, auto captions, 9:16 reframe. Free, local-first, no watermark. 长视频/直播回放一键切成爆款竖屏短视频。", "main": "./out/main/index.js", "author": "xixihhhh", "license": "AGPL-3.0-only", "homepage": "https://github.com/xixihhhh/hotclip", "repository": { "type": "git", "url": "https://github.com/xixihhhh/hotclip.git" }, "scripts": { "dev": "electron-vite dev", "dev:web": "vite --config vite.renderer.config.ts", "build": "electron-vite build", "typecheck": "tsc --noEmit -p tsconfig.node.json && tsc --noEmit -p tsconfig.web.json", "test": "vitest run", "quality:eval": "tsx tools/quality-eval.ts", "quality:eval:asr": "tsx tools/asr-model-benchmark.ts", "quality:eval:vision": "tsx tools/vision-model-benchmark.ts", "mcp": "tsx src/mcp/server.ts", "cli": "tsx src/cli/index.ts", "start": "electron-vite preview", "pack:dir": "electron-vite build && electron-builder --dir", "dist": "electron-vite build && electron-builder --publish never" }, "devDependencies": { "@types/node": "^26.1.0", "@types/react": "^19.2.17", "@types/react-dom": "^19.2.3", "@types/unbzip2-stream": "^1.4.3", "@vitejs/plugin-react": "^6.0.3", "electron": "^43.0.0", "electron-builder": "^26.15.3", "electron-vite": "^5.0.0", "tsx": "^4.23.0", "typescript": "^6.0.3", "vite": "^8.1.3", "vitest": "^4.1.9" }, "dependencies": { "@ffprobe-installer/ffprobe": "^2.1.2", "@tailwindcss/vite": "^4.3.2", "ffmpeg-static": "^5.3.0", "onnxruntime-node": "^1.27.0", "react": "^19.2.7", "react-dom": "^19.2.7", "react-icons": "^5.7.0", "sherpa-onnx-node": "^1.13.3", "tailwindcss": "^4.3.2", "tar": "^7.5.22", "unbzip2-stream": "^1.4.3", "zustand": "^5.0.14" }, "pnpm": { "onlyBuiltDependencies": [ "electron", "esbuild", "ffmpeg-static", "@ffprobe-installer/darwin-arm64", "@ffprobe-installer/win32-x64", "@ffprobe-installer/linux-x64", "electron-winstaller" ] } } - README.en.md (74740 bytes)
<div align="center"> <a href="https://github.com/xixihhhh/hotclip/releases/latest"> <img src="docs/readme-hero-en.png" alt="HotClip — turn long videos and livestream VODs into viral vertical shorts, locally" width="100%"> </a> # HotClip — Free Open-Source AI Video Clipper: Long Videos → Viral Vertical Shorts **A free, local Opus Clip alternative — no credits, no watermark, no uploads** [简体中文](README.md) | **English** | [Website](https://xixihhhh.github.io/hotclip/en.html) | [Download](https://github.com/xixihhhh/hotclip/releases/latest) | [FAQ](#faq) | [Issues](https://github.com/xixihhhh/hotclip/issues) <p> <a href="https://github.com/xixihhhh/hotclip/releases/latest"><img src="https://img.shields.io/github/v/release/xixihhhh/hotclip?label=release&color=ff5722" alt="Latest release"></a> <a href="https://github.com/xixihhhh/hotclip/releases"><img src="https://img.shields.io/github/downloads/xixihhhh/hotclip/total?label=downloads&color=ff9800" alt="Downloads"></a> <img src="https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-blue" alt="Platform"> <a href="LICENSE"><img src="https://img.shields.io/github/license/xixihhhh/hotclip" alt="License"></a> <a href="https://github.com/xixihhhh/hotclip/stargazers"><img src="https://img.shields.io/github/stars/xixihhhh/hotclip?style=social" alt="GitHub stars"></a> </p> **AI highlight detection (with reasoning) · comfort-first 9:16 auto-reframe · dynamic word-level captions · filler-word & silence removal** 100% local · no watermark · no credits · no length caps · no account </div> Local transcription now checkpoints completed windows and resumes after interruption. Transcript editing adds cross-sentence search, virtualized long lists, and timing calibration with listen/apply/undo. After a full visual scan, the same search also finds confirmed scene descriptions and on-screen text, with one-click seeking to each timestamp. Exports improve language-aware caption readability. See the [speech and long-transcript guide](docs/local-speech.md) for optional local Qwen3-ASR setup and reproducible model evaluation. When exact search finds nothing, the transcript workspace can opt into **Similar speech** to find recognition results that differ by one character. Results are labeled approximate and can be listened to before selecting a clip. **Search to clip**: browse speech and visual matches in time order, filter by source, seek matched words, play context, then review preselected full sentences in the picker. Estimated timing stays labeled; long transcript picking is virtualized, and added candidates support undo/redo. When speaker detection is enabled for a conversation, the transcript workspace can filter by S1 / S2 and keep search, similar-speech matches and clip preview within that speaker. ## Export progress and cancellation Exports identify preparation, translation, publish copy, variants, encoding and finalization. Cancel during preparation, wait for cleanup, then retry without losing the candidate selection. A second desktop export cannot replace an active job. Progress stays below 100% until delivery completes. SRT-only export retains word timings even when burned captions are off. Cuts, jump cuts, audiograms, compilations and web-caption renders write into a private folder beside the destination, then replace it only after successful encoding. Failed or cancelled encoding preserves the previous target; previously completed clips stay available. Normal cancellation cleans staging files. A forced process exit can leave hidden `.hotclip-write-*` folders; remove them only after confirming no export is active. Protection applies to each encoding operation, not an all-or-nothing batch rollback. ## Why HotClip - **Actually free, not a trial**: AGPL-3.0 open source — no watermark, no credits, no length caps, no account. There is no crippled free tier, and no quota that expires at month's end - **Your footage never leaves your machine**: transcription, captions, cutting and export all run locally — unreleased footage and client material stay yours - **Every cut comes with receipts**: virality score + opening hook + reasoning + four-dimension review, plus visible caption-timing provenance and review ranges — the AI never guesses timestamps or fabricates confidence - **Automatic cuts listen for speech first**: a sub-1MB local detector independently checks the word timeline; word gaps that still contain speech stay intact, edge movement is strictly capped, and uncertain evidence preserves the original cut exactly - **Denoise only when needed, at the tier you choose**: Basic keeps the conservative fixed filter; Smart downloads an optional ~10MB 48kHz local dialogue model, processes the fully assembled edit before SFX/BGM, and truthfully falls back to Basic when unavailable - **Live chat feeds highlight detection**: the chat log next to your recording is auto-discovered (BililiveRecorder .xml and Douyin-recorder .jsonl both work), gifts and superchats weighted, spam-proof — the audience voting second by second, evidence most tools never even look at - **Ready to post, not just cut**: vertical clips + dynamic captions + covers + post copy + per-platform publish packs, all in one run - **Interrupted work picks up safely**: source, transcript, candidates, selections and hand-tuned cuts recover after restart; folder watch and webhooks share a durable queue with retry and cancel - **Real projects, not disposable tasks**: a project workspace manages multiple edits with offline/changed-source relinking; selections, copy, boundaries and transcript fixes all undo/redo, even after reopening - **Analysis and render always see the same picture**: motion/shots, timeline thumbnails, facial emotion and optional vision review all bind the globally selected video stream; executable PQ/HLG paths get a safe SDR analysis preview, and evidence caches isolate picture plus colour decision - **Long videos get faster on repeat**: identical base renders come straight from a bounded local cache; H.264 video is stream-copied only for keyframe-aligned, pixel-unchanged single cuts, while audio processing remains intact and every uncertain case falls back to accurate encoding - **Picture correction only when evidence calls for it**: on non-HDR-detected sources, optional adaptive finishing measures retained frames and makes a restrained correction only for clearly dark, flat or oversaturated footage; any detected PQ/HLG bypasses this SDR-domain pass, while healthy sources stay untouched and monochrome is never force-coloured - **HDR in, predictable SDR out**: automatic conversion requires an explicit PQ/HLG transfer plus complete, supported primaries, colour matrix and range; qualifying sources are tone-mapped in linear light to tagged SDR BT.709, while SDR and unknown transfers stay on the existing path - **Publishing teaches the next cut**: every export gets a stable content ID; multi-version exports form local A/B groups and only receive a direction when platform, publish window and sample evidence are comparable - **Clips while you sleep**: a 24/7 watch folder turns finished recordings into clips; one-click health diagnostics catch missing tools/models first, while CLI / MCP make it a one-sentence agent deliverable <!-- TODO(P0): 30-60s demo mp4 here (drag into GitHub's README web editor to get a user-attachments inline player): drop in a livestream replay → highlight candidate cards appear → one-click export → vertical clip with dynamic captions --> <!-- TODO(P0): "Sample output" table here: 2-3 vertical clips side by side in a <table> with <video> tags --> ## How to turn a long video into shorts — in three steps 1. **Import**: drop in a podcast, livestream replay, lecture or vlog (MP4 / MKV / MOV / FLV / TS, audio-only too), or paste a public Bilibili / YouTube-style video URL. The official resolver is downloaded and verified on first use; the source lands locally and enters the same on-device workflow 2. **Pick highlights**: local word-level transcription → the AI reads the whole transcript and nominates quotables, conflicts and peak moments — each with a **virality score, an opening hook and its reasoning**, cut points accurate to the word; untick anything you don't like 3. **Export**: one click produces vertical 9:16 clips — comfort-first reframe, dynamic word-synced captions, title cards and -14 LUFS loudness. PQ/HLG becomes tagged SDR BT.709 only when its primaries, colour matrix and range are complete and supported; otherwise the video stays unconverted and the result says so — plus a cover image and post copy, ready for **TikTok / Reels / Shorts / Douyin / Bilibili** ## Screenshots <p align="center"> <img src="docs/screenshots/04-highlights.png" width="840" alt="Pro workbench: preview, signal timeline, candidate list and inspector in one screen"> </p> <p align="center"><sub><b>A real workbench</b> — preview, timeline, candidate table and inspector on one screen: live-chat and loudness heat curves are drawn right on the timeline, and candidate segments sit on the peaks, so "why this segment" is visible at a glance. Every candidate ships a virality score, four-dimension breakdown and word-accurate cut points; weak picks are auto-flagged, and you make the final call.</sub></p> <table> <tr> <td width="50%"><img src="docs/screenshots/01-import.png" alt="Import a long video"><br/><sub>① <b>Import</b> a podcast / livestream replay / lecture — everything is processed locally, nothing is uploaded</sub></td> <td width="50%"><img src="docs/screenshots/02-engines.png" alt="Pick a transcription engine"><br/><sub>② <b>Pick an ASR engine</b> — the source stays docked in the workbench; three local tiers (SenseVoice / Paraformer / FireRedASR2) plus an optional cloud tier</sub></td> </tr> <tr> <td width="50%"><img src="docs/screenshots/03-transcript.png" alt="Word-level transcript"><br/><sub>③ <b>Transcript tab</b> — timestamped, click-to-fix, click a timecode to seek the preview; the foundation for highlights and captions</sub></td> <td width="50%"><img src="docs/screenshots/05-export.png" alt="One-click export"><br/><sub>④ <b>One-click export</b> — reusable export schemes, vertical clips ready to post, with cover images and clips.json metadata</sub></td> </tr> </table> > Real UI, driven with a sample livestream-selling replay. **If the cuts look right to you, a ⭐ helps more creators escape credit-metered clipping tools — and Star + Watch gets you release notifications.** ## ⬇️ Download **[Get the latest release »](https://github.com/xixihhhh/hotclip/releases/latest)** | Platform | File | Notes | |---|---|---| | Windows installer | `HotClip-x.y.z-win-x64.exe` | Double-click to install | | Windows portable | `HotClip-x.y.z-win-x64.zip` | Unzip & run | | macOS (Apple Silicon) | `HotClip-x.y.z-mac-arm64.dmg` | Drag into Applications | | Linux (experimental) | `HotClip-x.y.z-linux-x64.AppImage` | `chmod +x` then run; add `--no-sandbox` if it fails to launch with a sandbox error | > ⚠️ Windows builds are unsigned: on SmartScreen choose "More info → Run anyway". macOS builds use ad-hoc signatures from v0.32.1 and are not Apple-notarized; allow the first launch under System Settings → Privacy & Security. For “app is damaged”, see the [macOS installation guide](docs/macos-install.md). > > No Python, no Docker, no command line, no account — a real desktop app you double-click. ## Who it's for - **Streamers & clippers**: clip your own VODs into highlight shorts right after the stream; the 24/7 **watch folder** turns finished recordings into clips while you sleep; live-chat density feeds straight into highlight detection (both Bilibili danmaku .xml and Douyin recorder .jsonl chat logs work) - **Podcasters**: audio-only episodes still become video — an **audiogram waveform** plus quote captions turns your podcast into vertical clips - **Educators & marketers**: lectures, webinars and demos become snackable clips with covers, titles and metadata — with a banned-words lint before publish - **Talking-head creators**: silences, ums and stutters removed automatically; click-to-fix transcripts and a custom-vocabulary glossary keep names right, episode after episode ## How it compares (free Opus Clip alternative) Commercial clippers meter **credits per source minute** (a 2-hour podcast burns a month's quota; credits expire monthly), require **cloud uploads**, ship **black-box scoring**, or paywall captions. Open-source alternatives are CLI/Docker tools most creators can't install. HotClip fixes both sides: | | HotClip | OpusClip / Klap / Vizard (SaaS) | CapCut smart clipping | FunClip / autoclip (open source) | |---|---|---|---|---| | Price | **Free & open source** | $15–29+/mo, credits per source minute, expire monthly | Core features paywalled | Free | | Your footage | **Stays on your machine** | Mandatory cloud upload | Mostly cloud | Local | | Watermark / caps | **None** | Free tier: watermark, caps, projects expire in 3 days | Some restricted | None | | Account | **No sign-up** | Account required, projects deleted on unsubscribe | Login required | None | | Beginner-friendly | **Double-click installer** | Web app, easy | Easy | CLI / Docker / self-hosted | | Cut quality | **Word-aligned, reasoning attached, veto anything** | Black-box scoring | Black box | Sentence-level, unranked | | Vertical captions | **9:16 reframe + dynamic captions built in** | Yes (paid tiers) | Auto-captions paywalled | Mostly no vertical reframe | <sub>Competitor info verified 2026-07. Deep dive: [HotClip vs OpusClip](https://xixihhhh.github.io/hotclip/alternatives/opus-clip.html)</sub> ## Features > The full pipeline — import → AI highlights → vertical clips with captions — ships today. Expand each group for details; the details are all real features, not adjectives. ### 🎙️ Local transcription: three engines, word-level timestamps Already have a transcript? Choose **Import subtitles** on the transcription screen and select a UTF-8 SRT / WebVTT aligned to the current source. Review, correct and pick sentences immediately; start AI detection when needed. Import runs without ASR and leaves the transcription cache intact. Original cue boundaries are preserved; within-cue word timing is marked as estimated and appears in the timing-review filter. Manual splices strictly respect your selected boundaries. CLI `transcribe`, `highlights` and `clip` accept `--subtitles "/path/original.srt"`; all three MCP tools accept `subtitlePath`. For structured output with timing provenance, run `pnpm cli transcribe "/path/video.mp4" --subtitles "/path/original.vtt" --json`. Use one original-language transcript track, up to 5 MB, 20,000 cues and 100,000 words. Overlapping, unordered, out-of-range or streaming-clock-mapped subtitles produce an actionable error instead of silently dropping dialogue. Fast SenseVoice (5 languages, 170MB) / balanced Paraformer / most-accurate FireRed - README.md (69287 bytes)
<div align="center"> <a href="https://github.com/xixihhhh/hotclip/releases/latest"> <img src="docs/readme-hero.png" alt="HotClip 爆款切片 — 本地 AI · 一键切片:长视频、直播回放一键切成爆款竖屏短视频" width="100%"> </a> # HotClip 爆款切片 — 免费开源的 AI 直播切片 / 视频剪辑工具 **长视频、直播回放一键切成爆款竖屏短视频** **简体中文** | [English](README.en.md) | [官网](https://xixihhhh.github.io/hotclip/) | [下载](https://github.com/xixihhhh/hotclip/releases/latest) | [FAQ](#常见问题) | [反馈](https://github.com/xixihhhh/hotclip/issues) <p> <a href="https://github.com/xixihhhh/hotclip/releases/latest"><img src="https://img.shields.io/github/v/release/xixihhhh/hotclip?label=%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC&color=ff5722" alt="最新版本"></a> <a href="https://github.com/xixihhhh/hotclip/releases"><img src="https://img.shields.io/github/downloads/xixihhhh/hotclip/total?label=%E4%B8%8B%E8%BD%BD%E9%87%8F&color=ff9800" alt="下载量"></a> <img src="https://img.shields.io/badge/%E5%B9%B3%E5%8F%B0-Windows%20%7C%20macOS%20%7C%20Linux-blue" alt="平台"> <a href="LICENSE"><img src="https://img.shields.io/github/license/xixihhhh/hotclip?label=%E5%BC%80%E6%BA%90%E5%8D%8F%E8%AE%AE" alt="License"></a> <a href="https://github.com/xixihhhh/hotclip/stargazers"><img src="https://img.shields.io/github/stars/xixihhhh/hotclip?style=social" alt="GitHub stars"></a> </p> **AI 自动找爆点(附理由) · 横屏转竖屏 9:16 · 自动加动态字幕 · 去气口剪口头禅** 无水印 · 无积分制 · 不限时长 · 素材不上传 · 连注册都不用 </div> 本地转写现支持分段保存、停止与续跑;逐句稿提供跨句全文搜索、长稿虚拟列表,以及可试听、确认和撤销的局部时间校准。开启全场画面扫描后,搜索还会命中已确认的画面描述和屏显文字,并可直接跳到对应时刻。导出自动改善不同语言的字幕阅读节奏。可选 Qwen3-ASR 本地服务与模型评估命令见 [语音与长稿使用指南](docs/local-speech.md)。 搜索没有精确命中时,逐句稿可主动开启「相近台词」:查找仅差一个字的识别结果,并明确标为近似命中,试听确认后再选段。 **搜到就能选段**:台词和画面命中按时间统一浏览,可按来源筛选、定位命中字词、试听上下文,再带着完整句进入选段窗口。估算时间明确标注;长稿选段按需渲染,确认加入候选后支持撤销和重做。 多人对谈开启说话人识别后,逐句稿工作台可按 S1 / S2 等说话人筛选;搜索、相近台词和选段预览会同步收窄范围。 ## 切片时长、数量与直播品类 逐字稿生成后,工作台右栏常驻「时长档」和「最多切片数量」,无需切换到检测参数页。需要带货长切片时选择 **1–2 分钟 (60–120 秒)**,数量可选 **1–12 条**,再点「重新检测」。数量是候选上限,不会为凑数降低质量;时长指源片选段,导出剪气口等处理后可能缩短。 CLI 的 `clip` 和 `highlights` 支持同一组参数: ```bash pnpm cli clip "/path/直播回放.mp4" --genre shopping --clip-length extended --max-clips 10 pnpm cli highlights "/path/游戏录播.mp4" --genre game --genre-custom "只选包含铺垫和反应的完整翻盘" --json ``` `--clip-length` 可选 `short` (10–30 秒)、`standard` (8–40 秒,默认)、`long` (40–90 秒)、`extended` (60–120 秒)。`--genre` 复用桌面全部品类 id,默认 `auto`,非法值会报错并列出可选项。非空的 `--genre-custom` 覆盖内置判据,所选品类仍控制证据通道和导出跳剪节奏。 MCP 的 `clip_video` / `detect_highlights` 对应参数为 `genre`、`genreCustom`、`clipLength`、`maxClips`。录播监听窗口也可选择品类、时长和数量,文件夹监听与 webhook 均在启动时保存设置;修改后需重启监听,任务重试保留原设置。 ## 导出与取消 导出页会显示素材检查、字幕翻译、文案生成、多版准备、编码和文件整理阶段。准备阶段即可取消,停止清理结束后可以直接重试;重复启动不会覆盖正在运行的任务。进度在文件整理完成前最高显示 99%。关闭烧录字幕、只打开 SRT 导出时,也会保留逐字稿供字幕文件使用。 常规剪切、跳剪、音频成片、合集与动态字幕编码先写目标目录内的独立临时文件,成功关闭编码器后才替换目标。失败或取消的这次编码不会把已有目标文件截断;同一批此前完成的切片保留。普通取消会清理临时目录;系统强制结束进程时可能留下隐藏的 `.hotclip-write-*` 目录,需在确认没有导出运行后再处理。这是逐次编码的文件保护,不是整批回滚。 ## 为什么选 HotClip - **真免费,不是试用**:开源 AGPL-3.0,无水印、无积分制、不限时长、无需注册——不存在「免费档阉割」,也没有月底清零的积分 - **素材不出你的电脑**:转写、字幕、剪辑、导出全程本地;未发布素材、客户内容不用交给任何云 - **AI 每一刀都有理由**:爆款分 + 开场钩子 + 推荐理由 + 四维分项,切点逐字对齐;字幕时间来源与需复核区间也明确可见——AI 不猜时间戳,也不用虚构置信度替你做决定 - **自动切点也会先听人声**:不到 1MB 的本地语音检测给逐字时间再加一道独立证据;词间空隙仍有人声就不剪,开头结尾在严格上限内保护尾音,证据对不上时完全沿用原切点 - **有底噪才开,而且能选档**:基础档继续用温和固定滤镜;智能档首次下载约 10MB 的 48kHz 本地人声增强模型,在完整剪辑组装后、音效/BGM 混入前只处理讲话原声,不可用时自动回退基础档并在成片回执里说清楚 - **弹幕直接进爆点判断**:自动发现录播旁的弹幕文件(B 站录播姬 / 抖音录制都认),礼物 SC 点赞按互动计权、单人刷屏防伪——观众逐秒投的票,别家工具还没在看的证据 - **切完就能发**:竖屏成片 + 动态字幕 + 封面 + 发布文案 + 平台发布包一次出齐,抖音 / 快手 / B站 / 小红书 / 视频号逐平台整理好 - **任务断了也接得上**:重启自动恢复素材、逐字稿、候选与手调切点;录播监听和 webhook 共用持久任务队列,失败可重试、运行中可取消 - **长期工程,不是一次性任务**:项目工作区集中管理多份工程,源素材离线/变更会明确提示并可重连;标题、切点、勾选和逐句稿都能撤销/重做,重开项目后编辑历史仍在 - **分析和成片始终看同一画面**:多视频轨素材从运动/镜头、时间轴缩略图、表情与视觉复核到最终渲染都绑定同一条选中视频轨;PQ/HLG 的分析预览先安全转成 SDR,证据缓存也按画面与色彩决定隔离 - **长视频越剪越快**:完全相同的基础成片直接复用本地渲染缓存;H.264 单段在关键帧对齐且不改画面时只复制视频流,音频处理照常,条件不满足自动回退精确编码 - **HDR 手机素材不再盲转**:PQ/HLG 且色彩信息完整、受支持的 HDR 视频自动安全转成社媒通用 SDR BT.709;路径不完整/不支持就保持既有渲染并明确提示,普通 SDR 不变 - **画面只在确有必要时校正**:可选的智能画面校正只测最终保留帧,明显偏暗、发灰或过饱和才克制修正;正常素材不乱动、黑白素材不强行增艳,不用上传也不新增模型 - **发出去还会学习**:每条成片有稳定内容 ID,导入播放/互动 CSV 后自动对账;一片多版自动形成本地 A/B 实验,只在同平台、同发布窗口、样本充足时给方向判断 - **睡觉也在出片**:录播监听 7×24 盯住录播姬 / OBS 目录,录完自动切;出片前一键健康检查,CLI / MCP 让 Agent 一句话交付 <!-- TODO(P0): 在此处放 30-60 秒实操 demo mp4(README 网页编辑器直接拖入 mp4 生成 user-attachments 页内播放器): 内容:拖入直播回放 → 爆点候选卡片弹出 → 一键导出 → 竖屏成片带动态字幕播放 2 秒 --> <!-- TODO(P0): 在此处放「成片效果」表格:<table> 并排 2-3 个竖屏成片 <video>(中文播客金句 / 带货直播高能 / 中英混说),每个配一行标题 --> ## 直播切片怎么做?三步出片 <p align="center"> <img src="docs/readme-story.png" alt="一条长视频变成多个爆款短片:AI 智能识别高光时刻,一键生成竖版爆款短视频" width="100%"> </p> 1. **导入**:把播客、直播回放、课程、Vlog 丢进来(MP4 / MKV / MOV / FLV / TS,纯音频也行),或直接粘贴 B站 / YouTube 等公开视频地址;首次自动下载并校验官方解析工具,源视频落到本机后进入同一套处理流程 2. **AI 找爆点**:本地逐字转写 → AI 通读全文挑出金句、冲突、高能片段,每条附**爆款分、开场钩子和推荐理由**,切点精确到词;看不顺眼的取消勾选即可 3. **一键出片**:竖屏 9:16 成片直接导出——舒适构图、动态逐字字幕、标题贴片、响度归一,附封面图和发布文案,打开就能发**抖音 / 快手 / B站 / 视频号 / 小红书 / TikTok** ## 界面预览 <p align="center"> <img src="docs/screenshots/04-highlights.png" width="840" alt="专业工作台:预览 + 信号时间轴 + 候选列表 + 详情栏一屏工作"> </p> <p align="center"><sub><b>专业工作台</b> —— 预览、时间轴、候选表、详情栏一屏工作:弹幕/响度热度曲线直接画在时间轴上,候选段就落在峰上,「为什么选这段」一眼可见;每条候选附爆款分、四维分项与逐字精确切点,弱片自动标「不建议发布」,勾选取舍全在你手。</sub></p> <table> <tr> <td width="50%"><img src="docs/screenshots/01-import.png" alt="导入长视频"><br/><sub>① <b>导入</b> 播客 / 直播回放 / 课程 —— 全程本地处理,素材不上传</sub></td> <td width="50%"><img src="docs/screenshots/02-engines.png" alt="选择转写引擎"><br/><sub>② <b>选转写引擎</b> —— 素材常驻工作台,三档本地(SenseVoice / Paraformer / FireRedASR2)+ 可选云端,隐私分级明标</sub></td> </tr> <tr> <td width="50%"><img src="docs/screenshots/03-transcript.png" alt="逐字转写"><br/><sub>③ <b>逐句稿页签</b> —— 带时间戳、即点即改,点时间码画面直接跳过去;是找爆点和字幕的地基</sub></td> <td width="50%"><img src="docs/screenshots/05-export.png" alt="一键出片"><br/><sub>④ <b>一键出片</b> —— 出片方案一键复用,竖屏成片直接发,附封面图与 clips.json 元数据</sub></td> </tr> </table> > 截图为真实界面(演示素材:一段带货直播回放)。**觉得切得准,就点个 ⭐——每颗星都是让更多人不用再给积分制付费的一票,Star + Watch 还能第一时间收到新版本通知。** ## ⬇️ 下载安装 **[去 Releases 页下载最新版 »](https://github.com/xixihhhh/hotclip/releases/latest)** | 平台 | 文件 | 说明 | |---|---|---| | Windows 安装版 | `HotClip-x.y.z-win-x64.exe` | 双击安装即用 | | Windows 绿色版 | `HotClip-x.y.z-win-x64.zip` | 免安装,解压即用 | | macOS(Apple 芯片) | `HotClip-x.y.z-mac-arm64.dmg` | 拖进「应用程序」 | | Linux(实验性) | `HotClip-x.y.z-linux-x64.AppImage` | `chmod +x` 后运行;启动报 sandbox 错误加 `--no-sandbox` | > ⚠️ Windows builds are unsigned: on SmartScreen choose "More info → Run anyway". macOS builds use ad-hoc signatures from v0.32.1 and are not Apple-notarized; allow the first launch under System Settings → Privacy & Security. For “app is damaged”, see the [macOS installation guide](docs/macos-install.md). > > 不用 Python、不用 Docker、不用命令行——下载安装包双击即开。 ## 谁在用:直播切片 · 切片带货 · 播客剪辑 · 口播剪辑 - **主播 / 切片手(直播切片、录播切片)**:下播后把几小时直播回放自动切成高光短视频;开「录播监听」后 7×24 无人值守,录完自动出片;弹幕热度直接进爆点判断,B 站录播姬 .xml 和抖音直播录制的弹幕 .jsonl 都认 - **带货 / 矩阵团队(切片带货,已授权)**:商品讲解模式按转化逻辑选段,一键全托管批量出片,每条附发布文案、封面图与 clips.json 元数据,120+ 违禁词规则发布前点名;**一片多版**出差异化包装、**平台发布包**按各平台规格整理齐套素材,多账号多平台拿起来直接发 - **播客主(播客剪辑、播客视频化)**:纯音频也能出片——自动合成波形动画画面 + 金句字幕,播客节目直接变竖屏视频 - **知识区 UP 主 / 口播博主(口播剪辑)**:自动去气口、剪口头禅、删停顿,长口播变利落短视频,字幕逐字点亮 ## 和 OpusClip、剪映智能切片的区别 市面上的 AI 切片工具,要么**按分钟扣积分**(一期 2 小时播客烧光整月额度,积分月底还清零),要么**必须上传云端**(未发布素材/客户内容不敢传),要么切点稀烂、中文支持名不副实;开源侧则几乎全是命令行,小白装不起来。HotClip 把两边的坑同时填上: | | HotClip | OpusClip / Klap / Vizard 等 SaaS | 剪映/CapCut 智能切片 | FunClip / autoclip 等开源 | |---|---|---|---|---| | 价格 | **免费开源** | $15-29+/月,按源视频分钟扣积分,积分月底清零 | 核心功能进会员/Pro | 免费 | | 素材去向 | **全程本地,不上传** | 必须上传云端 | 云端处理为主 | 本地 | | 水印/时长限制 | **无** | 免费档有水印、限时长、项目 3 天过期 | 部分模板有限制 | 无 | | 账号 | **无需注册** | 需注册,退订删项目 | 需登录 | 无 | | 小白可用 | **双击安装即用** | 网页版,易用 | 易用 | 命令行/Docker/自部署 | | 切点质量 | **逐字对齐精确到词,附理由可否决** | 黑盒打分,常被抱怨断章取义 | 黑盒 | 按句切,无爆点排序 | | 竖屏字幕 | **9:16 重构 + 逐字点亮字幕内置** | 有(付费档) | 自动字幕已进付费 | 多数无竖屏重构 | <sub>竞品信息核对于 2026-07,具体以各家官网为准。详细英文对比:[HotClip vs OpusClip](https://xixihhhh.github.io/hotclip/alternatives/opus-clip.html)</sub> ## 功能一览 > 「导入 → AI 找爆点 → 竖屏+逐字字幕成片」三步全流程可下载可用。每组点开看细节——细节里全是真功能,不是形容词。 ### 🎙️ 本地转写:三档引擎,逐字时间戳 已有原文字幕时,在转写页点击 **导入字幕**,选择与当前素材对齐的 UTF-8 SRT / WebVTT,即可进入逐句稿、纠错和文字选段;需要 AI 分析时再点击检测。导入本身不运行 ASR,也不覆盖已有转写缓存。保留字幕原句时间,句内字词时间明确标记为估算,可通过「需复核」筛选检查;手动拼接严格遵守选段边界。 CLI 的 `transcribe`、`highlights`、`clip` 均支持 `--subtitles "/path/original.srt"`,三个 MCP 工具均支持 `subtitlePath`。例如 `pnpm cli transcribe "/path/video.mp4" --subtitles "/path/original.vtt" --json` 可输出带时间来源的结构化逐句稿。支持单轨原文字幕,最大 5 MB、20,000 段、100,000 字词;重叠、乱序、超出素材时长或带流媒体时间映射的字幕会提示修正后再导入,不会静默丢句。 快速 SenseVoice(五语种,170MB)/ 均衡 Paraformer(中文更准)/ 最准 FireRedASR2(普通话/方言/中英混说)——全部本地运行、普通 CPU 就能跑,首次自动下载(国内镜像优先、断点续传);另有云端档 ElevenLabs(自带 Key,只上传音轨)。 <details> <summary><b>展开细节</b>:转写缓存 · 逐句稿即点即改 · 热词词表 · 说话人分离</summary> - **转写结果本地缓存**:同一个文件下次再开、换设置多切几条,跳过重转写秒进挑爆点;文件改动或换引擎自动失效 - **逐句稿即点即改(转写纠错)**:每句 hover 出铅笔当场改,字幕、双语翻译、发布文案全用修正后文本;重建的逐词时间会明确标「估时」,可一键只看需复核句 - **热词词表(专有名词一次纠错,期期自动修正)**:人名/品牌/术语改一处,一键应用到全片同错句并入词表;之后每次转写自动整词替换,词表更新后同素材直接用缓存重放、不重跑识别 - **多人对谈「谁在说话」**:一键说话人分离(本地 pyannote + 3D-Speaker,零上传),逐句标注谁在说;AI 按说话人挑段不断章取义,气泡字幕按人上色 </details> ### 🔥 AI 找爆点:九路证据链,每一刀都有理由 LLM 只负责«挑哪段»并引用原文,时间戳由逐字转写**反向对齐**——不让 AI 猜时间。每条候选附爆款分、开场钩子、推荐理由与四维分项,弱片自动标「不建议发布」。 <details> <summary><b>展开细节</b>:复评质量门 · 弹幕/表情/视觉信号 · 画面复核 · 参考爆款 · 省钱漏斗 · 商品模式</summary> - **AI 复评质量门**:严格评审员二次盲评,钩子/结构/价值/热点四维分项打分,每维一句话理由,另给一条可烧上片头的悬念句 - **爆款分=排名不是玄学**:四维加权后按候选间相对排名归一(推荐档 76-99),同批次可直接比大小,不受批间漂移影响 - **画面声音证据**:响度峰值、镜头切换密度与轻量运动峰值本地采集注入判断;工作台时间轴直接显示运动曲线,不再纯文本盲选 - **弹幕热度信号(B 站 / 抖音双格式)**:自动发现录播旁的弹幕文件——录播姬同名 .xml、抖音直播录制的弹幕 .jsonl 都认;滑窗密度+高能词加权圈出观众实时高能段,SC/舰长/礼物/关注/点赞按互动档另计权(花钱和动手的票比弹幕硬);同一人刷屏有贡献封顶(一个人刷一百条不算全场沸腾),突然爆发另有加成——观众逐秒投的票,证据力最强 - **信号共振与算力聚焦**:多路信号同秒共振才可信——只有音量高可能是 BGM,只有弹幕高可能是刷屏,音量+弹幕+笑声一起亮才是真爆点;运动峰与均匀全场覆盖共同引导抽帧,避免前半段热闹画面挤掉后半段安静但重要的场景 - **本地多模态证据索引**:响度/镜头/运动、TransNetV2 边界与可选视觉扫描结果按「源文件指纹 + 能力版本 + 模型」原子落盘;桌面、CLI、MCP、录播监听与 webhook 复用同一份结果。默认最多 64MB、最近使用淘汰,损坏或配置变化自动重算,视觉 API Key 从不写入索引 - **表情峰值信号**:YuNet + FER+(几 MB 的 MIT 协议模型)找大笑/惊讶/激动峰值,零配置让 AI「看见」情绪爆点 - **视觉爆点信号(可选)**:新用户默认预填本机 Ollama `qwen3.5:4b`,一个多模态模型同时处理文字与九宫格接触表,研判画面高能时刻,专治逐字稿里看不见的画面梗;端点不可用自动跳过,不会挡住纯文字找爆点 - **AI 画面复核(可选)**:检测后对头部候选再抽九宫格让视觉模型过目——画面炸裂加分、信号候选画面死气降分、画面与标题货不对板标警告,看点写进推荐理由;本机 Ollama 免费,视觉设置填 API Key 即切云端模型 - **参考视频驱动**:丢一条想对标的爆款切片,本地实测节奏生成风格画像,选段向对标靠拢(CLI `--reference`) - **审阅反馈回流**:你采用/否决的候选自动落进本地偏好档,下次「同类优先/同类少选」,越用越懂你;偏好数据不出本机 - **真实数据反哺选段**:把平台导出的 CSV/JSON 播放与互动数据交给 `pnpm cli feedback`,HotClip 在本机归纳高/低表现内容的题材、钩子与时长共性;桌面端/CLI/MCP/录播监听下一轮找爆点都会参考真实观众结果,不照抄旧标题、不上传账号数据 - **端侧两级漏斗**:本机小模型先初筛,云端只精读入围段;长稿最多同时预筛两段,失败或超时未处理的段落完整保留,减少本机模型排队压力 - **模型请求有等待边界**:文字分析、画面研判和模型列表都能在无响应时结束等待;短暂限流或服务忙时最多自动重试一次,等待中可取消,错误提示隐藏回显密钥 - **镜头切点吸附**:TransNetV2 本地检测真实镜头边界,切点自动吸附(词边界守卫绝不切掉说话) - **商品讲解模式(带货直播切片)**:填商品词,按「试用实测 > 卖点 > 价格机制」转化逻辑选段;纯憋单拉互动段(平台判违规)明确排除 - **切片时长档**:短·10-30s / 标准·8-40s / 长·40-90s 一键切换重选段,目标时长作为硬约束进提示词 </details> ### 📝 字幕与文案:自动加字幕、发布文案一步到位 动态字幕多款样式一档切换——关键词高亮、大字弹出、气泡特效、动态极简——词级时间戳驱动、语义断行不拦腰截断,SRT 可导出、双语可选;AI 起的爆款标题自动烧进顶部贴片,发布文案带标签生成。 <details> <summary><b>展开细节</b>:气泡特效字幕 · 开场钩子 · 双语 · Hormozi 大字 · 违禁词 lint · AIGC 标识</summary> - **语义断行(免 Key)**:顺着标点在真实子句处换行,长句无标点时回看结构助词断行——等价于头部项目用 LLM 插 `[br]`,但零额外调用 - **气泡特效字幕(Web 渲染引擎)**:内置 Chromium 离屏逐帧渲染 CSS 字幕层——自适应圆角气泡底、关键词渐变金字、弹性入场,libass 做不出的效果一档切换 - **开场钩子(黄金3秒)**:AI 写的悬念句自动大字烧在开头 2 秒上三分之一,淡入淡出避开主体;没写出悬念句自动不加 - **Hormozi 大字爆点字幕**:带货营销风,特大加粗、硬阴影、逐词点亮 - **说话人标签字幕**:多人对谈时换人行首自动加彩色「A:」「B:」标签(与气泡字幕按人上色同一套色板),观众一眼分清谁在说 - **双语字幕(出海一步到位)**:整句语境翻译烧成副轨,中文源自动译英;跳剪压缩时译文行同步重映射 - **SRT 字幕文件导出**:时间轴已对齐跳剪/剪口头禅后的成片,断行与烧录字幕一致 - **发布文案生成**:每条切片生成钩子风标题 + 3-6 个垂类标签 + 简介,落 `.post.txt` 与 clips.json;8 种钩子角度 × 5 类 CTA 可选 - **平台违禁词 lint**:120+ 本地规则扫标题/文案/字幕,绝对化用语/医疗宣称/导流话术发布前点名,零上传 - **AIGC 标识(合规内建)**:按《人工智能生成合成内容标识办法》一键打标——显式画面标识 + 隐式元数据标识 </details> ### 🎬 成片质量:像人剪的,不是机切的 舒适优先智能取景、气口跳剪、剪口头禅、响度归一 -14 LUFS、帧精确切割——制作链一步到位,每条切片出片后还有自我质检。 <details> <summary><b>展开细节</b>:横屏转竖屏 · 去气口 · 降噪 · 音效 · BGM · 质检自修复 · 智能封面 · 高潮前置 · 爆点闪现 · 精准切点</summary> - **舒适优先智能取景(横屏转竖屏 9:16)**:逐镜头汇总所有可见人脸;单人移动或多人同框只要能安全装进竖屏就锁住机位,避免追脸晃动。确实超出画幅才平滑跟随,短暂漏检先保持、持续丢失自动回正,无人脸回退居中裁剪 - **构图回执**:`clips.json` 记录每条成片锁定/多人锁定/跟随/丢失回正/居中兜底的镜头数;镜头锁定会保持到下一次切镜,跳剪也不会让裁窗穿过被删区间 - **HDR 安全转 SDR(自动)**:输入需同时具备 PQ(ST 2084)或 HLG 传递、BT.2020 原色、BT.2020 非恒定亮度矩阵与 TV/PC 范围,才会先在线性光域克制映射高光,再输出电视范围 SDR BT.709;有 MaxCLL / mastering 峰值时用于约束高光,缺失则交给 FFmpeg 安全估计。连续剪、跳剪、多片段、高潮前置、Web 字幕与质检修复走同一决定。检测到 HDR 但色彩路径不完整/不支持时不猜测、不套用 SDR 域智能校正,完成结果会提示未转换;若色彩信息检查本身失败,同样停用 SDR 域智能校正并明确提示检查失败;普通 SDR 沿用原路径。不新增模型、下载或上传,源色彩信息与决定写入 `clips.json` - **智能画面校正(可选,默认关)**:复用本地 Tier-0 分析,按每条最终保留片段测量亮度、反差与饱和度;只对证据充分且明显偏暗/发灰/过饱和的画面做有硬上限的轻校正,正常素材保持原样、接近黑白的素材不强行增艳。跳剪、多片段与高潮前置分别按真实保留窗口计算,具体测量和调整进入 `clips.json` - **去录屏UI**:手机直播录屏的状态栏/固定UI/黑边,时域方差自动检测裁除 - **语音感知气口跳剪(自动去气口)**:剪掉停顿静音并拼接,字幕时间轴同步重映射;「无词 + 声学静默 + 本地语音检测无人声」三重判定,ASR 漏词、轻声尾音、笑声掌声都不会被当空白误删;可选保留 0.25 秒呼吸口——节奏紧凑但不窒息。手调/拼接外边界不改,模型或证据异常精确回退旧逻辑 - **剪口头禅**:嗯/呃、结巴重复自动剪除(词表刻意保守),剪了什么逐条写进 clips.json - **响度标准化**:每条按 EBU R128 归一到 -14 LUFS 社媒标准,整批音量一致;跳剪后按拼接后音轨计算 - **双档人声降噪(显式开启)**:基础档沿用双高通 + 温和谱减;智能 48k 档首次按需下载约 10MB、经 SHA-256 校验的 DPDFNet2 本地模型,按声道分块增强最长 180 秒成片,视频流直拷。模型、下载或推理异常自动回退基础档;完成页与 `clips.json` 区分“智能生效 / 已回退 / 未应用”,字幕和逐字稿不改 - **音效打点**:whoosh 卡多段拼接/高潮前置的硬切缝、「叮」卡全片情绪最高点、开场钩子上屏轻响——规则打点每条最多 3 个宁缺毋滥;音效本地合成零素材零版权,想换真实音效包替换同名 wav 即可 - **背景音乐(BGM 闪避)**:选个本地音频自动循环铺满全片,音量压在人声之下、说话时自动闪避、结尾淡出;混音是独立后处理趟,视频流直拷零画质损失 - **出片自我质检 + 自我修复**:黑屏/长静音/冻结帧/主体出框/响度/时长/切点半词复核写进 clips.json;冻结和构图问题只提醒回放核对,可安全自愈的告警才当场修复,修完重检变好才采纳 - **高潮前置(cold-open)**:最炸的钩子句自动剪到开头再接正片——完播率手法,商业工具锁在付费档;定位失败自动跳过,宁可不做不可做错 - **爆点闪现(flash-forward)**:正片开始前先闪 0.3-1 秒全片最炸瞬间再切回——悬念开场手法(全网仅 0.04% 切片有 visual hook);与高潮前置自动二选一,定位失败自动跳过 - **精准切点(Paraformer 二遍对齐)**:导出前对选中片段二遍解码,一体化词级时间戳修正字幕、跳剪与切点;对齐词/内插词、匹配覆盖率与不确定区间写进回执,低匹配或异常仍自动回退 - **字幕时间质量门**:按最终跳剪/拼接后的真实时间轴检查重叠、无效时间、阅读速度、闪现短句、超宽词与估时区间;结果随每条成片写入 `clips.json`,问题可定位而不是只报一个“失败” - **质量排序智能封面**:响度峰先提名有内容的时刻,再在最终成片上本地比较清晰度、信息量、曝光、明暗跨度与转场稳定性;黑白场/模糊过渡帧先淘汰,一片多版按排名取不同封面,探测失败原样回退旧响度峰 - **帧精确切割**:快速定位+重编码,爆点第一秒不糊不偏;数小时 FLV/TS 直接进 - **硬件加速出片**:自动探测随包 ffmpeg 支持的 VideoToolbox / NVENC / QSV,H.264 重编码优先走 GPU;设备或驱动临时不可用会透明回退 x264,不拿速度换可靠性 - **导出可取消 + 实时进度**:ffmpeg 进度流式回报,随时一键取消,已完成切片保留 </details> ### 🧰 审阅与工作流:AI 粗剪,人终剪 导出前应用内看片微调:切片审阅台(波形时间轴拖手柄逐词调切点)、字幕安全区预览(九平台真实遮挡遮罩)、出片偏好记忆、品牌样式模板。 <details> <summary><b>展开细节</b>:项目工作区 · 可逆编辑 · 断点恢复 · 审阅台 · 安全区 · 发布包 · 主题系列 · 一片多版 · 品牌模板 · 双画幅 · 合集 · EDL · 设置页</summary> - **项目工作区**:多份剪辑工程集中管理,切换/重命名/关闭/删除互不串档;源素材缺失或被改动时保留工程并明确标记,重新选择文件即可安全重连,删除工程绝不删除素材 - **可逆编辑 + 专业快捷键**:选片、标题/切点、多片段调整、手动加片和逐句稿纠错都支持撤销/重做,历史随项目保存且有容量保护;`空格/K` 播放、`J/L` 前后 5 秒、`I/O` 设入出点、`[/]` 切换候选,输入框和弹窗中自动让出 - **重启接着剪**:当前素材、逐字稿、候选、勾选结果与手调切点自动保存;重开应用校验源文件后恢复到最后稳定状态,检测中/导出中等临时状态不会被错误复活 - **切片审阅台**:应用内直接播放候选,波形时间轴拖两端手柄逐词微调(自动吸附字词边界),一键还原 AI 切点;手调过的切点导出时机器不再改人的决定 - **字幕安全区预览**:一键叠加平台 UI 遮挡遮罩(抖音/快手/B站竖屏/视频号/小红书/TikTok/Reels/Shorts + 通用并集九档,按实测数据画) - **平台发布包(切完就能发)**:导出后按平台整理齐套素材到「发布包/」——每平台一个文件夹:视频(硬链接不占双份磁盘)+ 按平台画幅重裁的封面(小红书 3:4、B站 16:10、视频号 6:7)+ 按平台上限适配的文案(小红书标题硬截 20 字、话题数各按平台建议),manifest 记录适配了什么;打开文件夹逐平台上传即可 - **主题系列自动整理**:同场至少两条原版成片共享关键词时,按源片时间整理到「系列/主题/」并编号,附机器可读 manifest;变体不会被误当成剧集,视频仍优先硬链接不重复占空间 - **一片多版(多账号真差异)**:同一切片一次出 2-3 版差异化包装——不同钩子角度的贴片标题、开场悬念句、发布文案,封面自动抓下一个响度峰;多账号分发靠增量价值,不做抽帧/镜像那类已被平台判搬运的像素级去重 - **品牌样式模板**:主高亮色/字幕字号位置/logo 水印配一次存成预设,每条切片自动带上;用了什么写进回执 - **一键双画幅**:同批切片竖屏之外再出一版横屏(自动去标题贴片、字幕换底部布局),竖版发抖音、横版发B站一次搞定 - **精华合集一键成片**:整批切片流复制拼接成合 - SKILL.md (4646 bytes)
--- name: hotclip description: Turn long videos & livestream VODs into viral vertical shorts, 100% locally — on-device transcription, LLM highlight detection, 9:16 reframe with karaoke captions, and a per-clip render-QA report. Use when the user asks to clip / cut / 切片 / 剪 a long video, podcast or stream replay into short clips, find highlights / 爆点 in a video, or transcribe a media file. Footage never leaves the machine. --- # HotClip — local AI clipping pipeline HotClip(AGPL-3.0 开源)把几小时的直播回放/播客/课程切成可直接发布的竖屏短视频,全程本地运行。You drive the same pipeline the desktop app uses, via a headless CLI (or the bundled MCP server). Everything — ASR, highlight detection, cutting, caption burn-in — runs on this machine; the footage is never uploaded. ## Setup (once) 1. Repo + deps (skip what already exists): ```bash git clone https://github.com/xixihhhh/hotclip.git && cd hotclip pnpm install # ffmpeg/ffprobe are bundled npm binaries — nothing else to install ``` 2. LLM config for highlight detection, via env vars (any OpenAI-compatible endpoint): ```bash export HOTCLIP_LLM_BASE_URL=http://localhost:11434/v1 # local Ollama (no key needed) export HOTCLIP_LLM_MODEL=qwen3:8b # cloud endpoints additionally need: export HOTCLIP_LLM_API_KEY=sk-... ``` If these are missing, ask the user which LLM endpoint to use — do not guess keys. 3. First `transcribe`/`clip` run auto-downloads the ASR model (~1GB, CN mirror first). Warn the user it may take a few minutes once. ## Commands (run from the repo root) ```bash pnpm cli transcribe <video> # on-device word-level ASR (cached; instant on re-run) pnpm cli highlights <video> [--max-clips N] [--reference REF] [--json] # AI highlight candidates — review before cutting pnpm cli clip <video> [--max-clips N] [--reference REF] [--no-vertical] [--no-captions] [--out DIR] [--json] # fully managed: transcribe → detect → export + render-QA ``` `--reference <video>`: hand in a viral clip to model after — its pacing (duration / speech rate / shot-cut frequency / hook shape) is measured locally and steers candidate selection as a preference (never a hard rule). Use when the user says "切得像这条" / "learn from this clip". Input: MP4 / MKV / MOV / FLV / TS, or audio-only (podcasts get an auto-generated waveform video). Paths with spaces need quoting. ## Recommended workflow 1. **Review-first (default for interactive sessions)**: run `highlights --json`, show the user the candidates (title / score / hook / recommended), let them pick, then run `clip` — the pipeline re-detects from cache so this is cheap. For "just do it" requests, run `clip` directly. 2. **Read the output receipt**: the export folder contains one mp4 + cover JPG + `.post.txt` (publish copy) per clip, plus `clips.json` — per-clip evidence chain (`render`: what the pipeline did) and **`qa`: render-QA report** (`status: pass|warn` with issues like black frames, long silences, loudness deviation, duration mismatch, mid-word cuts, and **platform-risk words** in title/copy/captions via a local rule lint — `qa.contentHits` lists each term and where it appeared). 3. **Self-repair is automatic**: fixable warnings (leading/trailing silence or black frames → edge-trim; loudness deviation → second normalize pass) are repaired and re-checked in one pass; `qa.repair` records what was done (`actions`, `applied`). A repair is only kept when the re-check strictly improves. 4. **Surface QA warnings**: if any clip still has `qa.status === "warn"`, tell the user which clip and why; content-lint hits mean the copy/captions may be throttled or rejected by 抖音/小红书/视频号 — suggest rewording before publishing. Never silently ship a warned clip. ## Hard rules - **Never guess timestamps.** Cut points come from word-aligned transcription (`highlights` output); do not invent start/end seconds or hand-roll ffmpeg cuts when the pipeline covers it. - **Local-first**: do not upload the footage or transcript to any service beyond the user-configured LLM endpoint (which only ever receives transcript text, never media). - Exports land next to the source video in `<name>-hotclip/` unless `--out` is given; tell the user the absolute path when done. ## MCP alternative The same pipeline is exposed as a local stdio MCP server (`pnpm mcp`, tools: `clip_video` / `detect_highlights` / `transcribe_video`) — prefer it when the host supports MCP registration; the CLI is equivalent otherwise. - skills/hotclip/SKILL.md (4646 bytes)
--- name: hotclip description: Turn long videos & livestream VODs into viral vertical shorts, 100% locally — on-device transcription, LLM highlight detection, 9:16 reframe with karaoke captions, and a per-clip render-QA report. Use when the user asks to clip / cut / 切片 / 剪 a long video, podcast or stream replay into short clips, find highlights / 爆点 in a video, or transcribe a media file. Footage never leaves the machine. --- # HotClip — local AI clipping pipeline HotClip(AGPL-3.0 开源)把几小时的直播回放/播客/课程切成可直接发布的竖屏短视频,全程本地运行。You drive the same pipeline the desktop app uses, via a headless CLI (or the bundled MCP server). Everything — ASR, highlight detection, cutting, caption burn-in — runs on this machine; the footage is never uploaded. ## Setup (once) 1. Repo + deps (skip what already exists): ```bash git clone https://github.com/xixihhhh/hotclip.git && cd hotclip pnpm install # ffmpeg/ffprobe are bundled npm binaries — nothing else to install ``` 2. LLM config for highlight detection, via env vars (any OpenAI-compatible endpoint): ```bash export HOTCLIP_LLM_BASE_URL=http://localhost:11434/v1 # local Ollama (no key needed) export HOTCLIP_LLM_MODEL=qwen3:8b # cloud endpoints additionally need: export HOTCLIP_LLM_API_KEY=sk-... ``` If these are missing, ask the user which LLM endpoint to use — do not guess keys. 3. First `transcribe`/`clip` run auto-downloads the ASR model (~1GB, CN mirror first). Warn the user it may take a few minutes once. ## Commands (run from the repo root) ```bash pnpm cli transcribe <video> # on-device word-level ASR (cached; instant on re-run) pnpm cli highlights <video> [--max-clips N] [--reference REF] [--json] # AI highlight candidates — review before cutting pnpm cli clip <video> [--max-clips N] [--reference REF] [--no-vertical] [--no-captions] [--out DIR] [--json] # fully managed: transcribe → detect → export + render-QA ``` `--reference <video>`: hand in a viral clip to model after — its pacing (duration / speech rate / shot-cut frequency / hook shape) is measured locally and steers candidate selection as a preference (never a hard rule). Use when the user says "切得像这条" / "learn from this clip". Input: MP4 / MKV / MOV / FLV / TS, or audio-only (podcasts get an auto-generated waveform video). Paths with spaces need quoting. ## Recommended workflow 1. **Review-first (default for interactive sessions)**: run `highlights --json`, show the user the candidates (title / score / hook / recommended), let them pick, then run `clip` — the pipeline re-detects from cache so this is cheap. For "just do it" requests, run `clip` directly. 2. **Read the output receipt**: the export folder contains one mp4 + cover JPG + `.post.txt` (publish copy) per clip, plus `clips.json` — per-clip evidence chain (`render`: what the pipeline did) and **`qa`: render-QA report** (`status: pass|warn` with issues like black frames, long silences, loudness deviation, duration mismatch, mid-word cuts, and **platform-risk words** in title/copy/captions via a local rule lint — `qa.contentHits` lists each term and where it appeared). 3. **Self-repair is automatic**: fixable warnings (leading/trailing silence or black frames → edge-trim; loudness deviation → second normalize pass) are repaired and re-checked in one pass; `qa.repair` records what was done (`actions`, `applied`). A repair is only kept when the re-check strictly improves. 4. **Surface QA warnings**: if any clip still has `qa.status === "warn"`, tell the user which clip and why; content-lint hits mean the copy/captions may be throttled or rejected by 抖音/小红书/视频号 — suggest rewording before publishing. Never silently ship a warned clip. ## Hard rules - **Never guess timestamps.** Cut points come from word-aligned transcription (`highlights` output); do not invent start/end seconds or hand-roll ffmpeg cuts when the pipeline covers it. - **Local-first**: do not upload the footage or transcript to any service beyond the user-configured LLM endpoint (which only ever receives transcript text, never media). - Exports land next to the source video in `<name>-hotclip/` unless `--out` is given; tell the user the absolute path when done. ## MCP alternative The same pipeline is exposed as a local stdio MCP server (`pnpm mcp`, tools: `clip_video` / `detect_highlights` / `transcribe_video`) — prefer it when the host supports MCP registration; the CLI is equivalent otherwise. - src/cli/__tests__/cli.test.ts (6721 bytes)
import { describe, it, expect } from "vitest"; import { parseCliArgs } from "../index"; import { GENRE_PRESETS, GENRE_CUSTOM_MAX_CHARS } from "../../core/genre"; describe("unattended selection controls", () => { it("accepts every preset on both detection commands and keeps default inputs unchanged", () => { for (const command of ["clip", "highlights"]) { expect(parseCliArgs([command, "source.mp4"]).genre).toBeUndefined(); for (const genre of GENRE_PRESETS) { expect(parseCliArgs([command, "--genre", genre.id, "source.mp4"]).genre?.id).toBe(genre.id); } } }); it("validates genres, missing values and unsupported commands before processing", () => { for (const tail of [[], ["--json"], ["typo"]]) { expect(() => parseCliArgs(["clip", "source.mp4", "--genre", ...tail])).toThrow(/auto.*shopping.*game/); } expect(() => parseCliArgs(["clip", "source.mp4", "--genre-custom", "--json"])).toThrow(/文本/); expect(() => parseCliArgs(["transcribe", "source.mp4", "--genre", "game"])).toThrow(/仅用于/); }); it("keeps custom criteria independent of option order and bounds their length", () => { for (const flags of [["--genre", "game", "--genre-custom", " 只选翻盘 "], ["--genre-custom", " 只选翻盘 ", "--genre", "game"]]) { expect(parseCliArgs(["clip", "source.mp4", ...flags]).genre).toEqual({ id: "game", custom: "只选翻盘" }); } expect(parseCliArgs(["clip", "source.mp4", "--genre-custom", " "]).genre).toEqual({ id: "auto", custom: undefined }); expect(parseCliArgs(["clip", "source.mp4", "--genre-custom", "字".repeat(2000)]).genre?.custom).toHaveLength(GENRE_CUSTOM_MAX_CHARS); }); it("accepts the 1–2 minute preset with ten clips and rejects invalid lengths", () => { expect(parseCliArgs(["clip", "source.mp4", "--clip-length", "extended", "--max-clips", "10"])) .toMatchObject({ clipLength: "extended", maxClips: 10 }); for (const value of ["", "120", "--json", "toString"]) { expect(() => parseCliArgs(["highlights", "source.mp4", "--clip-length", value])).toThrow(/short.*extended/); } }); }); describe("parseCliArgs (CLI 参数解析)", () => { it("accepts an explicit subtitle track for all transcript-consuming commands", () => { for (const command of ["transcribe", "highlights", "clip"]) { expect(parseCliArgs([command, "--subtitles", "/v/原文.srt", "/v/source.mp4", "--json"])) .toMatchObject({ videoPath: "/v/source.mp4", subtitlePath: "/v/原文.srt", json: true }); } expect(() => parseCliArgs(["clip", "/v/source.mp4", "--subtitles"])).toThrow(/SRT/); expect(() => parseCliArgs(["clip", "/v/source.mp4", "--subtitles", "--json"])).toThrow(/SRT/); expect(() => parseCliArgs(["doctor", "--subtitles", "/v/原文.srt"])).toThrow(/仅用于/); }); it("clip 全默认:竖屏+字幕开,视频路径为首个非选项参数", () => { const a = parseCliArgs(["clip", "/v/直播回放.mp4"]); expect(a).toMatchObject({ command: "clip", videoPath: "/v/直播回放.mp4", vertical: true, captions: true, autoEnhance: false, json: false, }); }); it("--auto-enhance is explicit and defaults off", () => { expect(parseCliArgs(["clip", "/v/a.mp4"]).autoEnhance).toBe(false); expect(parseCliArgs(["clip", "/v/a.mp4", "--auto-enhance"]).autoEnhance).toBe(true); expect(parseCliArgs(["clip", "/v/a.mp4", "--denoise"]).denoiseMode).toBe("basic"); expect(parseCliArgs(["clip", "/v/a.mp4", "--smart-denoise"]).denoiseMode).toBe("smart"); }); it("开关与带值选项:--no-vertical / --no-captions / --max-clips / --out / --json", () => { const a = parseCliArgs(["clip", "/v/a.mp4", "--no-vertical", "--no-captions", "--max-clips", "3", "--out", "/tmp/o", "--json"]); expect(a.vertical).toBe(false); expect(a.captions).toBe(false); expect(a.maxClips).toBe(3); expect(a.outDir).toBe("/tmp/o"); expect(a.json).toBe(true); }); it("--max-clips 钳到 1..12(与 MCP 同一约束)", () => { expect(parseCliArgs(["clip", "/v/a.mp4", "--max-clips", "99"]).maxClips).toBe(12); expect(parseCliArgs(["clip", "/v/a.mp4", "--max-clips", "0"]).maxClips).toBe(1); }); it("选项顺序无关:选项可在路径之前", () => { const a = parseCliArgs(["highlights", "--max-clips", "5", "/v/a.mp4"]); expect(a.videoPath).toBe("/v/a.mp4"); expect(a.maxClips).toBe(5); }); it("--reference 带值解析;缺值抛错", () => { const a = parseCliArgs(["highlights", "/v/a.mp4", "--reference", "/v/对标爆款.mp4"]); expect(a.referencePath).toBe("/v/对标爆款.mp4"); expect(parseCliArgs(["clip", "/v/a.mp4"]).referencePath).toBeUndefined(); expect(() => parseCliArgs(["clip", "/v/a.mp4", "--reference"])).toThrow(/对标视频路径/); }); it("doctor 不需要视频路径,--download 可选", () => { expect(parseCliArgs(["doctor"])).toMatchObject({ command: "doctor", download: false }); expect(parseCliArgs(["doctor", "--download"]).download).toBe(true); }); it("发布表现反馈:import 需要文件,report 不需要路径", () => { expect(parseCliArgs(["feedback", "/data/bilibili.csv"])).toMatchObject({ command: "feedback", videoPath: "/data/bilibili.csv", }); expect(parseCliArgs(["feedback-report", "--json"])).toMatchObject({ command: "feedback-report", videoPath: "", json: true, }); expect(() => parseCliArgs(["feedback"])).toThrow(/数据文件路径/); }); it("缺命令/缺路径/未知选项/非法数值 → 抛出含用法的错误", () => { expect(() => parseCliArgs([])).toThrow(/用法/); expect(() => parseCliArgs(["clip"])).toThrow(/缺少视频路径/); expect(() => parseCliArgs(["clip", "/v/a.mp4", "--wat"])).toThrow(/未知选项/); expect(() => parseCliArgs(["clip", "/v/a.mp4", "--max-clips", "abc"])).toThrow(/数字/); }); it("-h / --help → 输出用法", () => { expect(() => parseCliArgs(["--help"])).toThrow(/pnpm cli clip/); }); }); describe("local speech engine options", () => { it("parses the optional service and explicit restart", () => { expect(parseCliArgs(["transcribe", "source.mp4", "--engine", "qwen3", "--asr-url", "http://127.0.0.1:8766", "--restart-transcription"])).toMatchObject({ engineId: "qwen3", localServiceUrl: "http://127.0.0.1:8766", restart: true }); }); it("rejects unsupported engines and remote service URLs", () => { expect(() => parseCliArgs(["transcribe", "source.mp4", "--engine", "unknown"])).toThrow(); expect(() => parseCliArgs(["transcribe", "source.mp4", "--asr-url", "https://example.com"])).toThrow(); }); }); - src/cli/index.ts (19045 bytes)
import { localSpeechUrl } from "../core/transcribe/qwen-local"; /** * HotClip Headless CLI —— 不开桌面端,终端或 Coding Agent 直接驱动本地 * 切片管线(与桌面端/MCP/录播监听共用 core/pipeline,产物完全一致): * * pnpm cli transcribe <视频> 端侧逐字转写(带缓存) * pnpm cli highlights <视频> [--max-clips N] AI 找爆点(候选 JSON) * pnpm cli clip <视频> [选项] 全托管:转写→找爆点→出片+质检 * * LLM 配置走环境变量(与 MCP Server 同一套):HOTCLIP_LLM_BASE_URL / * HOTCLIP_LLM_MODEL / HOTCLIP_LLM_API_KEY(本地 Ollama 免 key)。 * 模型与转写缓存和桌面 App 共享——下载一次三边都能用。 */ import { join, basename } from "path"; import { transcribeCached, detectForPipeline, autoClip, analyzeReferenceVideo } from "../core/pipeline"; import type { ReferenceProfile } from "../core/reference"; import { loadGlossary } from "../core/glossary-store"; import { describeSubtitleImportError } from "../shared/subtitle-import"; import { userDataDir, modelsRoot, cacheDir, renderCacheDir, evidenceCacheDir, llmFromEnv } from "../core/appenv"; import { runDoctor } from "../core/doctor"; import { ensureModel } from "../core/models"; import { loadReviewMemory } from "../core/review-memory"; import { importPerformanceFile, loadPerformanceMemory, performanceReport } from "../core/performance-memory"; import { parseGenre } from "../core/genre"; import { isClipLength } from "../shared/detection-options"; import type { DetectionOptions } from "../shared/api-types"; const USAGE = `HotClip CLI —— 本地 AI 切片,素材不出电脑 用法: pnpm cli transcribe <视频路径> 本地逐字转写(默认 SenseVoice,自动续跑已完成的识别段) --engine sensevoice|paraformer|fireredasr|qwen3 --asr-url http://127.0.0.1:8766 可选 Qwen3 本地服务 --restart-transcription 丢弃当前引擎的分段进度并重做 transcribe / highlights / clip 均支持 --subtitles <原文.srt|原文.vtt> 显式导入已有 UTF-8 字幕,跳过 ASR;字词时间为估算,需复核 pnpm cli highlights <视频路径> [--max-clips N] [--reference 对标视频] [--json] AI 通读全文找爆点,输出候选清单(评分/钩子/切点,先审后剪) --reference: 丢一条想对标的爆款切片,实测其节奏(时长/语速/镜头/钩子) 生成画像,选段向对标节奏靠拢(偏好不是硬约束) pnpm cli clip <视频路径> [--max-clips N] [--reference 对标视频] [--no-vertical] [--no-captions] [--auto-enhance] [--denoise|--smart-denoise] [--out 目录] [--json] 全托管一条龙:转写 → 找爆点 → 出片(竖屏/字幕/跳剪/响度默认全开) + 出片质检(黑屏/长静音/响度/时长/切点/平台违禁词复核),报告进 clips.json + 可自愈告警自动修复(首尾静音黑屏裁边/响度重归一,修复记录进 qa.repair) --auto-enhance: 本地测量保留画面,仅在明显偏暗/灰/过饱和时克制校正(默认关闭) --denoise: 基础固定滤镜降噪;--smart-denoise: 48kHz 本地模型增强人声,失败回退基础档 highlights / clip 共用选项: --genre <id> 直播品类;默认 auto,非法值会列出全部可选项 --genre-custom <文本> 自定义判据,非空时覆盖所选品类的内置判据 --clip-length <档位> short=10-30s / standard=8-40s / long=40-90s / extended=60-120s --max-clips N 最多 1-12 条(默认 6);不足时保留实际候选,不凑数 pnpm cli doctor [--download] 环境自检:ffmpeg/模型安装状态/LLM 端点/磁盘/缓存,给出修复建议 --download: 把默认管线要用的模型现在预下载好(断点续传,断网重跑接着下) pnpm cli feedback <平台导出的 CSV 或 JSON> 导入真实播放/点赞/评论/分享/收藏数据,本地学习高低表现模式 支持中英文字段名与 1.2万/7.8k 等数字格式;同平台同视频重复导入会更新 pnpm cli feedback-report [--json] 查看 HotClip 已学到的高/低表现样例;之后桌面/CLI/MCP/监听找爆点都会使用 环境变量(highlights / clip 需要): HOTCLIP_LLM_BASE_URL OpenAI 兼容端点(本地 Ollama: http://localhost:11434/v1) HOTCLIP_LLM_MODEL 模型名(如 qwen3:8b) HOTCLIP_LLM_API_KEY 云端接口的 key(本地 Ollama 可省)`; /** 极简参数解析:布尔开关 + 带值选项,首个非选项参数是视频路径。 */ export interface CliArgs extends DetectionOptions { command: string; videoPath: string; vertical: boolean; captions: boolean; autoEnhance: boolean; denoiseMode?: "basic" | "smart"; outDir?: string; /** 对标爆款视频路径(参考画像驱动选段)。 */ referencePath?: string; subtitlePath?: string; engineId?: string; localServiceUrl?: string; restart?: boolean; json: boolean; /** doctor 专用:把缺失的默认管线模型现在预下载好。 */ download: boolean; } export function parseCliArgs(argv: string[]): CliArgs { const [command, ...rest] = argv; if (!command || command === "-h" || command === "--help") { throw new Error(USAGE); } const args: CliArgs = { command, videoPath: "", vertical: true, captions: true, autoEnhance: false, json: false, download: false }; for (let i = 0; i < rest.length; i++) { const a = rest[i]; if (a === "--no-vertical") args.vertical = false; else if (a === "--no-captions") args.captions = false; else if (a === "--auto-enhance") args.autoEnhance = true; else if (a === "--denoise") args.denoiseMode = "basic"; else if (a === "--smart-denoise") args.denoiseMode = "smart"; else if (a === "--json") args.json = true; else if (a === "--download") args.download = true; else if (a === "--restart-transcription") args.restart = true; else if (a === "--genre") { const value = rest[++i]; args.genre = parseGenre(!value || value.startsWith("--") ? "" : value, args.genre?.custom); } else if (a === "--genre-custom") { const value = rest[++i]; if (value === undefined || value.startsWith("--")) throw new Error("--genre-custom 需要一段判据文本"); args.genre = parseGenre(args.genre?.id, value); } else if (a === "--clip-length") { const value = rest[++i]; if (!isClipLength(value)) throw new Error("--clip-length: short | standard | long | extended (60-120s)"); args.clipLength = value; } else if (a === "--engine") { const value = rest[++i]; if (!["sensevoice", "paraformer", "fireredasr", "qwen3"].includes(value)) throw new Error("--engine: sensevoice | paraformer | fireredasr | qwen3"); args.engineId = value; } else if (a === "--asr-url") { const value = rest[++i]; if (!value) throw new Error("--asr-url requires a loopback URL"); args.localServiceUrl = localSpeechUrl(value); } else if (a === "--max-clips") { const v = Number(rest[++i]); if (!Number.isFinite(v)) throw new Error("--max-clips 需要一个数字"); args.maxClips = Math.max(1, Math.min(12, Math.round(v))); } else if (a === "--out") { const v = rest[++i]; if (!v) throw new Error("--out 需要一个目录路径"); args.outDir = v; } else if (a === "--reference") { const v = rest[++i]; if (!v) throw new Error("--reference 需要一个对标视频路径"); args.referencePath = v; } else if (a === "--subtitles") { const v = rest[++i]; if (!v?.trim() || v.startsWith("--")) throw new Error("--subtitles 需要一个 SRT 或 WebVTT 文件路径"); args.subtitlePath = v; } else if (a.startsWith("--")) { throw new Error(`未知选项: ${a}\n\n${USAGE}`); } else if (!args.videoPath) { args.videoPath = a; } } // doctor/feedback-report 不吃路径;feedback 的位置参数是指标文件路径 if (!args.videoPath && !["doctor", "feedback-report"].includes(args.command)) throw new Error(`缺少视频路径或数据文件路径\n\n${USAGE}`); if (args.subtitlePath && !["transcribe", "highlights", "clip"].includes(args.command)) throw new Error("--subtitles 仅用于 transcribe / highlights / clip"); if ((args.genre || args.clipLength) && !["highlights", "clip"].includes(args.command)) throw new Error("--genre / --genre-custom / --clip-length 仅用于 highlights / clip"); return args; } function fmtClock(sec: number): string { const m = Math.floor(sec / 60); return `${String(m).padStart(2, "0")}:${String(Math.floor(sec % 60)).padStart(2, "0")}`; } async function main(signal?: AbortSignal): Promise<void> { const args = parseCliArgs(process.argv.slice(2)); if (args.command === "doctor") { // LLM 未配置不是错误(transcribe 用不到),按"未配置"呈现 let llm = null; try { llm = llmFromEnv(); } catch { // 保持 null } const report = await runDoctor({ modelsRoot: modelsRoot(), cacheDir: cacheDir(), renderCacheDir: renderCacheDir(), evidenceCacheDir: evidenceCacheDir(), llm }); const icon = { ok: "✅", warn: "⚠️", fail: "❌" } as const; for (const c of report.checks) { process.stdout.write(`${icon[c.status]} ${c.name}:${c.detail}\n`); if (c.fix) process.stdout.write(` ↳ ${c.fix}\n`); } if (args.download && report.missingCoreModels.length > 0) { const mb = (n: number): number => Math.round(n / (1024 * 1024)); for (const asset of report.missingCoreModels) { await ensureModel(modelsRoot(), asset, (p) => { const pct = Math.min(100, Math.round((p.downloadedBytes / p.totalBytes) * 100)); const verb = p.phase === "extract" ? "解压" : "下载"; process.stderr.write(`\r${verb} ${asset.id}:${pct}%(${mb(p.downloadedBytes)}/${mb(p.totalBytes)}MB) `); }); process.stderr.write(`\r✅ ${asset.id} 已就绪${" ".repeat(24)}\n`); } process.stdout.write("默认管线模型全部就绪。\n"); } else if (report.missingCoreModels.length > 0) { process.stdout.write(`有 ${report.missingCoreModels.length} 个默认管线模型未安装,可加 --download 现在下好。\n`); } if (report.checks.some((c) => c.status === "fail")) process.exitCode = 1; return; } if (args.command === "feedback") { const result = await importPerformanceFile(userDataDir(), args.videoPath); process.stdout.write(`已导入 ${result.imported} 条发布表现,跳过 ${result.skipped} 条无效记录;本地共学习 ${result.total} 条。\n`); process.stdout.write(`${performanceReport(result.entries)}\n`); return; } if (args.command === "feedback-report") { const entries = await loadPerformanceMemory(userDataDir()); process.stdout.write(args.json ? `${JSON.stringify(entries, null, 2)}\n` : `${performanceReport(entries)}\n`); return; } const glossary = await loadGlossary(userDataDir()); // 参考画像:用户显式给的输入,分析失败要说清并按无参考继续(不静默丢) const loadReference = async (): Promise<ReferenceProfile | undefined> => { if (!args.referencePath) return undefined; process.stderr.write("分析对标视频节奏…\n"); try { const p = await analyzeReferenceVideo(args.referencePath, { modelsRoot: modelsRoot(), cacheDir: cacheDir(), evidenceCacheDir: evidenceCacheDir(), glossary, signal, }); const cuts = p.cutsPerMin !== null ? `·镜头 ${p.cutsPerMin} 切/分` : ""; const unit = p.zh ? "字" : "词"; process.stderr.write( `参考画像:时长 ${Math.round(p.durationSec)}s·语速 ${p.speechRate}${unit}/秒·句长 ${p.avgSentenceLen}${unit}${cuts}·钩子「${p.hookLine.slice(0, 20)}」\n` ); return p; } catch (e) { process.stderr.write(`⚠ 对标视频分析失败,按无参考继续:${e instanceof Error ? e.message : String(e)}\n`); return undefined; } }; if (args.command === "transcribe") { const t = await transcribeCached(args.videoPath, modelsRoot(), cacheDir(), glossary, signal, args.subtitlePath, { engineId: args.engineId, localServiceUrl: args.localServiceUrl, restart: args.restart }); if (args.json) process.stdout.write(`${JSON.stringify(t, null, 2)}\n`); else for (const s of t.segments) process.stdout.write(`[${fmtClock(s.startSec)}] ${s.text}\n`); if (args.subtitlePath) process.stderr.write("已导入字幕;句内字词时间为估算,请复核字幕时间与自动切点。\n"); process.stderr.write(`语言:${t.language} 时长:${fmtClock(t.durationSec)} 共 ${t.segments.length} 句\n`); return; } if (args.command === "highlights") { const llm = llmFromEnv(); const reference = await loadReference(); process.stderr.write(args.subtitlePath ? "导入字幕中(字词时间为估算)…\n" : "转写中(带缓存)…\n"); const transcript = await transcribeCached(args.videoPath, modelsRoot(), cacheDir(), glossary, signal, args.subtitlePath, { engineId: args.engineId, localServiceUrl: args.localServiceUrl, restart: args.restart }); process.stderr.write("AI 找爆点中…\n"); const candidates = await detectForPipeline(args.videoPath, transcript, { modelsRoot: modelsRoot(), evidenceCacheDir: evidenceCacheDir(), llm, maxClips: args.maxClips, genre: args.genre, clipLength: args.clipLength, reference, // 桌面审阅台积累的本机偏好,CLI 检测同享(只读) reviewMemory: await loadReviewMemory(userDataDir()), performanceMemory: await loadPerformanceMemory(userDataDir()), signal, }); if (candidates.length === 0) { process.stderr.write("没有找到值得切的爆点候选。\n"); return; } const rows = candidates.map((c) => ({ id: c.id, start: fmtClock(c.startSec), end: fmtClock(c.endSec), startSec: c.startSec, endSec: c.endSec, title: c.title, hook: c.hook, score: c.score, reason: c.reason, recommended: c.recommended, reviewNote: c.reviewNote || undefined, visualEvidence: c.visualEvidence, })); if (args.json) { process.stdout.write(`${JSON.stringify(rows, null, 2)}\n`); } else { for (const r of rows) { const mark = r.recommended ? "✅" : "⚠️ 不建议发布"; process.stdout.write(`#${r.id} [${r.start}-${r.end}] 评分 ${r.score} ${mark}\n ${r.title}\n 钩子:${r.hook}\n`); } } return; } if (args.command === "clip") { const llm = llmFromEnv(); const reference = await loadReference(); const outcome = await autoClip(args.videoPath, { modelsRoot: modelsRoot(), cacheDir: cacheDir(), renderCacheDir: renderCacheDir(), evidenceCacheDir: evidenceCacheDir(), llm, maxClips: args.maxClips, genre: args.genre, clipLength: args.clipLength, vertical: args.vertical, captions: args.captions, autoEnhance: args.autoEnhance, denoiseMode: args.denoiseMode, subtitlePath: args.subtitlePath, asr: { engineId: args.engineId, localServiceUrl: args.localServiceUrl, restart: args.restart }, signal, outDir: args.outDir, reference, reviewMemory: await loadReviewMemory(userDataDir()), performanceMemory: await loadPerformanceMemory(userDataDir()), fontsDir: join(__dirname, "..", "..", "resources", "fonts"), glossary, onStage: (stage) => { const label = { transcribing: args.subtitlePath ? "导入字幕中(字词时间为估算)…" : "转写中(带缓存)…", detecting: "AI 找爆点中…", exporting: "出片中…" }[stage]; process.stderr.write(`${label}\n`); }, }); if (outcome.exported.length === 0) { process.stderr.write("AI 复评后没有建议发布的切片(候选都被判定为弱钩子)。可用 highlights 命令查看全部候选与复评意见。\n"); return; } if (args.json) { process.stdout.write( `${JSON.stringify( { outDir: outcome.outDir, clips: outcome.exported.map((r) => ({ file: basename(r.path), path: r.path, durationSec: Math.round(r.durationSec * 1000) / 1000, colorConverted: Boolean(r.colorConverted), colorConversionSkipped: Boolean(r.colorConversionSkipped), colorInspectionFailed: Boolean(r.colorInspectionFailed), audioEnhancement: r.audioEnhancement ?? null, qa: r.qa ?? null, })), }, null, 2 )}\n` ); return; } process.stdout.write(`已导出 ${outcome.exported.length} 条切片到 ${outcome.outDir}\n`); for (const r of outcome.exported) { const c = outcome.candidates.find((x) => x.id === r.id); const colorNote = r.colorConverted ? " · HDR→SDR" : r.colorConversionSkipped ? " · HDR 色彩路径不支持,未转换" : r.colorInspectionFailed - src/core/__tests__/align.smoke.test.ts (3074 bytes)
/** * 精准切点真模型冒烟(env 门控,CI 不跑): * HOTCLIP_ALIGN_SMOKE=1 pnpm vitest run src/core/__tests__/align.smoke.test.ts * 流程:macOS say 合成中文语音 → SenseVoice(本机已装)转写拿词表 → * 人为整体平移 +0.6s 模拟「反向对齐不准」→ createClipAligner 二遍对齐 → * 断言词表被拉回原位附近。首次运行会把 Paraformer 模型(~240MB)下载到 * 应用模型目录(与桌面端共用,不白下)。 */ import { describe, it, expect } from "vitest"; import { execFileSync } from "child_process"; import { homedir, tmpdir } from "os"; import { join } from "path"; import { rmSync } from "fs"; const RUN = process.env.HOTCLIP_ALIGN_SMOKE === "1" && process.platform === "darwin"; describe.runIf(RUN)("align 真模型冒烟", () => { it("平移过的词表被 Paraformer 二遍对齐拉回原位", { timeout: 15 * 60_000 }, async () => { const { SenseVoiceEngine } = await import("../transcribe/sensevoice"); const { createClipAligner } = await import("../align"); const modelsRoot = join(homedir(), "Library/Application Support/hotclip/models"); // 1) 合成 ~15s 中文语音(带一句停顿制造多段词) const wav = join(tmpdir(), "hotclip-align-smoke.wav"); execFileSync("say", [ "-v", "Tingting", "-o", wav, "--data-format=LEI16@16000", "今天给大家推荐一款产品,原价一百九十九,今天直播间只要九十九。用过的都说好,库存只剩最后两百件,喜欢的抓紧下单。", ]); // 2) SenseVoice 转写拿词表(本机已装,不触发下载) const sv = new SenseVoiceEngine(modelsRoot); const transcript = await sv.transcribe(wav); const words = transcript.segments.flatMap((s) => s.words); expect(words.length).toBeGreaterThan(10); const origFirstStart = words[0].startSec; // 3) 整体平移 +0.6s 模拟不准的词表 const shifted = words.map((w) => ({ ...w, startSec: w.startSec + 0.6, endSec: w.endSec + 0.6 })); // 4) 二遍对齐(首次会下载 Paraformer ~240MB) const align = createClipAligner(modelsRoot); const refined = await align(wav, { startSec: 0, endSec: transcript.durationSec, words: shifted, }); expect(refined).not.toBeNull(); // 对齐后首词回到原位 ±0.3s(Paraformer CIF 时间戳应与 SenseVoice 原始 // 时间大致一致,而与 +0.6s 的平移版明显不同) expect(Math.abs(refined!.words[0].startSec - origFirstStart)).toBeLessThan(0.3); expect(Math.abs(refined!.words[0].startSec - shifted[0].startSec)).toBeGreaterThan(0.25); expect(refined!.report.matchedFrac).toBeGreaterThanOrEqual(0.5); // 单调无倒流 for (let i = 1; i < refined!.words.length; i++) { expect(refined!.words[i].startSec).toBeGreaterThanOrEqual(refined!.words[i - 1].endSec - 1e-3); } rmSync(wav, { force: true }); }); }); describe.runIf(!RUN)("align 真模型冒烟(跳过)", () => { it("需要 HOTCLIP_ALIGN_SMOKE=1 且 macOS 才运行", () => { expect(true).toBe(true); }); }); - src/core/__tests__/align.test.ts (3455 bytes)
import { describe, it, expect } from "vitest"; import { refineWordTimings, toAlignUnits, ALIGN_MIN_MATCH_FRAC } from "../align"; import type { TranscriptWord } from "../../shared/api-types"; function w(text: string, startSec: number, endSec: number): TranscriptWord { return { text, startSec, endSec }; } describe("toAlignUnits(归一化对齐单元)", () => { it("CJK 逐字、拉丁/数字逐字符,标点空白丢弃,记录归属下标", () => { const units = toAlignUnits([{ text: "你好," }, { text: "AI 99!" }]); expect(units.map((u) => u.ch).join("")).toBe("你好ai99"); expect(units.map((u) => u.idx)).toEqual([0, 0, 1, 1, 1, 1]); }); }); describe("refineWordTimings(二遍对齐时间重映射)", () => { it("全文匹配:词表整体采纳参考时间(文本原样保留)", () => { // 主转写时间整体偏了 0.5s;参考流是逐字 token 带准确时间 const words = [w("今天", 10.5, 11.1), w("只要", 11.1, 11.7), w("九块九", 11.7, 12.6)]; const ref = [ w("今", 10.0, 10.3), w("天", 10.3, 10.6), w("只", 10.6, 10.9), w("要", 10.9, 11.2), w("九", 11.2, 11.5), w("块", 11.5, 11.8), w("九", 11.8, 12.1), ]; const res = refineWordTimings(words, ref); expect(res.matchedFrac).toBe(1); expect(res.words.map((x) => x.text)).toEqual(["今天", "只要", "九块九"]); expect(res.words[0].startSec).toBeCloseTo(10.0, 5); expect(res.words[0].endSec).toBeCloseTo(10.6, 5); expect(res.words[2].startSec).toBeCloseTo(11.2, 5); expect(res.words[2].endSec).toBeCloseTo(12.1, 5); expect(res.words.every((x) => x.timingSource === "aligned")).toBe(true); expect(res.alignedWords).toBe(3); }); it("参考流有幻觉/漏字仍能对齐,未命中词内插进前后锚点", () => { const words = [w("你好", 10, 10.6), w("世界", 10.6, 11.2), w("再见", 11.2, 11.8)]; // 参考流:「世界」没识别出来,但「你好」「再见」时间准确 const ref = [w("你", 9.5, 9.8), w("好", 9.8, 10.1), w("再", 11.5, 11.8), w("见", 11.8, 12.1)]; const res = refineWordTimings(words, ref); expect(res.words[0].startSec).toBeCloseTo(9.5, 5); expect(res.words[2].startSec).toBeCloseTo(11.5, 5); // 「世界」内插在 10.1(前锚点尾)与 11.5(后锚点头)之间且单调 expect(res.words[1].startSec).toBeGreaterThanOrEqual(10.1 - 1e-6); expect(res.words[1].endSec).toBeLessThanOrEqual(11.5 + 1e-6); expect(res.matchedFrac).toBeCloseTo(4 / 6, 5); expect(res.words[1].timingSource).toBe("interpolated"); expect(res.interpolatedWords).toBe(1); }); it("完全对不上:matchedFrac 低于门槛(调用方回退)", () => { const words = [w("完全", 0, 0.5), w("无关", 0.5, 1)]; const ref = [w("音", 0, 0.3), w("乐", 0.3, 0.6)]; const res = refineWordTimings(words, ref); expect(res.matchedFrac).toBeLessThan(ALIGN_MIN_MATCH_FRAC); }); it("重映射后时间严格单调、无零长词", () => { const words = [w("啊", 5, 5.1), w("这个", 5.1, 5.5), w("产品", 5.5, 6)]; const ref = [w("这", 4.0, 4.2), w("个", 4.2, 4.4), w("产", 4.4, 4.6), w("品", 4.6, 4.8)]; const res = refineWordTimings(words, ref); for (let i = 0; i < res.words.length; i++) { expect(res.words[i].endSec).toBeGreaterThan(res.words[i].startSec); if (i > 0) expect(res.words[i].startSec).toBeGreaterThanOrEqual(res.words[i - 1].endSec - 1e-3); } }); }); - src/core/__tests__/analysis-video.integration.test.ts (3359 bytes)
import { execFile } from "child_process"; import { mkdtemp, rm, writeFile } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; import { promisify } from "util"; import { describe, expect, it } from "vitest"; import { resolveFfmpegPath } from "../binaries"; import { planColorRender } from "../color"; import { composeContactSheetJpeg } from "../contact-sheet"; import { runFfmpeg } from "../cut"; import { probeMedia } from "../probe"; const execFileAsync = promisify(execFile); async function centerRgb(jpegPath: string): Promise<[number, number, number]> { const { stdout } = await execFileAsync( resolveFfmpegPath(), [ "-hide_banner", "-v", "error", "-i", jpegPath, "-vf", "scale=1:1", "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "rgb24", "-", ], { encoding: "buffer", maxBuffer: 1024 } ); return [stdout[0] ?? 0, stdout[1] ?? 0, stdout[2] ?? 0]; } describe("analysis picture FFmpeg integration", () => { it("uses the selected HDR video stream and emits a viewable SDR contact sheet", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-analysis-video-")); const source = join(root, "dual-video.mkv"); const selectedJpeg = join(root, "selected.jpg"); const otherJpeg = join(root, "other.jpg"); try { await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", "color=c=red:size=160x90:rate=24:duration=1", "-f", "lavfi", "-i", "color=c=blue:size=320x180:rate=24:duration=1", "-map", "0:v:0", "-map", "1:v:0", "-c:v", "ffv1", "-level:v:0", "3", "-level:v:1", "3", "-pix_fmt:v:0", "yuv420p", "-color_primaries:v:0", "bt709", "-color_trc:v:0", "bt709", "-colorspace:v:0", "bt709", "-color_range:v:0", "tv", "-pix_fmt:v:1", "yuv420p10le", "-color_primaries:v:1", "bt2020", "-color_trc:v:1", "smpte2084", "-colorspace:v:1", "bt2020nc", "-color_range:v:1", "tv", "-disposition:v:0", "0", "-disposition:v:1", "default", "-shortest", source, ]); const media = await probeMedia(source); expect(media.videoStreamIndex).toBe(1); const color = planColorRender(media); expect(color.action).toBe("tonemap-bt709"); const selected = await composeContactSheetJpeg(source, [0.5], { videoStreamIndex: media.videoStreamIndex, color, }); const other = await composeContactSheetJpeg(source, [0.5], { videoStreamIndex: 0 }); expect(selected).not.toBeNull(); expect(other).not.toBeNull(); await writeFile(selectedJpeg, Buffer.from(selected!, "base64")); await writeFile(otherJpeg, Buffer.from(other!, "base64")); const [selectedRed, , selectedBlue] = await centerRgb(selectedJpeg); const [otherRed, , otherBlue] = await centerRgb(otherJpeg); // Pure synthetic PQ primaries can clip differently across FFmpeg builds, // but the selected blue track must still retain substantially more blue // than the unselected red SDR track after the SDR preview conversion. expect(selectedBlue).toBeGreaterThan(otherBlue + 40); expect(Math.max(selectedRed, selectedBlue)).toBeGreaterThan(40); expect(otherRed).toBeGreaterThan(otherBlue); } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); }); - src/core/__tests__/appenv-outdir.test.ts (1741 bytes)
/** * 导出位置(issue #3):用户改过就落他选的地方,没改过落系统「影片/HotClip」。 * 这条链路错了成片就"消失"——用户根本不知道该去哪儿找,所以边界值全钉死。 */ import { describe, expect, it } from "vitest"; import { join } from "path"; import { clipOutDir } from "../appenv"; describe("clipOutDir", () => { const videos = join("/Users", "duan", "Movies"); it("没选过导出位置 → 系统影片目录下的 HotClip/<片名>", () => { expect(clipOutDir(undefined, videos, "直播回放")).toBe(join(videos, "HotClip", "直播回放")); expect(clipOutDir(null, videos, "直播回放")).toBe(join(videos, "HotClip", "直播回放")); }); it("选过导出位置 → 落用户选的根目录下的 <片名>,不再拼 HotClip 一层", () => { const chosen = join("/Users", "duan", "Desktop", "我的成片"); expect(clipOutDir(chosen, videos, "直播回放")).toBe(join(chosen, "直播回放")); }); it("空串/纯空白视为没选过(旧偏好档里的脏值不能把成片扔到根目录)", () => { expect(clipOutDir("", videos, "片名")).toBe(join(videos, "HotClip", "片名")); expect(clipOutDir(" ", videos, "片名")).toBe(join(videos, "HotClip", "片名")); }); it("选择路径首尾空格被裁掉(拖拽/粘贴路径常带)", () => { expect(clipOutDir(" /tmp/out ", videos, "片名")).toBe(join("/tmp/out", "片名")); }); it("片名照原样落一层子目录,不同素材互不覆盖", () => { const a = clipOutDir(undefined, videos, "第一场"); const b = clipOutDir(undefined, videos, "第二场"); expect(a).not.toBe(b); expect(a.endsWith(join("HotClip", "第一场"))).toBe(true); }); }); - src/core/__tests__/atomic-output.integration.test.ts (4166 bytes)
import { mkdtemp, readFile, readdir, rm, writeFile } from "fs/promises"; import { join, relative } from "path"; import { tmpdir } from "os"; import { afterEach, describe, expect, it } from "vitest"; import { withAtomicOutput } from "../atomic-output"; import { cutClip, cutJumpClip, concatClips, runFfmpeg } from "../cut"; import { probeMedia } from "../probe"; import { runAudiogram, audiogramSpec } from "../audiogram"; const folders: string[] = []; afterEach(async () => { await Promise.all(folders.splice(0).map((path) => rm(path, { recursive: true, force: true }))); }); async function workspace(): Promise<string> { const folder = await mkdtemp(join(tmpdir(), "hotclip-atomic-test-")); folders.push(folder); return folder; } describe("safe media publication", () => { it("preserves existing output on a failed, empty, or cancelled producer and removes only its own workspace", async () => { const folder = await workspace(); const output = join(folder, "成片.mp4"); const existing = Buffer.from("existing completed output"); await writeFile(output, existing); await writeFile(join(folder, "成片.mp4.list.txt"), "user notes"); await expect(withAtomicOutput(output, async (path) => { await writeFile(path, "incomplete"); throw new Error("encoder failed"); })).rejects.toThrow("encoder failed"); await expect(withAtomicOutput(output, (path) => writeFile(path, ""))).rejects.toThrow("empty"); const controller = new AbortController(); await expect(withAtomicOutput(output, async (path) => { await writeFile(path, "completed but cancelled"); controller.abort(new Error("cancelled")); }, controller.signal)).rejects.toThrow("cancelled"); expect(await readFile(output)).toEqual(existing); expect((await readdir(folder)).sort()).toEqual(["成片.mp4", "成片.mp4.list.txt"]); }); it("waits for a real FFmpeg cancellation before removing its output, preserving the old file", async () => { const folder = await workspace(); const output = join(folder, "existing.mp4"); await writeFile(output, "previous output"); const controller = new AbortController(); let sawProgress = false; await expect(withAtomicOutput(output, (path) => runFfmpeg([ "-hide_banner", "-y", "-re", "-f", "lavfi", "-i", "color=size=96x64:rate=10", "-t", "20", "-c:v", "libx264", "-pix_fmt", "yuv420p", path, ], { signal: controller.signal, onTimeSec: () => { sawProgress = true; controller.abort(new Error("cancelled")); } }), controller.signal)).rejects.toThrow("cancelled"); expect(sawProgress).toBe(true); expect(await readFile(output, "utf8")).toBe("previous output"); expect(await readdir(folder)).toEqual(["existing.mp4"]); }, 10_000); it("publishes playable cut, jump-cut, concat and audiogram outputs without clobbering concat notes", async () => { const folder = await workspace(); const source = join(folder, "source.mp4"); await runFfmpeg(["-hide_banner", "-y", "-f", "lavfi", "-i", "color=size=96x64:rate=10", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=16000", "-t", "2", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", source]); const cut = join(folder, "cut.mp4"), jump = join(folder, "jump.mp4"), concat = join(folder, "concat.mp4"), audio = join(folder, "audio.mp4"); await cutClip(source, cut, 0, 1); await cutJumpClip(source, jump, 0, [{ startSec: 0, endSec: 0.4 }, { startSec: 1, endSec: 1.5 }]); await writeFile(`${concat}.list.txt`, "keep these user notes"); await concatClips([relative(process.cwd(), cut), cut], concat); await runAudiogram(source, audio, [{ startSec: 0, endSec: 0.2 }], { spec: { ...audiogramSpec(false), width: 160, height: 96, waveHeight: 32 } }); for (const path of [cut, jump, concat, audio]) { const info = await probeMedia(path); expect(info.hasVideo).toBe(true); expect(info.durationSec).toBeGreaterThan(0.1); } expect(await readFile(`${concat}.list.txt`, "utf8")).toBe("keep these user notes"); expect((await readdir(folder)).some((name) => name.startsWith(".hotclip-write-"))).toBe(false); }, 20_000); }); - src/core/__tests__/audio-peaks.test.ts (1801 bytes)
import { describe, expect, it } from "vitest"; import { peaksFromPcm, peakInRange, type PeakTrack } from "../audio-peaks"; describe("peaksFromPcm", () => { it("folds each block to its max |sample| normalised to 0..1", () => { const pcm = new Int16Array([0, 100, -3277, 50, 0, 0, 16384, -20, 0, 0]); const peaks = peaksFromPcm(pcm, 5); expect(peaks.length).toBe(2); expect(peaks[0]).toBeCloseTo(3277 / 32768, 5); expect(peaks[1]).toBeCloseTo(16384 / 32768, 5); }); it("keeps fractional block boundaries aligned via error accumulation", () => { // 16000 Hz at 30 blocks/s -> 533.33 samples per block; over 3s the block // count must be exact (90), not drift to 89 or 91. const pcm = new Int16Array(16000 * 3); const peaks = peaksFromPcm(pcm, 16000 / 30); expect(peaks.length).toBe(90); }); it("handles empty input", () => { expect(peaksFromPcm(new Int16Array(0), 5).length).toBe(0); }); }); describe("peakInRange", () => { const track: PeakTrack = { values: Float32Array.from([0.01, 0.02, 0.5, 0.02, 0.01]), startSec: 10, hopSec: 1, }; it("returns the max peak inside the window", () => { expect(peakInRange(track, 11.5, 13.5)).toBeCloseTo(0.5); }); it("stays quiet when the window avoids the burst", () => { expect(peakInRange(track, 10, 10.9)).toBeLessThan(0.05); }); it("returns 0 (no evidence) for windows fully outside the track", () => { expect(peakInRange(track, 0, 5)).toBe(0); expect(peakInRange(track, 100, 200)).toBe(0); }); it("clamps partially overlapping windows to the track bounds", () => { expect(peakInRange(track, 8, 10.5)).toBeCloseTo(0.02, 4); // start clamp; ceil reaches block 1 expect(peakInRange(track, 14.5, 100)).toBeCloseTo(0.01, 4); // end clamp }); }); - src/core/__tests__/audiogram.test.ts (3485 bytes)
import { describe, it, expect } from "vitest"; import { buildAudiogramArgs, audiogramSpec, hexToFfmpegColor } from "../audiogram"; const SPEC = audiogramSpec(true); describe("hexToFfmpegColor / audiogramSpec", () => { it("品牌色转 ffmpeg 形式,非法回落火焰橙", () => { expect(hexToFfmpegColor("#00ff88")).toBe("0x00FF88"); expect(hexToFfmpegColor("红色")).toBe("0xFF6E0D"); expect(hexToFfmpegColor(undefined)).toBe("0xFF6E0D"); }); it("竖屏 1080×1920/横屏 1920×1080,波形不到高的三分之一", () => { expect(audiogramSpec(true)).toMatchObject({ width: 1080, height: 1920 }); expect(audiogramSpec(false)).toMatchObject({ width: 1920, height: 1080 }); expect(SPEC.waveHeight).toBeLessThan(SPEC.height / 3); }); }); describe("buildAudiogramArgs", () => { const range = [{ startSec: 10, endSec: 25 }]; it("单段:fast seek 到段起点,atrim 相对时刻,双 map 音视频", () => { const args = buildAudiogramArgs("/a.mp3", "/out.mp4", range, { spec: SPEC }); expect(args.join(" ")).toContain("-ss 10.000 -i /a.mp3"); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("atrim=start=0.000:end=15.000"); expect(fc).toContain("showwaves=s=1080x480:mode=cline:rate=30:colors=0xFF6E0D"); expect(fc).toContain("color=c=0x141110:size=1080x1920"); expect(fc).toContain("overlay=x=0:y=(H-h)/2:shortest=1"); expect(args).toContain("[v0]"); expect(args).toContain("[aout]"); }); it("跳剪多段:atrim×N + concat,波形基于拼接后的音频", () => { const args = buildAudiogramArgs("/a.mp3", "/out.mp4", [ { startSec: 10, endSec: 14 }, { startSec: 16, endSec: 20 }, ], { spec: SPEC }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("atrim=start=0.000:end=4.000"); expect(fc).toContain("atrim=start=6.000:end=10.000"); expect(fc).toContain("concat=n=2:v=0:a=1[acat]"); expect(fc).toContain("[acat]asplit=2[aout][awave]"); }); it("响度标准化在拼接后、分流前;字幕与水印按序挂链", () => { const args = buildAudiogramArgs("/a.mp3", "/out.mp4", range, { spec: SPEC, normalizeLoudness: true, subtitlePath: "/tmp/c.ass", fontsDir: "/fonts", watermark: { path: "/logo.png", corner: "top-right", opacity: 0.6, widthPx: 172 }, }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("loudnorm=I=-14"); expect(fc).toContain("[anorm]asplit=2"); expect(fc).toContain("subtitles=filename='/tmp/c.ass':fontsdir='/fonts'[v1]"); expect(fc).toContain("[v1][wm]"); expect(args[args.indexOf("-map") + 1]).toBe("[vout]"); }); it("降噪在响度标准化之前:[a0]→降噪[adn]→loudnorm[anorm]→asplit", () => { const args = buildAudiogramArgs("/a.mp3", "/out.mp4", range, { spec: SPEC, denoise: true, normalizeLoudness: true, }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("afftdn"); expect(fc.indexOf("afftdn")).toBeLessThan(fc.indexOf("loudnorm")); // 先去噪再标准化 expect(fc).toContain("[adn]loudnorm"); expect(fc).toContain("[anorm]asplit=2"); }); it("空段/非法段抛错", () => { expect(() => buildAudiogramArgs("/a.mp3", "/o.mp4", [], { spec: SPEC })).toThrow(); expect(() => buildAudiogramArgs("/a.mp3", "/o.mp4", [{ startSec: 5, endSec: 5 }], { spec: SPEC })).toThrow(); }); }); - src/core/__tests__/automation-history.test.ts (2923 bytes)
import { mkdtemp } from "fs/promises"; import { join } from "path"; import { tmpdir } from "os"; import { describe, expect, it } from "vitest"; import type { AutomationTask } from "../../shared/api-types"; import { clearAutomationTasks, loadAutomationTasks, normalizeAutomationTasks, saveAutomationTasks } from "../automation-history"; const task = (id: string, status: AutomationTask["status"] = "completed"): AutomationTask => ({ id, sourcePath: `/videos/${id}.mp4`, sourceName: `${id}.mp4`, sourceSize: 10, sourceMtimeMs: 20, trigger: "folder", status, stage: status === "completed" ? "exporting" : "queued", attempts: 1, createdAt: "2026-08-24T00:00:00.000Z", updatedAt: `2026-08-24T00:00:0${id}.000Z`, }); describe("automation history", () => { it("retains selection settings across interruption and disk reload for retries", async () => { const dir = await mkdtemp(join(tmpdir(), "hotclip-tasks-")); const detection = { clipLength: "extended" as const, maxClips: 10, genre: { id: "game", custom: "只选翻盘" } }; await saveAutomationTasks(dir, [{ ...task("1", "running"), detection }, task("2")]); const loaded = await loadAutomationTasks(dir, true); expect(loaded.find((t) => t.id === "1")).toMatchObject({ status: "interrupted", detection }); expect(loaded.find((t) => t.id === "2")?.detection).toBeUndefined(); await clearAutomationTasks(dir); }); it("roundtrips newest first and removes duplicate ids", async () => { const dir = await mkdtemp(join(tmpdir(), "hotclip-tasks-")); await saveAutomationTasks(dir, [task("1"), task("2"), task("1")]); expect((await loadAutomationTasks(dir)).map((item) => item.id)).toEqual(["2", "1"]); }); it("marks active tasks interrupted after restart", () => { const recovered = normalizeAutomationTasks([task("1", "queued"), task("2", "running"), task("3")], true); expect(recovered.map((item) => item.status).sort()).toEqual(["completed", "interrupted", "interrupted"]); expect(recovered.filter((item) => item.status === "interrupted").every((item) => item.error === undefined)).toBe(true); }); it("drops malformed records and clears persisted history", async () => { const dir = await mkdtemp(join(tmpdir(), "hotclip-tasks-")); expect(normalizeAutomationTasks([{}, task("1")])).toHaveLength(1); await saveAutomationTasks(dir, [task("1")]); await clearAutomationTasks(dir); expect(await loadAutomationTasks(dir)).toEqual([]); }); it("bounds history to the newest 200 records", () => { const many = Array.from({ length: 240 }, (_, index) => ({ ...task(String(index).padStart(3, "0")), updatedAt: new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString(), })); const normalized = normalizeAutomationTasks(many); expect(normalized).toHaveLength(200); expect(normalized[0].id).toBe("239"); expect(normalized.at(-1)?.id).toBe("040"); }); }); - src/core/__tests__/autozoom.test.ts (5115 bytes)
import { describe, it, expect } from "vitest"; import { planZoomKeyframes, renderZoomExpr, buildZoomFilter, ZOOM_BASE, ZOOM_BREATH, ZOOM_EMPHASIS, ZOOM_MIN_CLIP_SEC, } from "../autozoom"; describe("planZoomKeyframes", () => { it("太短的切片不做运镜(推不到位只会显得晃)", () => { expect(planZoomKeyframes(ZOOM_MIN_CLIP_SEC - 0.1)).toEqual([]); expect(planZoomKeyframes(0)).toEqual([]); }); it("呼吸节奏:从基准起步,在基准与推近之间来回,不会一路推到底", () => { const kfs = planZoomKeyframes(30); expect(kfs[0]).toEqual({ t: 0, z: ZOOM_BASE }); const zooms = kfs.map((k) => k.z); expect(Math.max(...zooms)).toBeCloseTo(ZOOM_BREATH, 5); expect(Math.min(...zooms)).toBeCloseTo(ZOOM_BASE, 5); // 回落过:峰值之后一定还有一个基准值 const firstPeak = zooms.findIndex((z) => z > ZOOM_BASE); expect(zooms.slice(firstPeak).some((z) => z === ZOOM_BASE)).toBe(true); }); it("关键帧时间严格递增且不超片长", () => { const kfs = planZoomKeyframes(47, { emphasisAtSec: [5, 20, 33] }); for (let i = 1; i < kfs.length; i++) expect(kfs[i].t).toBeGreaterThan(kfs[i - 1].t); expect(kfs[kfs.length - 1].t).toBeLessThanOrEqual(47); expect(kfs.every((k) => k.z >= ZOOM_BASE)).toBe(true); }); it("强调时刻推到最近,并且是先动镜头后到内容", () => { const kfs = planZoomKeyframes(40, { emphasisAtSec: [20] }); const peak = kfs.find((k) => k.z === ZOOM_EMPHASIS); expect(peak).toBeDefined(); expect(peak!.t).toBeLessThanOrEqual(20); // 推近之前有一个基准帧(起推点),在强调点之前 const lead = kfs.filter((k) => k.t < peak!.t && k.z === ZOOM_BASE).pop(); expect(lead).toBeDefined(); }); it("挨得很近的多个强调并成一段长推近,不来回抽搐", () => { const kfs = planZoomKeyframes(40, { emphasisAtSec: [20, 20.5, 21] }); // 只推近一次、只回落一次 let rises = 0; let falls = 0; for (let i = 1; i < kfs.length; i++) { if (kfs[i].z === ZOOM_EMPHASIS && kfs[i - 1].z < ZOOM_EMPHASIS) rises++; if (kfs[i - 1].z === ZOOM_EMPHASIS && kfs[i].z < ZOOM_EMPHASIS) falls++; } expect(rises).toBe(1); expect(falls).toBe(1); }); it("任何两个关键帧之间的变焦速度都在人眼可接受范围(不出现瞬间跳变)", () => { // 每秒变焦超过 0.3 倍就是抽搐——曾经的 bug:两个相邻强调合并时 // 前一段的回落点没清掉,导致 0.1 秒内从 1.0 猛推到 1.1 const cases = [ [8, 8.5], [8, 8.5, 22], [5, 5.2, 5.4, 5.6], [10, 11, 12, 13, 14], ]; for (const emphasisAtSec of cases) { const kfs = planZoomKeyframes(30, { emphasisAtSec }); for (let i = 1; i < kfs.length; i++) { const dz = Math.abs(kfs[i].z - kfs[i - 1].z); const dt = kfs[i].t - kfs[i - 1].t; expect(dz / dt).toBeLessThanOrEqual(0.3); } } }); it("越界的强调时刻被忽略", () => { const kfs = planZoomKeyframes(20, { emphasisAtSec: [-5, 100, NaN] }); expect(kfs.every((k) => k.z <= ZOOM_BREATH)).toBe(true); }); }); describe("renderZoomExpr", () => { it("空关键帧 = 不缩放", () => { expect(renderZoomExpr([])).toBe("1"); }); it("单关键帧 = 常量倍率", () => { expect(renderZoomExpr([{ t: 0, z: 1.05 }])).toBe("1.0500"); }); it("分段线性插值,自变量是 in_time(不是输出帧号)", () => { const expr = renderZoomExpr([ { t: 0, z: 1 }, { t: 5, z: 1.1 }, ]); expect(expr).toContain("in_time"); expect(expr).toContain("lt(in_time,5.000)"); expect(expr).not.toContain("NaN"); }); it("关键帧过多时降采样,嵌套深度受控", () => { const many = Array.from({ length: 200 }, (_, i) => ({ t: i, z: 1 + (i % 2) * 0.05 })); const expr = renderZoomExpr(many, 8); expect((expr.match(/if\(/g) ?? []).length).toBeLessThanOrEqual(8); expect(expr).not.toContain("NaN"); }); it("同一时刻的重复关键帧不产生除零", () => { const expr = renderZoomExpr([ { t: 1, z: 1 }, { t: 1, z: 1.1 }, { t: 3, z: 1 }, ]); expect(expr).not.toContain("Infinity"); expect(expr).not.toContain("NaN"); expect(expr).not.toContain("/0.0000"); }); }); describe("buildZoomFilter", () => { it("生成 zoompan 串:居中缩放、输出目标尺寸、显式带上源帧率", () => { const f = buildZoomFilter(30, 30, 1080, 1920)!; expect(f).toContain("zoompan="); expect(f).toContain("s=1080x1920"); expect(f).toContain("fps=30"); // 不显式给 fps 会被重采样到 25 expect(f).toContain("d=1"); expect(f).toContain("iw/2-(iw/zoom/2)"); }); it("帧率未知时拒绝生成(宁可不运镜也不能改帧率)", () => { expect(buildZoomFilter(30, 0, 1080, 1920)).toBeNull(); expect(buildZoomFilter(30, NaN, 1080, 1920)).toBeNull(); }); it("太短的切片返回 null,调用方退回普通 scale", () => { expect(buildZoomFilter(2, 30, 1080, 1920)).toBeNull(); }); }); - src/core/__tests__/boundary.test.ts (3835 bytes)
import { describe, it, expect } from "vitest"; import { adjustClipBoundary, adjustCandidateBoundary } from "../../shared/boundary"; import type { Transcript } from "../../shared/api-types"; // four 4s sentences back to back: [0-4] [4-8] [8-12] [12-16] const tx: Transcript = { language: "zh", engine: "test", durationSec: 16, segments: [0, 1, 2, 3].map((i) => ({ id: i + 1, startSec: i * 4, endSec: i * 4 + 4, text: `第${i + 1}句`, words: [], })), }; describe("adjustClipBoundary", () => { const clip = { startSec: 4, endSec: 12 }; // sentences 2+3 it("start -1 pulls the previous sentence in", () => { const r = adjustClipBoundary(tx, clip, "start", -1)!; expect(r.startSec).toBe(0); expect(r.text).toBe("第1句 第2句 第3句"); }); it("start +1 drops the first sentence", () => { const r = adjustClipBoundary(tx, clip, "start", 1)!; expect(r.startSec).toBe(8); expect(r.text).toBe("第3句"); }); it("end +1 pulls the next sentence in", () => { const r = adjustClipBoundary(tx, clip, "end", 1)!; expect(r.endSec).toBe(16); expect(r.text).toBe("第2句 第3句 第4句"); }); it("end -1 drops the last sentence", () => { const r = adjustClipBoundary(tx, clip, "end", -1)!; expect(r.endSec).toBe(8); expect(r.text).toBe("第2句"); }); it("refuses to move past the transcript edges", () => { expect(adjustClipBoundary(tx, { startSec: 0, endSec: 8 }, "start", -1)).toBeNull(); expect(adjustClipBoundary(tx, { startSec: 8, endSec: 16 }, "end", 1)).toBeNull(); }); it("refuses to collapse a single-sentence clip", () => { expect(adjustClipBoundary(tx, { startSec: 4, endSec: 8 }, "start", 1)).toBeNull(); expect(adjustClipBoundary(tx, { startSec: 4, endSec: 8 }, "end", -1)).toBeNull(); }); it("refuses durations below the minimum", () => { // dropping one sentence from a 2-sentence clip leaves 4s ≥ 3s min — fine; // but a sub-3s result must be rejected const shortTx: Transcript = { ...tx, segments: [ { id: 1, startSec: 0, endSec: 1, text: "a", words: [] }, { id: 2, startSec: 1, endSec: 2, text: "b", words: [] }, ], }; expect(adjustClipBoundary(shortTx, { startSec: 0, endSec: 2 }, "end", -1)).toBeNull(); }); }); describe("adjustCandidateBoundary(拼接片)", () => { // 十句连排,每句 4 秒:[0-4] … [36-40] const tenTx: Transcript = { language: "zh", engine: "test", durationSec: 40, segments: Array.from({ length: 10 }, (_, i) => ({ id: i + 1, startSec: i * 4, endSec: i * 4 + 4, text: `第${i + 1}句`, words: [], })), }; it("手动 5 段片调边后仍是 5 段——不许被 AI 的 4 段护栏悄悄砍", () => { // 选了句 1/3/5/7/9(隔句选):5 个不相邻的段 const pieces = [0, 2, 4, 6, 8].map((i) => ({ startSec: i * 4, endSec: i * 4 + 4 })); const clip = { startSec: 0, endSec: 36, pieces }; const adj = adjustCandidateBoundary(tenTx, clip, "end", 1); expect(adj).not.toBeNull(); // 尾段向后扩一句:最后一段 [32,36] → [32,40],其余四段原样保留 expect(adj!.pieces).toHaveLength(5); expect(adj!.pieces![4]).toEqual({ startSec: 32, endSec: 40 }); expect(adj!.pieces!.slice(0, 4)).toEqual(pieces.slice(0, 4)); }); it("只动首/尾段的边,中间段一动不动", () => { const pieces = [ { startSec: 4, endSec: 8 }, { startSec: 16, endSec: 20 }, { startSec: 28, endSec: 32 }, ]; const adj = adjustCandidateBoundary(tenTx, { startSec: 4, endSec: 32, pieces }, "start", -1); expect(adj).not.toBeNull(); expect(adj!.pieces).toEqual([ { startSec: 0, endSec: 8 }, { startSec: 16, endSec: 20 }, { startSec: 28, endSec: 32 }, ]); }); }); - src/core/__tests__/brand.test.ts (7980 bytes)
import { describe, it, expect } from "vitest"; import { hexToAssColor, hexToAssInline, lightenHex, applyBrandToLayout, sanitizeBrand, isValidHex, FONT_SCALE_CHOICES, } from "../brand"; import { VERTICAL_LAYOUT, buildCaptionAss, keywordText } from "../subtitle"; import { buildOverlayPayload } from "../caption-overlay/payload"; import { watermarkStages, composeVideoFilter, buildCutArgs, buildJumpCutArgs } from "../cut"; describe("hex → ASS 颜色转换", () => { it("#RRGGBB → &HAABBGGRR(BGR 序)", () => { expect(hexToAssColor("#FF6E0D")).toBe("&H000D6EFF"); expect(hexToAssColor("#3355FF", "7F")).toBe("&H7FFF5533"); expect(hexToAssColor("22C55E")).toBe("&H005EC522"); // 无 # 前缀也接受 }); it("行内覆写形式 &HBBGGRR&", () => { expect(hexToAssInline("#FF6E0D")).toBe("&H0D6EFF&"); }); it("非法输入返回 null", () => { expect(hexToAssColor("#FFF")).toBeNull(); expect(hexToAssColor("red")).toBeNull(); expect(hexToAssInline("#GG0000")).toBeNull(); expect(isValidHex("#12345")).toBe(false); }); it("向白提亮(渐变第二停靠色)", () => { expect(lightenHex("#000000", 1)).toBe("#FFFFFF"); expect(lightenHex("#FF6E0D", 0)).toBe("#FF6E0D"); expect(lightenHex("bad", 0.5)).toBe("bad"); // 非法原样返回 }); }); describe("applyBrandToLayout", () => { it("无品牌或全默认 → 布局原样(引用不变)", () => { expect(applyBrandToLayout(VERTICAL_LAYOUT)).toBe(VERTICAL_LAYOUT); expect(applyBrandToLayout(VERTICAL_LAYOUT, { highlightColor: "#123456" })).toBe(VERTICAL_LAYOUT); }); it("字号放大 → 每行宽度单位同比减少", () => { const l = applyBrandToLayout(VERTICAL_LAYOUT, { fontScale: 1.18 }); expect(l.fontSize).toBe(Math.round(78 * 1.18)); expect(l.maxLineUnits).toBe(Math.round(22 / 1.18)); }); it("小字号档位明显缩小动态字幕", () => { const l = applyBrandToLayout(VERTICAL_LAYOUT, { fontScale: FONT_SCALE_CHOICES.small }); expect(l.fontSize).toBe(Math.round(VERTICAL_LAYOUT.fontSize * 0.68)); expect(l.fontSize).toBeLessThan(VERTICAL_LAYOUT.fontSize * 0.7); }); it("位置三档只动 marginV", () => { expect(applyBrandToLayout(VERTICAL_LAYOUT, { captionPosition: "low" }).marginV).toBe(420); expect(applyBrandToLayout(VERTICAL_LAYOUT, { captionPosition: "high" }).marginV).toBe(700); }); }); describe("sanitizeBrand(IPC 边界消毒)", () => { it("合法字段通过,非法字段剔除", () => { const b = sanitizeBrand({ highlightColor: "#22C55E", fontScale: 99, // 超界 → 钳到 1.6 captionPosition: "middle", // 非法档位 → 剔除 watermark: { path: "/tmp/logo.png", corner: "nowhere", opacity: 5 }, }); expect(b).toEqual({ highlightColor: "#22C55E", fontScale: 1.6, watermark: { path: "/tmp/logo.png", corner: "top-right", opacity: 1 }, }); }); it("全空/非对象 → undefined(管线走默认)", () => { expect(sanitizeBrand(undefined)).toBeUndefined(); expect(sanitizeBrand({})).toBeUndefined(); expect(sanitizeBrand({ highlightColor: "nope" })).toBeUndefined(); }); }); describe("品牌顶部标题", () => { const titleCard = { text: "标题", durationSec: 4 }; it("缺省使用历史底板样式;未知预设在 IPC 边界被忽略", () => { const old = buildCaptionAss([], 0, VERTICAL_LAYOUT, "karaoke", { titleCard }); const classic = buildCaptionAss([], 0, VERTICAL_LAYOUT, "karaoke", { titleCard: { ...titleCard, style: "plate" } }); expect(classic).toBe(old); expect(old).toContain("&H00FFFFFF,&H00FFFFFF,&H73000000,&H73000000,-1,0,0,0,100,100,0,0,3,12,0,8,"); expect(sanitizeBrand({ titleStyle: "future" })).toBeUndefined(); }); it.each([ ["outline", "#123456", "&H00FFFFFF", "&H00000000", "1,5"], ["brandPlate", "#FFDD00", "&H00000000", "&H0000DDFF", "3,12"], ["brandPlate", "#112233", "&H00FFFFFF", "&H00332211", "3,12"], ] as const)("导出 %s 配 %s,保持位置与时长", (style, highlightHex, text, border, shape) => { expect(sanitizeBrand({ titleStyle: style })).toEqual({ titleStyle: style }); const ass = buildCaptionAss([], 0, VERTICAL_LAYOUT, "karaoke", { titleCard: { ...titleCard, style }, highlightHex }); const line = ass.split("\n").find((l) => l.startsWith("Style: Title,")); expect(line).toContain(`${text},&H00FFFFFF,${border},${border},-1,0,0,0,100,100,0,0,${shape},0,8,`); expect(ass).toContain("Dialogue: 1,0:00:00.00,0:00:04.00,Title,,0,0,0,,标题"); }); }); describe("品牌色贯穿字幕构建", () => { const words = [ { text: "你好", startSec: 0.2, endSec: 0.8 }, { text: "世界", startSec: 0.9, endSec: 1.5 }, ]; it("卡拉OK主色与钩子色用品牌色;缺省仍是火焰橙", () => { const branded = buildCaptionAss(words, 0, VERTICAL_LAYOUT, "karaoke", { highlightHex: "#22C55E" }); expect(branded).toContain("&H005EC522"); expect(branded).not.toContain("&H000D6EFF"); const plain = buildCaptionAss(words, 0, VERTICAL_LAYOUT, "karaoke", {}); expect(plain).toContain("&H000D6EFF"); }); it("关键词行内覆写用品牌色", () => { expect(keywordText(words, ["世界"], "#3355FF")).toContain("\\c&HFF5533&"); expect(keywordText(words, ["世界"])).toContain("\\c&H0D6EFF&"); }); it("气泡 payload 带渐变双色;非法色回落默认", () => { const p = buildOverlayPayload(words, VERTICAL_LAYOUT, { highlightHex: "#3355FF" }); expect(p.highlightColor).toBe("#3355FF"); expect(p.highlightColor2).not.toBe("#3355FF"); // 已向白提亮 expect(buildOverlayPayload(words, VERTICAL_LAYOUT, {}).highlightColor).toBe("#FF6E0D"); }); }); describe("水印 filter 组装", () => { const wm = { path: "/tmp/logo.png", corner: "top-right" as const, opacity: 0.8, widthPx: 172 }; it("movie 源 + 透明度 + 缩放,四角定位表达式", () => { const s = watermarkStages(wm); expect(s.source).toBe("movie='/tmp/logo.png',format=rgba,colorchannelmixer=aa=0.800,scale=172:-1"); expect(s.overlay).toBe("overlay=W-w-44:44:format=auto"); expect(watermarkStages({ ...wm, corner: "bottom-left" }).overlay).toBe("overlay=44:H-h-44:format=auto"); // 不透明度 1 时省掉 mixer expect(watermarkStages({ ...wm, opacity: 1 }).source).not.toContain("colorchannelmixer"); }); it("composeVideoFilter:无水印原样;有水印起第二路;无前置链用 copy 垫底", () => { expect(composeVideoFilter(["scale=1080:1920"])).toBe("scale=1080:1920"); expect(composeVideoFilter(["scale=1080:1920"], wm)).toBe( "scale=1080:1920[main];movie='/tmp/logo.png',format=rgba,colorchannelmixer=aa=0.800,scale=172:-1[wm];[main][wm]overlay=W-w-44:44:format=auto" ); expect(composeVideoFilter([], wm)).toMatch(/^copy\[main\];/); }); it("buildCutArgs:水印进 -vf 且强制重编码", () => { const args = buildCutArgs("/in.mp4", "/out.mp4", 0, 10, { mode: "copy", watermark: wm }); const vf = args[args.indexOf("-vf") + 1]; expect(vf).toContain("[main][wm]overlay=W-w-44:44"); expect(args).toContain("libx264"); // copy 被升级为重编码 }); it("buildJumpCutArgs:水印叠在后处理链之后(字幕之上)", () => { const args = buildJumpCutArgs("/in.mp4", "/out.mp4", 0, [{ startSec: 0, endSec: 2 }, { startSec: 3, endSec: 5 }], { vertical: true, watermark: wm, }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("[vc]"); // concat 先出 [vc] expect(fc).toContain("[vmain][wm]overlay=W-w-44:44:format=auto[vout]"); // 无后处理链时直接 [vc][wm] 收口 const bare = buildJumpCutArgs("/in.mp4", "/out.mp4", 0, [{ startSec: 0, endSec: 2 }, { startSec: 3, endSec: 5 }], { watermark: wm, }); const fcBare = bare[bare.indexOf("-filter_complex") + 1]; expect(fcBare).toContain("[vc][wm]overlay=W-w-44:44:format=auto[vout]"); }); }); - src/core/__tests__/caption-overlay.test.ts (2863 bytes)
import { describe, expect, it } from "vitest"; import { buildOverlayPayload } from "../caption-overlay/payload"; import { VERTICAL_LAYOUT } from "../subtitle"; import type { TranscriptWord } from "../../shared/api-types"; const w = (text: string, s: number, e: number): TranscriptWord => ({ text, startSec: s, endSec: e }); describe("buildOverlayPayload", () => { const words = [w("你看", 0, 0.4), w("这个", 0.4, 0.8), w("速度", 0.8, 1.2), w("真的", 3.0, 3.4), w("绝了", 3.4, 3.8)]; it("derives geometry from the ASS layout", () => { const p = buildOverlayPayload(words, VERTICAL_LAYOUT); expect(p.width).toBe(1080); expect(p.height).toBe(1920); expect(p.fontSize).toBe(78); // marginV 560 from the bottom of 1920 → baseline ≈ 70.8% expect(p.baselineFrac).toBeCloseTo((1920 - 560) / 1920, 5); }); it("times words in ms and lingers the last line", () => { const p = buildOverlayPayload(words, VERTICAL_LAYOUT); const last = p.lines[p.lines.length - 1]; expect(last.words[last.words.length - 1].endMs).toBe(3800); expect(last.endMs).toBe(3800 + 350); }); it("marks keyword words after fusion", () => { const kw = buildOverlayPayload(words, VERTICAL_LAYOUT, { keywords: ["速度"] }); const flat = kw.lines.flatMap((l) => l.words); expect(flat.find((x) => x.text === "速度")?.keyword).toBe(true); expect(flat.find((x) => x.text === "你看")?.keyword).toBe(false); }); it("respects forced breaks (jump-cut splice points)", () => { const tight = [w("一", 0, 0.3), w("二", 0.3, 0.6), w("三", 0.6, 0.9)]; const p = buildOverlayPayload(tight, VERTICAL_LAYOUT, { forcedBreaks: [0.6] }); expect(p.lines.length).toBe(2); expect(p.lines[1].words[0].text).toBe("三"); }); it("holds each line until the next begins (anti-flicker) but clears on a real pause", () => { // 0.4s gap after "这个" → bridged; 1.8s gap after "速度" → capped hold, not bridged const p = buildOverlayPayload(words, { ...VERTICAL_LAYOUT, maxLineUnits: 4 }); expect(p.lines.length).toBeGreaterThanOrEqual(3); // line "你看" (ends 0.4) holds to "这个" start (0.4) — contiguous, no flicker expect(p.lines[0].endMs).toBe(p.lines[1].startMs); // "速度" ends 1.2 before a 1.8s pause → held only +0.8 cap = 2.0s, not to 3.0 const beforePause = p.lines[2]; expect(beforePause.endMs).toBe(2000); }); it("carries per-word speaker ids through for caption coloring", () => { const spoken = [ { ...w("甲", 0, 0.4), speaker: 0 }, { ...w("乙", 0.4, 0.8), speaker: 1 }, ]; const p = buildOverlayPayload(spoken, VERTICAL_LAYOUT); const flat = p.lines.flatMap((l) => l.words); expect(flat.find((x) => x.text === "甲")?.speaker).toBe(0); expect(flat.find((x) => x.text === "乙")?.speaker).toBe(1); }); }); - src/core/__tests__/caption-readability.test.ts (2447 bytes)
import { describe, expect, it } from "vitest"; import { buildCaptionAss, captionReadableChars, captionReadingCps, planReadableCaptions, VERTICAL_LAYOUT } from "../subtitle"; import { buildOverlayPayload } from "../caption-overlay/payload"; import { srtLinesFromWords } from "../srt"; import { lintSubtitleTimeline } from "../subtitle-quality"; describe("language-aware caption display", () => { it("uses graphemes and script-specific reading defaults", () => { expect(captionReadableChars("cafe\u0301")).toBe(4); expect(captionReadingCps("你好")).toBe(9); expect(captionReadingCps("こんにちは")).toBe(7); expect(captionReadingCps("안녕")).toBe(12); expect(captionReadingCps("hello")).toBe(20); }); it("extends short display only into available space; original word timings stay unchanged", () => { const words = [{ text: "你好", startSec: 0, endSec: 0.2, timingSource: "native" as const }]; const before = JSON.stringify(words); const plan = planReadableCaptions(words, 22, [], 0.9); expect(plan[0].endSec).toBeCloseTo(0.7); expect(JSON.stringify(words)).toBe(before); expect(planReadableCaptions(words, 22, [0.4], 0.9)[0].endSec).toBe(0.4); expect(planReadableCaptions(words, 22, [], 0.3)[0].endSec).toBe(0.3); }); it("merges readable short lines but respects splices and speaker changes", () => { const words = [{ text: "好!", startSec: 0, endSec: 0.2, speaker: 0 }, { text: "走吧。", startSec: 0.25, endSec: 0.6, speaker: 0 }]; expect(planReadableCaptions(words, 22, [], 1)).toHaveLength(1); expect(planReadableCaptions(words, 22, [0.23], 1)).toHaveLength(2); expect(planReadableCaptions([words[0], { ...words[1], speaker: 1 }], 22, [], 1)).toHaveLength(2); }); it("ASS, overlay, SRT and quality checks share the effective display duration", () => { const words = [{ text: "Hello", startSec: 0, endSec: 0.1, timingSource: "native" as const }]; const options = { readability: true, endSec: 1 }; const ass = buildCaptionAss(words, 0, VERTICAL_LAYOUT, "karaoke", options); expect(ass).toContain("0:00:00.70"); expect(ass).toContain("\\k10"); expect(buildOverlayPayload(words, VERTICAL_LAYOUT, options).lines[0].endMs).toBe(700); expect(srtLinesFromWords(words, [], [], options)[0].endSec).toBeCloseTo(0.7); expect(lintSubtitleTimeline(words, VERTICAL_LAYOUT, "karaoke", [], [], options).status).toBe("pass"); }); }); - src/core/__tests__/chat-complete-errors.test.ts (11221 bytes)
/** * LLM 连接失败的报错必须可执行(issue #6):选了本地 Ollama 但没装/没启动的 * 用户,只看到 fetch failed 是不知道下一步的——本地/云端要给不同的指引。 */ import { afterEach, describe, expect, it, vi } from "vitest"; import { chatComplete, MAX_TOKENS, RETRY_MAX_TOKENS, thinkingParams } from "../highlight/detect"; import { visionChatComplete } from "../highlight/vision"; const OLLAMA = { baseUrl: "http://localhost:11434/v1", apiKey: "", model: "qwen3:8b" }; const CLOUD = { baseUrl: "https://api.atlascloud.ai/v1", apiKey: "sk-x", model: "qwen/qwen3.5-flash" }; afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); }); describe("chatComplete 连接失败指引", () => { it("本地端点连不上 → 指引安装/启动 Ollama 或换云端", async () => { vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("fetch failed"); })); await expect(chatComplete(OLLAMA, "s", "u")).rejects.toThrow(/Ollama/); await expect(chatComplete(OLLAMA, "s", "u")).rejects.toThrow(/ollama\.com/); }); it("云端端点连不上 → 指引查网络与 Base URL,不提 Ollama", async () => { vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("fetch failed"); })); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(err.message).toContain("检查网络"); expect(err.message).not.toContain("Ollama"); }); it("本地 404(模型没拉) → 附 ollama pull 命令", async () => { vi.stubGlobal("fetch", vi.fn(async () => new Response("model not found", { status: 404 }))); await expect(chatComplete(OLLAMA, "s", "u")).rejects.toThrow(/ollama pull qwen3:8b/); }); it("云端 404 → 不附 ollama pull 提示", async () => { vi.stubGlobal("fetch", vi.fn(async () => new Response("no such model", { status: 404 }))); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(err.message).toContain("HTTP 404"); expect(err.message).not.toContain("ollama pull"); }); }); /** 构造一条 OpenAI 兼容响应。 */ function chatResponse(message: Record<string, unknown>, finishReason = "stop"): Response { return new Response(JSON.stringify({ choices: [{ finish_reason: finishReason, message }] }), { status: 200 }); } describe("Kimi 固定采样温度 (issue #18)", () => { it.each(["kimi-k3", "moonshotai/Kimi-K2.5"])("文本与视觉首请求交给 %s 选择合法默认值", async (model) => { const fetchMock = vi.fn(async (_url: string, init: RequestInit) => { const body = JSON.parse(String(init.body)); return "temperature" in body ? new Response("invalid temperature: only 1 is allowed for this model", { status: 400 }) : chatResponse({ content: "ok" }); }); vi.stubGlobal("fetch", fetchMock); const llm = { ...CLOUD, model }; await expect(chatComplete(llm, "s", "u")).resolves.toBe("ok"); await expect(visionChatComplete(llm, "s", "u", "ZmFrZQ==")).resolves.toBe("ok"); expect(fetchMock).toHaveBeenCalledTimes(2); }); it("保留其他模型的文本和视觉采样设置", async () => { const fetchMock = vi.fn(async (_url: string, _init: RequestInit) => chatResponse({ content: "ok" })); vi.stubGlobal("fetch", fetchMock); await chatComplete(CLOUD, "s", "u"); await visionChatComplete(CLOUD, "s", "u", "ZmFrZQ=="); expect(fetchMock.mock.calls.map(([, init]) => JSON.parse(String(init.body)).temperature)).toEqual([0.6, 0.2]); }); }); describe("chatComplete 请求恢复边界", () => { it("限流恢复和 Qwen 参数回退共用一次重试额度", async () => { const fetchMock = vi.fn() .mockResolvedValueOnce(new Response("busy", { status: 429, headers: { "retry-after": "0" } })) .mockResolvedValueOnce(new Response("unsupported parameter: enable_thinking", { status: 400 })) .mockResolvedValueOnce(new Response("busy again", { status: 503, headers: { "retry-after": "0" } })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete(CLOUD, "s", "u")).rejects.toThrow("HTTP 503"); expect(fetchMock).toHaveBeenCalledTimes(3); expect(fetchMock.mock.calls.map((c) => JSON.parse(c[1].body).enable_thinking)).toEqual([false, false, undefined]); }); it("参数回退只使用首次请求剩余的时间", async () => { vi.useFakeTimers(); const fetchMock = vi.fn() .mockImplementationOnce(async () => { vi.setSystemTime(Date.now() + 170_000); return new Response("unsupported parameter: enable_thinking", { status: 400 }); }) .mockImplementation((_url, init: RequestInit) => new Promise((_resolve, reject) => { init.signal!.addEventListener("abort", () => reject(init.signal!.reason)); })); vi.stubGlobal("fetch", fetchMock); const pending = chatComplete(CLOUD, "s", "u"); const check = expect(pending).rejects.toMatchObject({ kind: "timeout" }); await vi.advanceTimersByTimeAsync(10_000); await check; expect(fetchMock).toHaveBeenCalledTimes(2); expect(vi.getTimerCount()).toBe(0); }); it("服务端要求长等待时保留等待提示并隐藏回显 Key", async () => { const fetchMock = vi.fn(async () => new Response(`rate limited for ${CLOUD.apiKey}`, { status: 429, headers: { "retry-after": "120" }, })); vi.stubGlobal("fetch", fetchMock); const error = await chatComplete(CLOUD, "s", "u").catch((e: Error) => e) as Error; expect(error.message).toContain("120 秒"); expect(error.message).not.toContain(CLOUD.apiKey); expect(fetchMock).toHaveBeenCalledTimes(1); }); it("空白正文仍可取回正常结束的 reasoning", async () => { const fetchMock = vi.fn(async () => chatResponse({ content: " \n ", reasoning: '{"clips":[]}' })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete(CLOUD, "s", "u")).resolves.toBe('{"clips":[]}'); expect(fetchMock).toHaveBeenCalledTimes(1); }); }); describe("chatComplete 空响应处理(issue #8)", () => { it("Qwen3 混合思考模型首请求关闭 thinking,避免正文预算被吃光", async () => { const fetchMock = vi.fn(async () => chatResponse({ content: '{"clips":[]}' })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete({ ...CLOUD, model: "qwen3.5-flash" }, "s", "u")).resolves.toBe('{"clips":[]}'); const body = JSON.parse(((fetchMock.mock.calls[0] as unknown) as [string, { body: string }])[1].body) as Record<string, unknown>; expect(body.enable_thinking).toBe(false); expect(fetchMock).toHaveBeenCalledTimes(1); }); it("严格网关不认识 enable_thinking 时回退为标准请求", async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(new Response("unsupported parameter: enable_thinking", { status: 400 })) .mockResolvedValueOnce(chatResponse({ content: '{"clips":[]}' })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete({ ...CLOUD, model: "qwen3.5-flash" }, "s", "u")).resolves.toBe('{"clips":[]}'); const firstBody = JSON.parse(((fetchMock.mock.calls[0] as unknown) as [string, { body: string }])[1].body) as Record<string, unknown>; const secondBody = JSON.parse(((fetchMock.mock.calls[1] as unknown) as [string, { body: string }])[1].body) as Record<string, unknown>; expect(firstBody.enable_thinking).toBe(false); expect(secondBody.enable_thinking).toBeUndefined(); }); it("兼容 OpenAI 多模态 content 数组和旧式 choices.text", async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(chatResponse({ content: [{ type: "text", text: "{" }, { type: "text", text: '"clips":[]}' }] })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete(CLOUD, "s", "u")).resolves.toBe('{"clips":[]}'); vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ choices: [{ text: "legacy" }] }), { status: 200 }))); await expect(chatComplete(CLOUD, "s", "u")).resolves.toBe("legacy"); }); it("只给 Qwen/QwQ 注入关闭 thinking 参数", () => { expect(thinkingParams("qwen3.5-flash")).toEqual({ enable_thinking: false }); expect(thinkingParams("Qwen/QwQ-32B")).toEqual({ enable_thinking: false }); expect(thinkingParams("deepseek-v4-flash")).toEqual({}); }); it("思考模型烧完预算(finish=length) → 换大预算重试并成功", async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(chatResponse({ content: "", reasoning_content: "先想想…" }, "length")) .mockResolvedValueOnce(chatResponse({ content: '{"clips":[]}' })); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete(CLOUD, "s", "u")).resolves.toBe('{"clips":[]}'); expect(fetchMock).toHaveBeenCalledTimes(2); const budgets = fetchMock.mock.calls.map( (c) => (JSON.parse((c as [string, { body: string }])[1].body) as { max_tokens: number }).max_tokens ); expect(budgets).toEqual([MAX_TOKENS, RETRY_MAX_TOKENS]); }); it("两次都只有思考没有正文 → 指引换非思考模型", async () => { vi.stubGlobal( "fetch", vi.fn(async () => chatResponse({ content: "", reasoning_content: "想了很久" }, "length")) ); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(err.message).toContain("未返回内容"); expect(err.message).toContain("深度思考"); expect(err.message).toContain("non-thinking"); }); it("正文被网关错放进 reasoning(正常收尾) → 直接取 reasoning,不重试", async () => { const fetchMock = vi.fn(async () => chatResponse({ content: "", reasoning: '{"clips":[]}' }, "stop")); vi.stubGlobal("fetch", fetchMock); await expect(chatComplete(CLOUD, "s", "u")).resolves.toBe('{"clips":[]}'); expect(fetchMock).toHaveBeenCalledTimes(1); }); it("安全审查拦截(content_filter) → 提示换供应商或素材", async () => { vi.stubGlobal("fetch", vi.fn(async () => chatResponse({ content: "" }, "content_filter"))); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(err.message).toContain("安全审查"); }); it("重试自身报 HTTP 错 → 不覆盖「空响应」诊断", async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(chatResponse({ content: "", reasoning_content: "…" }, "length")) .mockResolvedValueOnce(new Response("max_tokens too large", { status: 400 })); vi.stubGlobal("fetch", fetchMock); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(err.message).toContain("未返回内容"); expect(err.message).toContain("深度思考"); }); it("普通空响应(无思考轨迹) → 重试一次后给通用提示", async () => { const fetchMock = vi.fn(async () => chatResponse({ content: "" })); vi.stubGlobal("fetch", fetchMock); const err = (await chatComplete(CLOUD, "s", "u").catch((e: unknown) => e)) as Error; expect(fetchMock).toHaveBeenCalledTimes(2); expect(err.message).toContain("空内容"); expect(err.message).not.toContain("深度思考"); }); }); - src/core/__tests__/coldopen.test.ts (2597 bytes)
import { describe, it, expect } from "vitest"; import { planColdOpen, COLD_OPEN_MAX_SEC, COLD_OPEN_MIN_SEC } from "../coldopen"; import type { TranscriptWord } from "../../shared/api-types"; /** 每字 0.5s 的等宽词流,从 startSec 起。 */ function words(text: string, startSec: number, perChar = 0.5): TranscriptWord[] { return Array.from(text).map((ch, i) => ({ text: ch, startSec: startSec + i * perChar, endSec: startSec + (i + 1) * perChar, })); } describe("planColdOpen", () => { // 切片 100s 起,前 12s 是铺垫,钩子句「直接倒半杯水都不带渗」在 112s 起 const clipStart = 100; const clipWords = [...words("今天给大家带来一款超级好用的纸巾三层加厚", clipStart), ...words("直接倒半杯水都不带渗", 112)]; it("定位钩子句并返回其词边界区间(10 字×0.5s=5s,超 MAX=4s 截到 116s)", () => { const p = planColdOpen(clipWords, "直接倒半杯水都不带渗", clipStart); expect(p).not.toBeNull(); expect(p!.startSec).toBeCloseTo(112, 3); expect(p!.endSec).toBeCloseTo(116, 3); }); it("钩子句超长:从句首截到 MAX 并收口到词尾", () => { const p = planColdOpen(clipWords, "直接倒半杯水都不带渗", clipStart)!; expect(p.endSec - p.startSec).toBeLessThanOrEqual(COLD_OPEN_MAX_SEC + 1e-6); // 词边界收口:endSec 必须等于某个词的 endSec expect(clipWords.some((w) => Math.abs(w.endSec - p.endSec) < 1e-6)).toBe(true); }); it("钩子离切片开头太近(<10s)→ 跳过", () => { const near = [...words("开场白", 100), ...words("爆点句在这里", 103), ...words("后续内容继续说", 108)]; expect(planColdOpen(near, "爆点句在这里", 100)).toBeNull(); }); it("钩子定位失败/空钩子/过短片段 → null(宁可不做不可做错)", () => { expect(planColdOpen(clipWords, "转写里根本没有这句", clipStart)).toBeNull(); expect(planColdOpen(clipWords, "", clipStart)).toBeNull(); expect(planColdOpen([], "直接倒", clipStart)).toBeNull(); // 命中但只有一个字(0.5s < MIN=1s) const tiny = [...words("很长的铺垫内容一直在说话不停地说", 100), ...words("炸", 115)]; expect(planColdOpen(tiny, "炸", 100)).toBeNull(); expect(COLD_OPEN_MIN_SEC).toBeGreaterThan(0.5); }); it("引用与转写标点/大小写有出入仍能对齐(同选段的归一化匹配)", () => { const p = planColdOpen(clipWords, "直接倒半杯水,都不带渗!", clipStart); expect(p).not.toBeNull(); expect(p!.startSec).toBeCloseTo(112, 3); }); }); - src/core/__tests__/color.integration.test.ts (7629 bytes)
import { describe, expect, it } from "vitest"; import { mkdtemp, rm } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; import { isExecutableColorPlan, planColorRender } from "../color"; import { cutClip, cutJumpClip, runFfmpeg } from "../cut"; import { probeMedia } from "../probe"; const HDR_TRANSFERS = [ { name: "PQ colour bars (BT.2020 non-constant, TV range)", transfer: "smpte2084", detected: "pq", pattern: "smptehdbars=size=320x180:rate=24:duration=1", space: "bt2020nc", range: "tv", }, { name: "HLG saturated gradient (BT.2020 non-constant, full range)", transfer: "arib-std-b67", detected: "hlg", pattern: "gradients=size=320x180:rate=24:duration=1:c0=red:c1=blue:x0=0:y0=0:x1=320:y1=180", space: "bt2020nc", range: "pc", }, ] as const; describe("HDR color FFmpeg integration", () => { it.each(HDR_TRANSFERS)( "tone-maps a tagged synthetic $name 10-bit source to BT.709 SDR", async ({ transfer, detected, pattern, space, range }) => { const root = await mkdtemp(join(tmpdir(), "hotclip-color-integration-")); const source = join(root, `source-${detected}.mkv`); const output = join(root, `output-${detected}.mp4`); try { // FFV1 keeps this fixture lossless and compact while Matroska preserves // the stream color tags that drive HotClip's metadata-only HDR detection. await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", pattern, "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=1", "-c:v", "ffv1", "-level", "3", "-pix_fmt", "yuv420p10le", "-color_primaries", "bt2020", "-color_trc", transfer, "-colorspace", space, "-color_range", range, "-c:a", "pcm_s16le", "-shortest", source, ]); const input = await probeMedia(source); expect(input.pixelFormat).toBe("yuv420p10le"); expect(input.bitDepth).toBe(10); expect(input.colorTransfer).toBe(transfer); const color = planColorRender(input); expect(color.detected).toBe(detected); expect(color.action).toBe("tonemap-bt709"); expect(await cutClip(source, output, 0, 1, { color })).toBe("encode"); const rendered = await probeMedia(output); expect(rendered.hasVideo).toBe(true); expect(rendered.hasAudio).toBe(true); expect(rendered.pixelFormat).toBe("yuv420p"); expect(rendered.colorRange).toBe("tv"); expect(rendered.colorSpace).toBe("bt709"); expect(rendered.colorTransfer).toBe("bt709"); expect(rendered.colorPrimaries).toBe("bt709"); expect(rendered.durationSec).toBeGreaterThan(0.9); expect(rendered.durationSec).toBeLessThan(1.1); } finally { await rm(root, { recursive: true, force: true }); } }, 20_000 ); it("keeps a transfer-only HDR file exportable instead of guessing missing colour tags", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-color-incomplete-")); const source = join(root, "source-transfer-only.mkv"); const output = join(root, "output-transfer-only.mp4"); try { await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=24:duration=1", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=1", "-c:v", "ffv1", "-level", "3", "-pix_fmt", "yuv420p10le", "-color_primaries", "unknown", "-color_trc", "smpte2084", "-colorspace", "unknown", "-color_range", "unknown", "-c:a", "pcm_s16le", "-shortest", source, ]); const input = await probeMedia(source); expect(input.colorTransfer).toBe("smpte2084"); const color = planColorRender(input); expect(color).toMatchObject({ detected: "pq", action: "passthrough", reason: "hdr-pq-unsupported-color-path-passthrough", }); expect(await cutClip(source, output, 0, 1, { color })).toBe("encode"); const rendered = await probeMedia(output); expect(rendered.hasVideo).toBe(true); expect(rendered.durationSec).toBeGreaterThan(0.9); } finally { await rm(root, { recursive: true, force: true }); } }, 20_000); it.each([ { name: "SDR first, PQ default second", order: ["sdr", "pq"] as const, expected: "pq" as const, width: 320, }, { name: "PQ first, SDR default second", order: ["pq", "sdr"] as const, expected: "sdr" as const, width: 256, }, ])("keeps probe and render on the same stream for $name", async ({ order, expected, width }) => { const root = await mkdtemp(join(tmpdir(), "hotclip-color-multistream-")); const source = join(root, "source.mkv"); const output = join(root, "output.mp4"); const jumpOutput = join(root, "output-jump.mp4"); const pattern = (kind: "pq" | "sdr"): string => kind === "pq" ? "smptehdbars=size=320x180:rate=24:duration=1" : "testsrc2=size=256x144:rate=24:duration=1"; try { const args = [ "-hide_banner", "-y", "-f", "lavfi", "-i", pattern(order[0]), "-f", "lavfi", "-i", pattern(order[1]), "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=1", "-map", "0:v:0", "-map", "1:v:0", "-map", "2:a:0", "-c:v", "ffv1", "-level:v:0", "3", "-level:v:1", "3", "-c:a", "pcm_s16le", ]; for (let index = 0; index < order.length; index++) { const pq = order[index] === "pq"; args.push( `-pix_fmt:v:${index}`, pq ? "yuv420p10le" : "yuv420p", `-color_primaries:v:${index}`, pq ? "bt2020" : "bt709", `-color_trc:v:${index}`, pq ? "smpte2084" : "bt709", `-colorspace:v:${index}`, pq ? "bt2020nc" : "bt709", `-color_range:v:${index}`, "tv", `-disposition:v:${index}`, index === 1 ? "default" : "0" ); } args.push("-shortest", source); await runFfmpeg(args); const input = await probeMedia(source); expect(input.videoStreamIndex).toBe(1); expect(input.audioStreamIndex).toBe(2); expect(input.width).toBe(width); expect(input.colorTransfer).toBe(expected === "pq" ? "smpte2084" : "bt709"); const planned = planColorRender(input); expect(planned.detected).toBe(expected); const renderOptions = { videoStreamIndex: input.videoStreamIndex, audioStreamIndex: input.audioStreamIndex, color: isExecutableColorPlan(planned) ? planned : undefined, }; await cutClip(source, output, 0, 1, renderOptions); await cutJumpClip( source, jumpOutput, 0, [{ startSec: 0, endSec: 0.4 }, { startSec: 0.6, endSec: 1 }], renderOptions ); const rendered = await probeMedia(output); expect(rendered.width).toBe(width); expect(rendered.colorTransfer).toBe("bt709"); expect(rendered.colorPrimaries).toBe("bt709"); expect(rendered.colorSpace).toBe("bt709"); expect(rendered.hasAudio).toBe(true); const jumped = await probeMedia(jumpOutput); expect(jumped.width).toBe(width); expect(jumped.colorTransfer).toBe("bt709"); expect(jumped.colorPrimaries).toBe("bt709"); expect(jumped.colorSpace).toBe("bt709"); expect(jumped.hasAudio).toBe(true); } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); }); - src/core/__tests__/color.test.ts (7006 bytes)
import { describe, expect, it } from "vitest"; import type { MediaInfo } from "../probe"; import { colorOutputArgs, hdrToneMapFilter, isExecutableColorPlan, isHdrSource, planColorRender } from "../color"; function media(overrides: Partial<MediaInfo> = {}): MediaInfo { return { durationSec: 60, hasVideo: true, hasAudio: true, width: 3840, height: 2160, fps: 30, bitRate: 20_000_000, videoCodec: "hevc", audioCodec: "aac", ...overrides, }; } describe("planColorRender", () => { it("plans an explicit PQ to BT.709 transform", () => { const plan = planColorRender(media({ pixelFormat: "YUV420P10LE", bitDepth: 10, colorPrimaries: "BT2020", colorTransfer: "SMPTE2084", colorSpace: "BT2020NC", colorRange: "TV", })); expect(plan).toEqual({ source: { pixelFormat: "yuv420p10le", bitDepth: 10, primaries: "bt2020", transfer: "smpte2084", space: "bt2020nc", range: "tv", peakNits: 0, }, detected: "pq", action: "tonemap-bt709", output: { pixelFormat: "yuv420p", bitDepth: 8, primaries: "bt709", transfer: "bt709", space: "bt709", range: "tv", peakNits: 100, }, reason: "hdr-pq-tone-map-bt709", }); }); it("plans the same safe transform for explicit HLG", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "arib-std-b67", colorSpace: "bt2020nc", colorRange: "tv", })); expect(plan.detected).toBe("hlg"); expect(plan.action).toBe("tonemap-bt709"); expect(plan.output?.transfer).toBe("bt709"); expect(isExecutableColorPlan(plan)).toBe(true); }); it("detects HDR but fails open when the input colour path is incomplete", () => { const plan = planColorRender(media({ colorTransfer: "smpte2084" })); expect(plan).toMatchObject({ detected: "pq", action: "passthrough", output: null, reason: "hdr-pq-unsupported-color-path-passthrough", }); expect(isHdrSource(plan)).toBe(true); }); it("fails open when an HDR matrix is unknown instead of guessing BT.2020", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "arib-std-b67", colorSpace: "unknown", colorRange: "tv", })); expect(plan).toMatchObject({ detected: "hlg", action: "passthrough", output: null, reason: "hdr-hlg-unsupported-color-path-passthrough", }); }); it("rejects HDR matrix names not verified by the bundled zscale runtime", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "smpte2084", colorSpace: "smpte2085", colorRange: "tv", })); expect(plan).toMatchObject({ detected: "pq", action: "passthrough", reason: "hdr-pq-unsupported-color-path-passthrough", }); }); it("rejects BT.2020 constant-luminance HDR because the bundled transform cannot execute it", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "arib-std-b67", colorSpace: "bt2020c", colorRange: "pc", })); expect(plan).toMatchObject({ detected: "hlg", action: "passthrough", reason: "hdr-hlg-unsupported-color-path-passthrough", }); }); it("passes SDR through without manufacturing output metadata", () => { const plan = planColorRender(media({ colorTransfer: "bt709" })); expect(isHdrSource(plan)).toBe(false); expect(plan).toMatchObject({ detected: "sdr", action: "passthrough", output: null, reason: "sdr-transfer-passthrough", }); }); it("does not guess HDR from a 10-bit BT.2020 stream without a known transfer", () => { const plan = planColorRender(media({ pixelFormat: "yuv420p10le", bitDepth: 10, colorPrimaries: "bt2020", colorTransfer: "unspecified", colorSpace: "bt2020nc", })); expect(plan.detected).toBe("unknown"); expect(plan.action).toBe("passthrough"); expect(plan.output).toBeNull(); }); it("keeps legacy MediaInfo fixtures safe", () => { const plan = planColorRender(media()); expect(plan.source).toEqual({ pixelFormat: "", bitDepth: 0, primaries: "", transfer: "", space: "", range: "", peakNits: 0, }); expect(plan.detected).toBe("unknown"); expect(plan.action).toBe("passthrough"); }); }); describe("HDR FFmpeg helpers", () => { it("returns the verified linear-light Mobius chain and BT.709 tags", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "smpte2084", colorSpace: "bt2020nc", colorRange: "tv", })); expect(hdrToneMapFilter(plan)).toBe( "zscale=pin=bt2020:tin=smpte2084:min=bt2020nc:rin=tv:t=linear:npl=100,format=gbrpf32le,tonemap=tonemap=mobius:desat=2,zscale=p=bt709:t=bt709:m=bt709:r=tv:dither=error_diffusion,format=yuv420p" ); expect(colorOutputArgs(plan)).toEqual([ "-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709", "-color_range", "tv", ]); }); it("leaves peak automatic when static HDR metadata is absent", () => { const common = { colorPrimaries: "bt2020", colorSpace: "bt2020nc", colorRange: "tv", }; const pq = planColorRender(media({ ...common, colorTransfer: "smpte2084" })); const hlg = planColorRender(media({ ...common, colorTransfer: "arib-std-b67" })); expect(hdrToneMapFilter(pq)).not.toContain(":peak="); expect(hdrToneMapFilter(hlg)).not.toContain(":peak="); }); it("prefers static MaxCLL/mastering peak metadata over the fallback", () => { const plan = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "smpte2084", colorSpace: "bt2020nc", colorRange: "tv", hdrPeakNits: 4000, })); expect(plan.source.peakNits).toBe(4000); expect(hdrToneMapFilter(plan)).toContain("peak=40"); }); it("does not change the SDR/unknown render path", () => { const plan = planColorRender(media({ colorTransfer: "bt709" })); expect(hdrToneMapFilter(plan)).toBeNull(); expect(colorOutputArgs(plan)).toEqual([]); expect(hdrToneMapFilter(undefined)).toBeNull(); expect(colorOutputArgs(null)).toEqual([]); }); it("refuses a stale or manually forged active plan with an unsupported input path", () => { const valid = planColorRender(media({ colorPrimaries: "bt2020", colorTransfer: "smpte2084", colorSpace: "bt2020nc", colorRange: "tv", })); const forged = { ...valid, source: { ...valid.source, space: "smpte2085" } }; expect(isExecutableColorPlan(forged)).toBe(false); expect(hdrToneMapFilter(forged)).toBeNull(); expect(colorOutputArgs(forged)).toEqual([]); }); }); - src/core/__tests__/commands.test.ts (2662 bytes)
/** * 主播口令打点(v0.13):「这段剪下来/clip that」是主播自证的爆点。 * 误报代价低(提示词里多一行证据),但高频日常语(切换/这段时间)必须排掉, * 不然满屏假口令会稀释真信号。 */ import { describe, it, expect } from "vitest"; import { isClipCommand, detectClipCommands, COMMAND_MAX_MARKS } from "../highlight/commands"; import type { Transcript } from "../transcribe/types"; function tx(sentences: Array<[string, number]>): Transcript { return { language: "zh", engine: "test", durationSec: sentences[sentences.length - 1]?.[1] ?? 0, segments: sentences.map(([text, startSec], i) => ({ id: i + 1, startSec, endSec: startSec + 3, text, words: [], })), }; } describe("isClipCommand", () => { it("命中常见中文剪辑口令", () => { for (const s of [ "这段给我剪下来", "把这段剪下来发抖音", "刚才那段切出来", "记得把这个剪下来啊", "帮我剪个切片", "后期把这段剪了", "切片君剪一下这段", "这段剪出来绝对爆", ]) { expect(isClipCommand(s), s).toBe(true); } }); it("命中英文口令", () => { for (const s of ["clip that", "someone clip this moment", "that's a clip right there", "CLIP IT"]) { expect(isClipCommand(s), s).toBe(true); } }); it("不误伤日常用语", () => { for (const s of [ "这段时间我剪了很多视频", // 「这段时间」排除 "我们切换到下一个话题", // 切换排除 "我平时用剪映做剪辑", "把它切成两半就能吃了", "切一下水果给大家看", "这段感情让我成长了很多", "a clip from yesterday", // 名词用法 "今天聊聊剪辑技巧", ]) { expect(isClipCommand(s), s).toBe(false); } }); }); describe("detectClipCommands", () => { it("返回命中句的时刻,近距离命中只记一次", () => { const t = tx([ ["大家好", 0], ["这段剪下来", 100], ["对就是刚才那段切出来", 110], // 与上一条相距 <20s,去重 ["正常聊天", 200], ["clip that", 300], ]); expect(detectClipCommands(t)).toEqual([100, 300]); }); it("总量截断防口头禅刷屏", () => { const many = Array.from({ length: 40 }, (_, i) => ["这段剪下来", i * 30] as [string, number]); expect(detectClipCommands(tx(many))).toHaveLength(COMMAND_MAX_MARKS); }); it("没有口令时返回空数组", () => { expect(detectClipCommands(tx([["今天聊聊选品", 0]]))).toEqual([]); }); }); - src/core/__tests__/contact-sheet.test.ts (3685 bytes)
import { describe, it, expect } from "vitest"; import { chunkCells, buildSheetArgs, SHEET_CELLS } from "../contact-sheet"; import type { ColorRenderPlan } from "../color"; const pqPlan: ColorRenderPlan = { source: { pixelFormat: "yuv420p10le", bitDepth: 10, primaries: "bt2020", transfer: "smpte2084", space: "bt2020nc", range: "tv", peakNits: 1000, }, detected: "pq", action: "tonemap-bt709", output: { pixelFormat: "yuv420p", bitDepth: 8, primaries: "bt709", transfer: "bt709", space: "bt709", range: "tv", peakNits: 100, }, reason: "hdr-pq-tone-map-bt709", }; describe("chunkCells", () => { it("按满格容量分组,最后一组可不满", () => { const groups = chunkCells(Array.from({ length: 20 }, (_, i) => i)); expect(groups.map((g) => g.length)).toEqual([9, 9, 2]); expect(groups[0][0]).toBe(0); expect(groups[2]).toEqual([18, 19]); }); it("空数组返回空;自定义容量生效", () => { expect(chunkCells([])).toEqual([]); expect(chunkCells([1, 2, 3], 2).map((g) => g.length)).toEqual([2, 1]); }); }); describe("buildSheetArgs", () => { it("满格:九路 -ss 输入,每路取一帧,concat 后 tile 3x3", () => { const times = Array.from({ length: SHEET_CELLS }, (_, i) => i * 10 + 5); const args = buildSheetArgs("/v.mp4", times); expect(args.filter((a) => a === "-i")).toHaveLength(9); expect(args.filter((a) => a === "/v.mp4")).toHaveLength(9); expect(args[args.indexOf("-ss") + 1]).toBe("5.00"); const graph = args[args.indexOf("-filter_complex") + 1]; expect(graph).toContain("trim=end_frame=1"); // 每路只取一帧,concat 才不会等整条流 expect(graph).toContain("concat=n=9:v=1:a=0"); expect(graph).toContain("tile=3x3:color=black"); expect(args[args.indexOf("-map") + 1]).toBe("[sheet]"); expect(args).toContain("image2pipe"); }); it("不满格按需缩网格(4 帧 → 3x2,黑底补齐)", () => { const graph = buildSheetArgs("/v.mp4", [1, 2, 3, 4]).join(" "); expect(graph).toContain("tile=3x2"); }); it("两帧 → 2x1;单帧退化为不拼格", () => { expect(buildSheetArgs("/v.mp4", [1, 2]).join(" ")).toContain("tile=2x1"); const single = buildSheetArgs("/v.mp4", [3]).join(" "); expect(single).not.toContain("tile="); expect(single).not.toContain("concat="); expect(single).toContain("[sheet]"); }); it("给了字体才烧序号,路径按 filter 语法转义", () => { const withFont = buildSheetArgs("/v.mp4", [1, 2], { fontFile: "C:\\fonts\\f.otf" }).join(" "); expect(withFont).toContain("drawtext=fontfile='C\\:/fonts/f.otf'"); expect(withFont).toContain("text='1'"); expect(withFont).toContain("text='2'"); expect(buildSheetArgs("/v.mp4", [1, 2]).join(" ")).not.toContain("drawtext"); }); it("负时刻钳到 0,空时刻数组直接抛错", () => { const args = buildSheetArgs("/v.mp4", [-1]); expect(args[args.indexOf("-ss") + 1]).toBe("0.00"); expect(() => buildSheetArgs("/v.mp4", [])).toThrow(); }); it("每路输入都取同一全局选中视频轨,HDR 预览先转 SDR 再缩放", () => { const args = buildSheetArgs("/v.mkv", [1, 2], { videoStreamIndex: 3, color: pqPlan }); const graph = args[args.indexOf("-filter_complex") + 1]; expect(graph).toContain("[0:3]zscale=pin=bt2020"); expect(graph).toContain("[1:3]zscale=pin=bt2020"); expect(graph).toContain("trim=end_frame=1"); expect(graph).not.toContain("[0:v]"); expect(graph.indexOf("zscale=pin=bt2020")).toBeLessThan(graph.indexOf("scale=448:-2")); expect(graph).toContain("tonemap=tonemap=mobius"); }); }); - src/core/__tests__/content-lint.test.ts (4132 bytes)
import { describe, it, expect } from "vitest"; import { lintText, lintClipContent, formatLintIssue } from "../content-lint"; describe("lintText (平台违禁词扫描)", () => { it("绝对化用语命中", () => { const hits = lintText("这款是全网最低价,销量第一的国家级产品"); const terms = hits.map((h) => h.term); expect(terms).toContain("全网最低价"); expect(terms).toContain("销量第一"); expect(terms).toContain("国家级"); expect(hits.every((h) => h.category === "绝对化用语")).toBe(true); }); it("医疗功效与承诺类命中,类别正确", () => { const hits = lintText("三天见效,根治脱发,无效退款"); expect(hits).toEqual( expect.arrayContaining([ expect.objectContaining({ term: "根治", category: "医疗功效宣称" }), expect.objectContaining({ term: "三天见效", category: "夸大承诺" }), expect.objectContaining({ term: "无效退款", category: "夸大承诺" }), ]) ); }); it("收益承诺与导流话术命中", () => { const hits = lintText("躺赚月入过万,想要的加微信私信我领取"); const terms = hits.map((h) => h.term); expect(terms).toContain("躺赚"); expect(terms).toContain("月入过万"); expect(terms).toContain("加微信"); expect(terms).toContain("私信我领"); }); it("同一词多次出现只报一次", () => { const hits = lintText("最低价!今天最低价!还是最低价!"); expect(hits.filter((h) => h.term === "最低价")).toHaveLength(1); }); it("日常口语不误报(裸「最/第一」不在规则里)", () => { expect(lintText("我最近在减脂,今天第一次直播,最后聊聊感受")).toEqual([]); expect(lintText("这个功能特别好用,大家可以试试")).toEqual([]); }); it("空文本返回空", () => { expect(lintText("")).toEqual([]); }); }); describe("lintClipContent (整条切片的物料扫描)", () => { it("按物料来源分别报告;字幕按字拼接也能扫到跨词命中", () => { const hits = lintClipContent({ title: "全网最低价的秘密", hook: "看完你就知道怎么躺赚", publish: { title: "买它", hashtags: ["#好物"], description: "假一赔十,无效退款", cta: "加微信领福利" }, // 中文 ASR 按字出词:「根」「治」相邻拼接后才可命中 captionText: "这个方子能根治老胃病", }); expect(hits).toEqual( expect.arrayContaining([ expect.objectContaining({ term: "全网最低价", source: "title" }), expect.objectContaining({ term: "躺赚", source: "hook" }), expect.objectContaining({ term: "假一赔十", source: "publish" }), expect.objectContaining({ term: "无效退款", source: "publish" }), expect.objectContaining({ term: "加微信", source: "publish" }), expect.objectContaining({ term: "根治", source: "caption" }), ]) ); }); it("同一词在不同物料各报一次(要分别改)", () => { const hits = lintClipContent({ title: "最低价来了", captionText: "今天最低价" }); expect(hits.filter((h) => h.term === "最低价")).toHaveLength(2); }); it("全部缺省/干净物料 → 空命中", () => { expect(lintClipContent({})).toEqual([]); expect(lintClipContent({ title: "分享一个学习方法", captionText: "坚持就有收获" })).toEqual([]); }); }); describe("formatLintIssue (告警文案)", () => { it("无命中返回 null", () => { expect(formatLintIssue([])).toBeNull(); }); it("点名词+类别+来源;超过 5 个归入「等 N 处」", () => { const one = formatLintIssue([{ term: "根治", category: "医疗功效宣称", source: "caption" }]); expect(one).toContain("「根治」"); expect(one).toContain("医疗功效宣称"); expect(one).toContain("字幕"); const many = formatLintIssue( Array.from({ length: 7 }, (_, i) => ({ term: `词${i}`, category: "绝对化用语", source: "publish" as const })) ); expect(many).toContain("等 7 处"); }); }); - src/core/__tests__/copy-templates.test.ts (1731 bytes)
import { describe, it, expect } from "vitest"; import { isHookAngle, isCtaType, hookAngleMenu, ctaMenu, hookAngleLabel, ctaTypeLabel, } from "../copy-templates"; describe("校验器", () => { it("菜单内 id 通过,菜单外/非字符串拒绝", () => { expect(isHookAngle("question")).toBe(true); expect(isHookAngle("urgency")).toBe(true); expect(isHookAngle("clickbait")).toBe(false); expect(isHookAngle(1)).toBe(false); expect(isCtaType("product")).toBe(true); expect(isCtaType("buy_now")).toBe(false); expect(isCtaType(undefined)).toBe(false); }); }); describe("提示词菜单", () => { it("中文菜单 8 角度 5 CTA,每行带 id 与用法", () => { const angles = hookAngleMenu(true).split("\n"); expect(angles.length).toBe(8); expect(angles[0]).toContain("question=提问式"); const ctas = ctaMenu(true).split("\n"); expect(ctas.length).toBe(5); expect(ctas.some((l) => l.startsWith("product=商品引导"))).toBe(true); }); it("高危角度/CTA 的提示里带红线(与违禁词 lint 呼应)", () => { expect(hookAngleMenu(true)).toContain("禁编造截止"); expect(ctaMenu(true)).toContain("禁站外导流"); }); it("英文菜单同样成行", () => { expect(hookAngleMenu(false).split("\n").length).toBe(8); expect(ctaMenu(false).split("\n").length).toBe(5); }); }); describe("标签", () => { it("已知 id 出人类可读名,未知 id 原样返回", () => { expect(hookAngleLabel("pain", true)).toBe("痛点共鸣"); expect(hookAngleLabel("pain", false)).toBe("pain"); expect(ctaTypeLabel("save", true)).toBe("收藏回看"); expect(hookAngleLabel("mystery", true)).toBe("mystery"); }); }); - src/core/__tests__/cover-quality.integration.test.ts (1690 bytes)
import { mkdtemp, rm } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; import { describe, expect, it } from "vitest"; import { selectQualityCoverTime } from "../cover-quality"; import { runFfmpeg } from "../cut"; describe("quality cover FFmpeg golden set", () => { it("rejects black/white frames and ranks the sharp frame above its blurred twin", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-cover-quality-")); const video = join(root, "golden.mkv"); try { await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", "color=c=black:size=320x180:rate=24:duration=1", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=24:duration=1", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=24:duration=1", "-f", "lavfi", "-i", "color=c=white:size=320x180:rate=24:duration=1", "-filter_complex", "[0:v]format=yuv420p[v0];[1:v]gblur=sigma=14,format=yuv420p[v1];[2:v]format=yuv420p[v2];[3:v]format=yuv420p[v3];[v0][v1][v2][v3]concat=n=4:v=1:a=0[out]", "-map", "[out]", "-c:v", "ffv1", "-level", "3", video, ]); const selection = await selectQualityCoverTime({ videoPath: video, candidates: [0.5, 1.5, 2.5, 3.5].map((atSec) => ({ atSec, source: "uniform" as const, priority: 0.5 })), fallbackSec: 0.5, }); expect(selection.mode).toBe("quality-ranked"); expect(selection.candidatesEvaluated).toBe(4); expect(selection.candidatesRejected).toBeGreaterThanOrEqual(2); expect(selection.selectedSec).toBe(2.5); } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); }); - src/core/__tests__/cover-quality.test.ts (3701 bytes)
import { describe, expect, it, vi } from "vitest"; import { parseCoverMetricFrames, proposeCoverTimes, selectQualityCoverTime, type CoverFrameMetrics, } from "../cover-quality"; import type { PeakTrack } from "../audio-peaks"; const healthy = (over: Partial<CoverFrameMetrics> = {}): CoverFrameMetrics => ({ blur: 8, entropy: 0.5, yLow: 30, yAvg: 125, yHigh: 220, satAvg: 80, yDif: 4, ...over, }); describe("cover candidate proposal", () => { it("combines separated audio peaks with uniform fallback coverage", () => { const values = new Float32Array(200); values[40] = 1; values[90] = 0.9; values[140] = 0.8; const peaks: PeakTrack = { values, startSec: 10, hopSec: 0.1 }; const candidates = proposeCoverTimes(peaks, [{ startSec: 10, endSec: 30 }], 20); expect(candidates.length).toBeGreaterThanOrEqual(5); expect(candidates.length).toBeLessThanOrEqual(9); expect(candidates.some((candidate) => candidate.source === "audio")).toBe(true); expect(candidates.some((candidate) => candidate.source === "uniform")).toBe(true); for (let index = 1; index < candidates.length; index++) { expect(candidates.slice(0, index).every((item) => Math.abs(item.atSec - candidates[index].atSec) >= 0.7)).toBe(true); } }); }); describe("cover metric parsing and ranking", () => { it("parses complete FFmpeg metadata frames and drops partial blocks", () => { const text = [ "frame:0 pts_time:0", "lavfi.blur=4.2", "lavfi.entropy.normalized_entropy.normal.Y=0.48", "lavfi.signalstats.YLOW=30", "lavfi.signalstats.YAVG=120", "lavfi.signalstats.YHIGH=210", "lavfi.signalstats.SATAVG=90", "lavfi.signalstats.YDIF=0", "frame:1 pts_time:0.1", "lavfi.blur=9", ].join("\n"); expect(parseCoverMetricFrames(text)).toEqual([ { blur: 4.2, entropy: 0.48, yLow: 30, yAvg: 120, yHigh: 210, satAvg: 90, yDif: 0 }, ]); }); it("rejects black frames, prefers sharp stable detail, and gives variants the next result", async () => { const metrics = new Map<number, CoverFrameMetrics>([ [1, healthy({ yLow: 0, yAvg: 8, yHigh: 15, entropy: 0.04 })], [2, healthy({ blur: 28, entropy: 0.42 })], [3, healthy({ blur: 4, entropy: 0.6, yHigh: 230 })], [4, healthy({ blur: 5, yDif: 35 })], ]); const probe = vi.fn(async (_path: string, at: number) => metrics.get(at) ?? null); const common = { videoPath: "/finished.mp4", candidates: [1, 2, 3, 4].map((atSec) => ({ atSec, source: "audio" as const, priority: 0.8 })), fallbackSec: 1, probe, }; const first = await selectQualityCoverTime(common); const second = await selectQualityCoverTime({ ...common, rank: 1 }); expect(first).toMatchObject({ selectedSec: 3, mode: "quality-ranked", candidatesRejected: 1 }); expect(second.selectedSec).not.toBe(first.selectedSec); }); it("fails open to the old audio timestamp when metrics are unavailable or unsafe", async () => { const candidates = [{ atSec: 2, source: "audio" as const, priority: 1 }]; const unavailable = await selectQualityCoverTime({ videoPath: "/missing.mp4", candidates, fallbackSec: 4.2, probe: async () => null, }); expect(unavailable).toMatchObject({ selectedSec: 4.2, mode: "fallback", candidatesEvaluated: 0 }); const unsafe = await selectQualityCoverTime({ videoPath: "/flat.mp4", candidates, fallbackSec: 4.2, probe: async () => healthy({ yLow: 5, yAvg: 10, yHigh: 18, entropy: 0.05 }), }); expect(unsafe).toMatchObject({ selectedSec: 4.2, mode: "fallback", candidatesRejected: 1 }); }); }); - src/core/__tests__/cover.test.ts (2276 bytes)
import { describe, it, expect } from "vitest"; import { pickCoverTime, fallbackCoverTime } from "../cover"; import type { PeakTrack } from "../audio-peaks"; /** 构造峰值轨:每 0.1s 一块,values 直接传入,startSec 为源起点。 */ function track(values: number[], startSec: number): PeakTrack { return { values: Float32Array.from(values), startSec, hopSec: 0.1 }; } describe("fallbackCoverTime", () => { it("hook 后 0.8s,短片夹到时长内", () => { expect(fallbackCoverTime(20)).toBe(0.8); expect(fallbackCoverTime(0.5)).toBeCloseTo(0.4); }); }); describe("pickCoverTime", () => { // 切片 [10, 20],峰值在源 15s 处 const ranges = [{ startSec: 10, endSec: 20 }]; it("选切片内响度最高时刻,映射到输出时间", () => { const values = Array.from({ length: 100 }, (_, i) => (i === 50 ? 0.9 : 0.2)); // 源 15.0s const at = pickCoverTime(track(values, 10), ranges, 10); expect(at).toBeCloseTo(5.0, 1); // 15s - 10s = 输出 5s }); it("避开成片首尾守卫区:峰值贴边时不选", () => { const values = Array.from({ length: 100 }, (_, i) => (i === 1 ? 0.9 : i === 50 ? 0.5 : 0.1)); // 最响在 10.1s(输出 0.1s 贴边) const at = pickCoverTime(track(values, 10), ranges, 10); expect(at).toBeCloseTo(5.0, 1); // 退而选次响的中部 }); it("跳剪多段:被剪掉区间的峰值不参与,输出时间按段累计", () => { // 段1 [10,13],段2 [17,20];最响在被剪掉的 15s,段2 里 18s 次响 const values = Array.from({ length: 100 }, (_, i) => { const t = 10 + i * 0.1; if (Math.abs(t - 15) < 0.05) return 1.0; if (Math.abs(t - 18) < 0.05) return 0.7; return 0.1; }); const at = pickCoverTime(track(values, 10), [ { startSec: 10, endSec: 13 }, { startSec: 17, endSec: 20 }, ], 6); expect(at).toBeCloseTo(4.0, 1); // 段1 占 3s,18s 在段2 内偏移 1s → 输出 4s }); it("无峰值轨/全静音/超短片回退固定帧", () => { expect(pickCoverTime(undefined, ranges, 10)).toBe(0.8); const silent = Array.from({ length: 100 }, () => 0.01); expect(pickCoverTime(track(silent, 10), ranges, 10)).toBe(0.8); expect(pickCoverTime(track([0.9], 10), ranges, 0.6)).toBeCloseTo(0.5); }); }); - src/core/__tests__/cut.test.ts (19327 bytes)
import { describe, it, expect } from "vitest"; import { buildCutArgs, buildJumpCutArgs, buildVideoFilters, escapeFilterPath, metadataArgs, parseFfmpegProgress, edgeFadeFilters, LOUDNORM_FILTER, LOUDNORM_OUT_RATE, DENOISE_FILTER, EDGE_FADE_SEC } from "../cut"; import type { ColorRenderPlan } from "../color"; import type { VisualEnhancePlan } from "../visual-enhance"; function colorPlan(detected: "pq" | "hlg" | "sdr"): ColorRenderPlan { const hdr = detected !== "sdr"; return { source: { pixelFormat: hdr ? "yuv420p10le" : "yuv420p", bitDepth: hdr ? 10 : 8, primaries: hdr ? "bt2020" : "bt709", transfer: detected === "pq" ? "smpte2084" : detected === "hlg" ? "arib-std-b67" : "bt709", space: hdr ? "bt2020nc" : "bt709", range: "tv", peakNits: hdr ? 0 : 100, }, detected, action: hdr ? "tonemap-bt709" : "passthrough", output: hdr ? { pixelFormat: "yuv420p", bitDepth: 8, primaries: "bt709", transfer: "bt709", space: "bt709", range: "tv", peakNits: 100, } : null, reason: hdr ? `hdr-${detected}-tone-map-bt709` : "sdr-transfer-passthrough", }; } describe("buildCutArgs", () => { it("accurate mode: fast seek before -i, re-encode, faststart", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 61.5, 91.5); const ss = args.indexOf("-ss"); const i = args.indexOf("-i"); expect(ss).toBeGreaterThan(-1); expect(ss).toBeLessThan(i); // fast seek must precede the input expect(args[ss + 1]).toBe("00:01:01.500"); expect(args).toContain("-t"); expect(args[args.indexOf("-t") + 1]).toBe("00:00:30.000"); expect(args).toContain("libx264"); expect(args).toContain("+faststart"); expect(args[args.length - 1]).toBe("/v/out.mp4"); }); it("maps the probed global video/audio streams in continuous and jump-cut paths", () => { const selected = { videoStreamIndex: 1, audioStreamIndex: 2 }; const continuous = buildCutArgs("/v/in.mkv", "/v/out.mp4", 0, 5, selected); expect(continuous.slice(continuous.indexOf("-map"), continuous.indexOf("-map") + 4)).toEqual([ "-map", "0:1", "-map", "0:2", ]); const jump = buildJumpCutArgs( "/v/in.mkv", "/v/out.mp4", 0, [{ startSec: 0, endSec: 2 }, { startSec: 3, endSec: 5 }], selected ); const graph = jump[jump.indexOf("-filter_complex") + 1]; expect(graph).toContain("[0:1]trim=start=0.000:end=2.000"); expect(graph).toContain("[0:2]atrim=start=0.000:end=2.000"); }); it("copy mode: stream copy, no encoder flags", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 10, 20, { mode: "copy" }); expect(args).toContain("copy"); expect(args).not.toContain("libx264"); expect(args).toContain("make_zero"); }); it("clamps negative start to zero", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", -2, 8); expect(args[args.indexOf("-ss") + 1]).toBe("00:00:00.000"); expect(args[args.indexOf("-t") + 1]).toBe("00:00:08.000"); }); it("rejects inverted/empty ranges", () => { expect(() => buildCutArgs("/v/in.mp4", "/v/out.mp4", 30, 30)).toThrow(); expect(() => buildCutArgs("/v/in.mp4", "/v/out.mp4", 30, 10)).toThrow(); }); it("honors custom crf/preset", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { crf: 20, preset: "medium" }); expect(args[args.indexOf("-crf") + 1]).toBe("20"); expect(args[args.indexOf("-preset") + 1]).toBe("medium"); }); it("vertical: center-crop to 9:16 then 1080×1920", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { vertical: true }); const vf = args[args.indexOf("-vf") + 1]; expect(vf).toContain("crop=w='min(iw,ih*9/16)'"); expect(vf).toContain("scale=1080:1920"); expect(vf).toContain("setsar=1"); }); it("subtitlePath: appends the subtitles filter after reframing", () => { const vf = buildVideoFilters({ vertical: true, subtitlePath: "/tmp/a.ass" }); expect(vf[vf.length - 1]).toBe("subtitles=filename='/tmp/a.ass'"); expect(vf[0]).toContain("crop="); }); it("adaptive picture finish runs after geometry and before subtitles", () => { const visualEnhance: VisualEnhancePlan = { applied: true, sampleCount: 8, measurements: { lumaLow: 20, lumaAvg: 55, lumaHigh: 105, saturation: 16 }, adjustments: { brightness: 0.012, contrast: 1.08, saturation: 1.08, gamma: 1.08 }, reasons: ["underexposed", "flat", "muted"], }; const vf = buildVideoFilters({ vertical: true, visualEnhance, subtitlePath: "/tmp/a.ass" }); expect(vf.findIndex((filter) => filter.startsWith("eq="))).toBeGreaterThan(vf.findIndex((filter) => filter.startsWith("scale="))); expect(vf.findIndex((filter) => filter.startsWith("eq="))).toBeLessThan(vf.findIndex((filter) => filter.startsWith("subtitles="))); }); it("active finish disables copy while a neutral plan preserves it", () => { const active: VisualEnhancePlan = { applied: true, sampleCount: 8, measurements: { lumaLow: 20, lumaAvg: 55, lumaHigh: 105, saturation: 16 }, adjustments: { brightness: 0.012, contrast: 1.08, saturation: 1.08, gamma: 1.08 }, reasons: ["underexposed"], }; const neutral: VisualEnhancePlan = { ...active, applied: false, adjustments: { brightness: 0, contrast: 1, saturation: 1, gamma: 1 }, reasons: [] }; expect(buildCutArgs("/i.mp4", "/o.mp4", 0, 5, { mode: "copy", visualEnhance: active })).toContain("libx264"); expect(buildCutArgs("/i.mp4", "/o.mp4", 0, 5, { mode: "copy", visualEnhance: neutral })).toContain("make_zero"); }); it("applies the same finish to the concatenated jump-cut picture", () => { const visualEnhance: VisualEnhancePlan = { applied: true, sampleCount: 8, measurements: { lumaLow: 20, lumaAvg: 55, lumaHigh: 105, saturation: 16 }, adjustments: { brightness: 0.012, contrast: 1.08, saturation: 1.08, gamma: 1.08 }, reasons: ["underexposed"], }; const args = buildJumpCutArgs("/i.mp4", "/o.mp4", 0, [{ startSec: 0, endSec: 2 }, { startSec: 3, endSec: 5 }], { visualEnhance, subtitlePath: "/tmp/a.ass", }); const graph = args[args.indexOf("-filter_complex") + 1]; expect(graph).toContain("[vc]eq=brightness="); expect(graph.indexOf("eq=brightness=")).toBeLessThan(graph.indexOf("subtitles=filename=")); }); it("keeps undefined and explicit SDR color plans on the legacy argument path", () => { const legacy = buildCutArgs("/i.mp4", "/o.mp4", 0, 5, { mode: "copy" }); const sdr = buildCutArgs("/i.mp4", "/o.mp4", 0, 5, { mode: "copy", color: colorPlan("sdr") }); expect(sdr).toEqual(legacy); expect(buildVideoFilters({ color: colorPlan("sdr") })).toEqual([]); }); it("tone-maps PQ before every geometry/finish/text stage, tags SDR, and disables both copy paths", () => { const visualEnhance: VisualEnhancePlan = { applied: true, sampleCount: 8, measurements: { lumaLow: 20, lumaAvg: 55, lumaHigh: 105, saturation: 16 }, adjustments: { brightness: 0.012, contrast: 1.08, saturation: 1.08, gamma: 1.08 }, reasons: ["underexposed"], }; const options = { mode: "copy" as const, videoCopy: true, color: colorPlan("pq"), uiCrop: { topFrac: 0.05, bottomFrac: 0.03 }, vertical: true, autoZoom: { durationSec: 5, fps: 30 }, visualEnhance, subtitlePath: "/tmp/a.ass", }; const filters = buildVideoFilters(options); expect(filters[0]).toBe( "zscale=pin=bt2020:tin=smpte2084:min=bt2020nc:rin=tv:t=linear:npl=100,format=gbrpf32le,tonemap=tonemap=mobius:desat=2,zscale=p=bt709:t=bt709:m=bt709:r=tv:dither=error_diffusion,format=yuv420p" ); expect(filters.findIndex((filter) => filter.startsWith("crop="))).toBeGreaterThan(0); expect(filters.findIndex((filter) => filter.startsWith("zoompan="))).toBeGreaterThan(0); expect(filters.findIndex((filter) => filter.startsWith("eq="))).toBeGreaterThan(0); expect(filters.findIndex((filter) => filter.startsWith("subtitles="))).toBeGreaterThan(0); const args = buildCutArgs("/i.mp4", "/o.mp4", 0, 5, options); expect(args).toContain("libx264"); expect(args.slice(args.indexOf("-c:v"), args.indexOf("-c:v") + 2)).not.toEqual(["-c:v", "copy"]); expect(args.slice(args.indexOf("-color_primaries"), args.indexOf("-color_primaries") + 8)).toEqual([ "-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709", "-color_range", "tv", ]); }); it("keeps HLG tone mapping first on tracked reframes", () => { const filters = buildVideoFilters({ color: colorPlan("hlg"), trackPlan: { cropXExpr: "100", cropW: 1080, cropH: 1920, cropY: 0 }, subtitlePath: "/tmp/a.ass", }); expect(filters[0]).toContain("tonemap=tonemap=mobius"); expect(filters[1]).toBe("crop=w=1080:h=1920:x='100':y=0"); expect(filters.findIndex((filter) => filter.startsWith("subtitles="))).toBeGreaterThan(1); }); it("applies the same HDR transform and BT.709 tags to jump cuts", () => { const args = buildJumpCutArgs( "/i.mp4", "/o.mp4", 0, [{ startSec: 0, endSec: 2 }, { startSec: 3, endSec: 5 }], { color: colorPlan("hlg"), vertical: true, subtitlePath: "/tmp/a.ass" } ); const graph = args[args.indexOf("-filter_complex") + 1]; expect(graph).toContain("[vc]zscale=pin=bt2020:tin=arib-std-b67"); expect(graph.indexOf("zscale=pin=bt2020")).toBeLessThan(graph.indexOf("crop=w='min(iw,ih*9/16)'")); expect(graph.indexOf("crop=w='min(iw,ih*9/16)'")).toBeLessThan(graph.indexOf("subtitles=filename=")); expect(args.slice(args.indexOf("-color_primaries"), args.indexOf("-color_primaries") + 8)).toEqual([ "-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709", "-color_range", "tv", ]); }); it("fontsDir: rides along as the subtitles filter's fontsdir", () => { const vf = buildVideoFilters({ subtitlePath: "/tmp/a.ass", fontsDir: "C:\\App\\fonts" }); expect(vf[0]).toBe("subtitles=filename='/tmp/a.ass':fontsdir='C\\:/App/fonts'"); }); it("filters force accurate mode even when copy was requested", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { mode: "copy", vertical: true }); expect(args).toContain("libx264"); expect(args).not.toContain("make_zero"); }); it("smart video copy preserves AAC and the complete audio filter chain", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 10, 20, { videoCopy: true, denoise: true, normalizeLoudness: true, muteRanges: [{ startSec: 1, endSec: 2 }], }); expect(args.slice(args.indexOf("-c:v"), args.indexOf("-c:v") + 2)).toEqual(["-c:v", "copy"]); expect(args).not.toContain("libx264"); expect(args.slice(args.indexOf("-c:a"), args.indexOf("-c:a") + 2)).toEqual(["-c:a", "aac"]); const audio = args[args.indexOf("-af") + 1]; expect(audio).toContain(DENOISE_FILTER); expect(audio).toContain(LOUDNORM_FILTER); expect(audio).toContain("volume=enable='between(t,1.000,2.000)':volume=0"); expect(audio).toContain("afade=t=in"); }); it("pixel-changing filters disable smart video copy", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { videoCopy: true, vertical: true }); expect(args).toContain("libx264"); expect(args.slice(args.indexOf("-c:v"), args.indexOf("-c:v") + 2)).toEqual(["-c:v", "libx264"]); }); it("escapes windows drive colons and backslashes for the filter graph", () => { expect(escapeFilterPath("C:\\Users\\我\\a.ass")).toBe("C\\:/Users/我/a.ass"); }); }); describe("loudness normalization", () => { it("targets -14 LUFS; the 192kHz leak is capped by the -ar output option, not an in-graph resampler", () => { expect(LOUDNORM_FILTER).toContain("loudnorm=I=-14"); expect(LOUDNORM_FILTER).toContain("TP=-1.5"); // in-graph aresample breaks channel-layout negotiation with the AAC encoder expect(LOUDNORM_FILTER).not.toContain("aresample"); expect(LOUDNORM_OUT_RATE).toBe("48000"); }); it("plain cut: inserts -af loudnorm + -ar before the audio codec when enabled", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { normalizeLoudness: true }); const af = args.indexOf("-af"); expect(af).toBeGreaterThan(-1); expect(args[af + 1].startsWith(LOUDNORM_FILTER)).toBe(true); // 边缘淡化排在其后 expect(args[args.indexOf("-ar") + 1]).toBe(LOUDNORM_OUT_RATE); expect(af).toBeLessThan(args.indexOf("-c:a")); // filter precedes the encoder }); it("plain cut: 关掉响度后 -af 只剩边缘淡化,无 loudnorm", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5); expect(args[args.indexOf("-af") + 1]).toBe("afade=t=in:st=0:d=0.03,afade=t=out:st=4.970:d=0.03"); expect(args.join(" ")).not.toContain("loudnorm"); }); it("plain cut: loudnorm alone forces accurate mode even if copy requested", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { mode: "copy", normalizeLoudness: true }); expect(args).toContain("libx264"); expect(args).toContain("-af"); expect(args).not.toContain("make_zero"); }); it("jump cut: normalizes the spliced stream (concat → [araw] → loudnorm → [aout])", () => { const segs = [{ startSec: 10, endSec: 12 }, { startSec: 15, endSec: 17 }]; const args = buildJumpCutArgs("/v/in.mp4", "/v/out.mp4", 10, segs, { normalizeLoudness: true }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain(":a=1[vout][araw]"); // concat audio → raw label expect(fc).toContain(`[araw]${LOUDNORM_FILTER}[aout]`); // then normalize expect(args[args.indexOf("-map") + 1]).toBe("[vout]"); expect(args).toContain("[aout]"); // final audio map unchanged expect(args[args.indexOf("-ar") + 1]).toBe(LOUDNORM_OUT_RATE); // cap the 192kHz output }); it("jump cut: concat maps straight to [aout] when disabled", () => { const segs = [{ startSec: 10, endSec: 12 }, { startSec: 15, endSec: 17 }]; const fc = buildJumpCutArgs("/v/in.mp4", "/v/out.mp4", 10, segs)[ buildJumpCutArgs("/v/in.mp4", "/v/out.mp4", 10, segs).indexOf("-filter_complex") + 1 ]; expect(fc).toContain(":a=1[vout][aout]"); expect(fc).not.toContain("loudnorm"); }); }); describe("denoise (基础降噪)", () => { it("链路含双高通与 afftdn,不含 aresample", () => { expect(DENOISE_FILTER).toContain("highpass=f=80,highpass=f=80"); expect(DENOISE_FILTER).toContain("afftdn"); expect(DENOISE_FILTER).not.toContain("aresample"); }); it("普通切割:仅降噪 → -af 以降噪链开头(后接边缘淡化),无 -ar,且强制重编码", () => { const args = buildCutArgs("/v/in.mp4", "/v/out.mp4", 0, 5, { mode: "copy", denoise: true }); expect(args).toContain("libx264"); // copy 被升级 expect(args[args.indexOf("-af") + 1].startsWi - src/core/__tests__/danmaku.test.ts (9950 bytes)
import { describe, it, expect } from "vitest"; import { parseBiliDanmakuXml, parseDouyinDanmakuJsonl, danmakuWeight, danmakuPeaks, danmakuPathFor, danmakuPathsFor, danmakuHeatCurve, type DanmakuItem, } from "../danmaku"; describe("parseBiliDanmakuXml", () => { it("解析录播姬/主站格式,时间取 p 首字段,按时间排序", () => { const xml = `<?xml version="1.0"?><i> <d p="65.3,1,25,16777215,1720000000,0,uid,rid">哈哈哈哈</d> <d p="12.5,4,25,65535,1720000001,0,uid,rid">前面的弹幕</d> <d p="66.0,1,25,16777215,1720000002,0,uid,rid">666</d> </i>`; const items = parseBiliDanmakuXml(xml); expect(items.map((d) => d.t)).toEqual([12.5, 65.3, 66.0]); expect(items[1].text).toBe("哈哈哈哈"); }); it("坏条目/空文本/负时间跳过;HTML 实体还原", () => { const xml = `<i> <d p="abc,1">时间坏了</d> <d p="5,1"> </d> <d p="-3,1">负数</d> <d p="8,1">a &<b> ok</d> </i>`; const items = parseBiliDanmakuXml(xml); expect(items.length).toBe(1); expect(items[0].text).toBe("a &<b> ok"); }); it("非弹幕 XML/垃圾输入返回空", () => { expect(parseBiliDanmakuXml("<html><body>404</body></html>")).toEqual([]); expect(parseBiliDanmakuXml("")).toEqual([]); }); it("p 属性第 7 字段解析为发送者;占位 0 不算", () => { const xml = `<i> <d p="10,1,25,16777215,1720000000,0,abc123,rid">带 uid</d> <d p="20,1,25,16777215,1720000000,0,0,rid">占位 uid</d> <d p="30,1">老格式没有 uid</d> </i>`; const items = parseBiliDanmakuXml(xml); expect(items[0].uid).toBe("abc123"); expect(items[1].uid).toBeUndefined(); expect(items[2].uid).toBeUndefined(); }); it("录播姬扩展的 SC/礼物/舰长按付费档解析;没有 ts 的跳过", () => { const xml = `<i> <d p="5,1,25,16777215,1720000000,0,u1,rid">普通弹幕</d> <sc ts="100.5" uid="u2" price="30">太好笑了</sc> <gift ts="50" uid="u3" giftname="小心心" giftcount="10"/> <guard ts="70" user="u4" level="3"/> <gift uid="u5" giftname="没时间戳"/> </i>`; const items = parseBiliDanmakuXml(xml); expect(items.map((d) => d.t)).toEqual([5, 50, 70, 100.5]); const sc = items.find((d) => d.t === 100.5)!; expect(sc.text).toBe("太好笑了"); expect(sc.boost).toBe(6); // SC 是观众花钱说话,一条顶一波弹幕 expect(sc.uid).toBe("u2"); expect(items.find((d) => d.t === 50)!.boost).toBe(1); // 免费小礼物只算一票 expect(items.find((d) => d.t === 70)!.boost).toBe(6); }); }); describe("danmakuWeight", () => { it("高能反应词翻倍", () => { for (const hype of ["哈哈哈哈", "233333", "666", "草", "泪目", "awsl", "笑死我了", "牛逼", "绷不住了"]) { expect(danmakuWeight(hype)).toBe(2); } expect(danmakuWeight("主播今天吃了什么")).toBe(1); }); }); describe("danmakuPeaks", () => { // 构造:全场稀弹幕(每 20s 一条),在 300-315s 处塞一波高能弹幕 function burstItems(): DanmakuItem[] { const items: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 20) items.push({ t, text: "普通聊天" }); for (let i = 0; i < 30; i++) items.push({ t: 300 + i * 0.5, text: i % 2 ? "哈哈哈哈" : "666" }); return items.sort((a, b) => a.t - b.t); } it("弹幕爆发段被圈出,冷场段不圈", () => { const peaks = danmakuPeaks(burstItems(), 1200); expect(peaks.length).toBe(1); expect(peaks[0].startSec).toBeLessThanOrEqual(300); expect(peaks[0].endSec).toBeGreaterThanOrEqual(314); }); it("全场均匀(无峰)不圈段——阈值随基线自适应", () => { const flat: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 2) flat.push({ t, text: "刷屏但均匀" }); expect(danmakuPeaks(flat, 1200)).toEqual([]); }); it("弹幕太稀(峰值窗权重不足)不给伪峰", () => { const sparse: DanmakuItem[] = [ { t: 100, text: "一" }, { t: 102, text: "二" }, { t: 104, text: "三" }, ]; expect(danmakuPeaks(sparse, 1200)).toEqual([]); }); it("空输入/短视频返回空", () => { expect(danmakuPeaks([], 1200)).toEqual([]); expect(danmakuPeaks([{ t: 1, text: "x" }], 5)).toEqual([]); }); it("反刷屏:一个人刷出来的爆发被打折,不当全场沸腾", () => { // 同样 20 条高能弹幕的爆发:全出自一人 vs 出自 20 人 const base: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 20) base.push({ t, text: "普通聊天", uid: `bg${t}` }); const spam = [...base]; for (let i = 0; i < 20; i++) spam.push({ t: 300 + i * 0.4, text: "哈哈哈哈", uid: "spammer" }); const crowd = [...base]; for (let i = 0; i < 20; i++) crowd.push({ t: 300 + i * 0.4, text: "哈哈哈哈", uid: `u${i}` }); expect(danmakuPeaks(spam.sort((a, b) => a.t - b.t), 1200)).toEqual([]); const peaks = danmakuPeaks(crowd.sort((a, b) => a.t - b.t), 1200); expect(peaks.length).toBe(1); expect(peaks[0].startSec).toBeLessThanOrEqual(300); }); it("没有 uid 的老格式不做刷屏惩罚(fail-open)", () => { const items: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 20) items.push({ t, text: "普通聊天" }); for (let i = 0; i < 20; i++) items.push({ t: 300 + i * 0.4, text: "哈哈哈哈" }); expect(danmakuPeaks(items.sort((a, b) => a.t - b.t), 1200).length).toBe(1); }); it("突发加成:突然的小爆发能过线,同等密度的全场温热不圈", () => { // 6 条/窗的突发:裸权重 6 不到 8,加上突升加成 (6-0)*0.5 = 9 过线 const burst: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 40) burst.push({ t, text: "普通聊天" }); for (let i = 0; i < 6; i++) burst.push({ t: 500 + i * 0.4, text: "有点意思" }); const peaks = danmakuPeaks(burst.sort((a, b) => a.t - b.t), 1200); expect(peaks.length).toBe(1); expect(peaks[0].startSec).toBeLessThanOrEqual(500); // 全场都是 6 条/窗:窗间没有跳升,中位数阈值也随基线抬高 → 不圈 const warm: DanmakuItem[] = []; for (let t = 0; t < 1200; t += 1.7) warm.push({ t: Number(t.toFixed(1)), text: "一直聊" }); expect(danmakuPeaks(warm, 1200)).toEqual([]); }); }); describe("danmakuPathFor", () => { it("同名 .xml(录播姬约定)", () => { expect(danmakuPathFor("/rec/直播回放-2026.flv")).toBe("/rec/直播回放-2026.xml"); expect(danmakuPathFor("/rec/a.b.mp4")).toBe("/rec/a.b.xml"); }); }); describe("parseDouyinDanmakuJsonl", () => { it("chat 计弹幕;gift/social/like 按互动档;member/roomStats 不计", () => { const jsonl = [ `{"type":"chat","content":"主播牛的","userId":123456,"recvTimeSec":10.5}`, `{"type":"gift","giftName":"小心心","comboCount":10,"userName":"阿强","recvTimeSec":20}`, `{"type":"social","userName":"路人甲","action":1,"recvTimeSec":30}`, `{"type":"like","count":15,"total":9999,"userName":"路人乙","recvTimeSec":40}`, `{"type":"member","userName":"新观众","memberCount":50,"recvTimeSec":50}`, `{"type":"roomStats","displayLong":"1234人在线","recvTimeSec":60}`, ].join("\n"); const items = parseDouyinDanmakuJsonl(jsonl); expect(items.map((d) => d.t)).toEqual([10.5, 20, 30, 40]); // member/roomStats 被跳过 expect(items[0]).toMatchObject({ text: "主播牛的", uid: "123456" }); expect(items[0].boost).toBeUndefined(); // 普通弹幕走高能词加权 expect(items[1]).toMatchObject({ text: "小心心", boost: 1, uid: "阿强" }); expect(items[2].boost).toBe(2); // 关注是用行动投票 expect(items[3].boost).toBe(1); }); it("坏行/空行跳过(录制进程被杀时最后一行常是半截)", () => { const jsonl = `{"type":"chat","content":"完整的","recvTimeSec":5}\n\n{"type":"chat","content":"半截`; const items = parseDouyinDanmakuJsonl(jsonl); expect(items).toHaveLength(1); expect(items[0].text).toBe("完整的"); }); it("纪元时间戳不认——对不上时间轴的消息宁可丢,不能落在第 0 秒污染开头", () => { const jsonl = [ `{"type":"chat","content":"只有纪元秒","timestamp":1720000000}`, `{"type":"chat","content":"相对秒优先","recvTimeSec":12,"timestamp":1720000000}`, ].join("\n"); const items = parseDouyinDanmakuJsonl(jsonl); expect(items).toHaveLength(1); expect(items[0]).toMatchObject({ text: "相对秒优先", t: 12 }); }); it("垃圾输入返回空", () => { expect(parseDouyinDanmakuJsonl("")).toEqual([]); expect(parseDouyinDanmakuJsonl("not json at all")).toEqual([]); }); }); describe("danmakuPathsFor", () => { it("按优先级:同名 .xml → 同名 .jsonl → {首段}_danmaku.jsonl", () => { expect(danmakuPathsFor("/rec/300294032039_merged.mp4")).toEqual([ "/rec/300294032039_merged.xml", "/rec/300294032039_merged.jsonl", "/rec/300294032039_danmaku.jsonl", ]); }); it("视频名没有下划线时只有前两个候选", () => { expect(danmakuPathsFor("/rec/回放.flv")).toEqual(["/rec/回放.xml", "/rec/回放.jsonl"]); }); }); describe("danmakuHeatCurve", () => { it("按格加权计数并归一到 0..1,爆发格最热", () => { const items: DanmakuItem[] = []; for (let t = 0; t < 600; t += 10) items.push({ t, text: "普通聊天" }); for (let i = 0; i < 20; i++) items.push({ t: 300 + i * 0.3, text: "哈哈哈哈" }); const c = danmakuHeatCurve(items, 600, 120); expect(c).toHaveLength(120); expect(Math.max(...c)).toBe(1); const peakBin = Math.floor((302 / 600) * 120); expect(c[peakBin]).toBeGreaterThan(c[10]); }); it("空输入/非法时长返回空", () => { expect(danmakuHeatCurve([], 600, 120)).toEqual([]); expect(danmakuHeatCurve([{ t: 1, text: "x" }], 0, 120)).toEqual([]); }); }); - src/core/__tests__/diarize.test.ts (3273 bytes)
import { describe, expect, it } from "vitest"; import { assignWordSpeakers, speakerCount, dominantSpeaker, labelTranscript, type SpeakerTurn } from "../diarize"; import type { Transcript, TranscriptWord } from "../../shared/api-types"; const w = (text: string, s: number, e: number): TranscriptWord => ({ text, startSec: s, endSec: e }); describe("assignWordSpeakers", () => { const turns: SpeakerTurn[] = [ { startSec: 0, endSec: 3, speaker: 0 }, { startSec: 3.4, endSec: 6, speaker: 1 }, ]; it("labels words by maximum overlap", () => { const out = assignWordSpeakers([w("甲", 0.5, 1.0), w("乙", 4.0, 4.5)], turns); expect(out[0].speaker).toBe(0); expect(out[1].speaker).toBe(1); }); it("a word straddling a turn change goes to the side covering more of it", () => { const out = assignWordSpeakers([w("跨", 2.8, 3.6)], turns); expect(out[0].speaker).toBe(0); // 0.2s with A vs 0.2s with B — tie keeps first max; adjust span const out2 = assignWordSpeakers([w("跨", 2.9, 3.8)], turns); expect(out2[0].speaker).toBe(1); // 0.1s vs 0.4s }); it("gap words adopt the nearest turn only within the tolerance", () => { const out = assignWordSpeakers([w("近", 3.15, 3.35), w("远", 8.0, 8.4)], turns); expect(out[0].speaker).toBe(1); // 0.05s from turn B vs 0.15s from turn A expect(out[1].speaker).toBeUndefined(); // 2s past the last turn }); it("no turns → words pass through untouched", () => { const words = [w("原", 0, 1)]; expect(assignWordSpeakers(words, [])).toBe(words); }); }); describe("speakerCount", () => { it("counts distinct ids", () => { expect(speakerCount([ { startSec: 0, endSec: 1, speaker: 0 }, { startSec: 1, endSec: 2, speaker: 1 }, { startSec: 2, endSec: 3, speaker: 0 }, ])).toBe(2); }); }); describe("dominantSpeaker", () => { it("picks the speaker with the most word-time, not word-count", () => { const words = [ { ...w("a", 0, 0.2), speaker: 1 }, { ...w("b", 0.2, 0.4), speaker: 1 }, { ...w("c", 0.4, 2.0), speaker: 0 }, // one long word outweighs two short ]; expect(dominantSpeaker(words)).toBe(0); }); it("is undefined when no word carries a speaker", () => { expect(dominantSpeaker([w("x", 0, 1)])).toBeUndefined(); }); }); describe("labelTranscript", () => { const transcript: Transcript = { language: "zh", engine: "test", durationSec: 6, segments: [ { id: 1, startSec: 0, endSec: 2.5, text: "甲说的话", words: [w("甲说", 0.2, 1.0), w("的话", 1.0, 2.2)] }, { id: 2, startSec: 3.4, endSec: 5.5, text: "乙回应", words: [w("乙回", 3.6, 4.4), w("应", 4.4, 5.2)] }, ], }; const turns: SpeakerTurn[] = [ { startSec: 0, endSec: 3, speaker: 0 }, { startSec: 3.4, endSec: 6, speaker: 1 }, ]; it("labels words and sets each segment's dominant speaker", () => { const out = labelTranscript(transcript, turns); expect(out.segments[0].speaker).toBe(0); expect(out.segments[1].speaker).toBe(1); expect(out.segments[0].words.every((x) => x.speaker === 0)).toBe(true); }); it("returns the original transcript when no turns", () => { expect(labelTranscript(transcript, [])).toBe(transcript); }); }); - src/core/__tests__/doctor.test.ts (10104 bytes)
import { describe, it, expect, afterEach, vi } from "vitest"; import { mkdtemp, mkdir, rm, writeFile } from "fs/promises"; import { join } from "path"; import { tmpdir } from "os"; import { createHash } from "crypto"; import { runDoctor, dirSize } from "../doctor"; let root: string; afterEach(async () => { vi.unstubAllGlobals(); if (root) await rm(root, { recursive: true, force: true }); }); async function freshRoot(): Promise<{ modelsRoot: string; cacheDir: string }> { root = await mkdtemp(join(tmpdir(), "hotclip-doctor-")); const modelsRoot = join(root, "models"); const cacheDir = join(root, "cache"); await mkdir(modelsRoot, { recursive: true }); return { modelsRoot, cacheDir }; } // 单测不赌 runner 的 ffmpeg/ffprobe 环境(Linux 上 ffprobe 曾跑不起来,Windows // 上连 /bin/echo 都没有):路径解析和版本探测都注入假实现,不碰真进程。 const fakeBins = { ffmpeg: () => "/fake/ffmpeg", ffprobe: () => "/fake/ffprobe" }; const fakeProbe = async (): Promise<string> => "fake version 1.0\n"; describe("dirSize", () => { it("递归求和,不存在按 0", async () => { const { modelsRoot } = await freshRoot(); await mkdir(join(modelsRoot, "a/b"), { recursive: true }); await writeFile(join(modelsRoot, "a/x.bin"), Buffer.alloc(10)); await writeFile(join(modelsRoot, "a/b/y.bin"), Buffer.alloc(5)); expect(await dirSize(join(modelsRoot, "a"))).toBe(15); expect(await dirSize(join(modelsRoot, "不存在"))).toBe(0); }); }); describe("runDoctor", () => { it("空环境:核心模型报未安装并进 missingCoreModels,LLM 未配置是 warn 不是 fail", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const report = await runDoctor({ modelsRoot, cacheDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); // 默认管线五个核心模型都缺 expect(report.missingCoreModels.map((a) => a.id)).toEqual([ "sensevoice-2024-07-17", "yunet-2023mar", "emotion-ferplus-8", "transnetv2-onnx", "silero-vad-v6", ]); const sv = report.checks.find((c) => c.name.includes("SenseVoice")); expect(sv?.status).toBe("warn"); expect(sv?.detail).toContain("未安装"); expect(sv?.fix).toContain("预下载"); // 可选模型缺失不告警 const fireRed = report.checks.find((c) => c.name.includes("FireRed")); expect(fireRed?.status).toBe("ok"); const llm = report.checks.find((c) => c.name === "LLM 配置"); expect(llm?.status).toBe("warn"); expect(llm?.fix).toContain("HOTCLIP_LLM_BASE_URL"); // 二进制可用(注入的假二进制)时报 ok expect(report.checks.find((c) => c.name === "ffmpeg")?.status).toBe("ok"); expect(report.checks.find((c) => c.name === "ffprobe")?.status).toBe("ok"); }); it("二进制解析失败报 fail 并给修复建议", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const report = await runDoctor({ modelsRoot, cacheDir, llm: null, resolveBinaries: { ffmpeg: () => "/bin/echo", ffprobe: () => { throw new Error("no binary for this platform"); }, }, }); const ffprobe = report.checks.find((c) => c.name === "ffprobe"); expect(ffprobe?.status).toBe("fail"); expect(ffprobe?.fix).toContain("重新安装应用"); }); it("已装模型报体积;断点文件报续传量", async () => { const { modelsRoot, cacheDir } = await freshRoot(); // YuNet 装好(singleFile 形态) await mkdir(join(modelsRoot, "yunet-2023mar"), { recursive: true }); await writeFile(join(modelsRoot, "yunet-2023mar", "model.onnx"), Buffer.alloc(1024 * 1024)); // SenseVoice 留一个断点文件 await writeFile(join(modelsRoot, "sensevoice-2024-07-17.download.tar.bz2"), Buffer.alloc(2 * 1024 * 1024)); const report = await runDoctor({ modelsRoot, cacheDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); const yunet = report.checks.find((c) => c.name.includes("YuNet")); expect(yunet?.status).toBe("ok"); expect(yunet?.detail).toContain("已安装"); const sv = report.checks.find((c) => c.name.includes("SenseVoice")); expect(sv?.detail).toContain("断点"); expect(report.missingCoreModels.map((a) => a.id)).not.toContain("yunet-2023mar"); }); it("下载器缺失不告警,已缓存时校验完整性", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const toolsDir = join(root, "tools"); let report = await runDoctor({ modelsRoot, cacheDir, toolsDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id === "downloader")).toMatchObject({ status: "ok" }); await mkdir(toolsDir, { recursive: true }); const binary = join(toolsDir, process.platform === "win32" ? "yt-dlp.exe" : "yt-dlp"); const bytes = Buffer.from("verified-tool"); await writeFile(binary, bytes); await writeFile(`${binary}.sha256`, `${createHash("sha256").update(bytes).digest("hex")}\n`); report = await runDoctor({ modelsRoot, cacheDir, toolsDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id === "downloader")).toMatchObject({ status: "ok", detail: expect.stringContaining("校验通过") }); await writeFile(`${binary}.sha256`, "bad"); report = await runDoctor({ modelsRoot, cacheDir, toolsDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id === "downloader")).toMatchObject({ status: "warn", fix: expect.stringContaining("自动删除") }); }); it("英文桌面报告不混入中文操作文案", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const report = await runDoctor({ modelsRoot, cacheDir, llm: null, zh: false, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id.startsWith("model:"))).toMatchObject({ name: expect.stringContaining("transcription"), detail: expect.stringContaining("Not installed") }); expect(report.checks.find((c) => c.id === "llm")).toMatchObject({ name: "LLM configuration", fix: expect.stringContaining("AI model settings") }); expect(report.checks.find((c) => c.id === "cache")?.detail).toContain("Empty"); }); it("可选渲染缓存检查显示占用并支持英文报告", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const renderCacheDir = join(root, "render-cache"); await mkdir(renderCacheDir, { recursive: true }); await writeFile(join(renderCacheDir, "base.mp4"), Buffer.alloc(1024 * 1024)); let report = await runDoctor({ modelsRoot, cacheDir, renderCacheDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id === "render-cache")).toMatchObject({ name: "基础渲染缓存", status: "ok", detail: expect.stringContaining("1MB") }); report = await runDoctor({ modelsRoot, cacheDir, renderCacheDir, llm: null, zh: false, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.id === "render-cache")).toMatchObject({ name: "Render cache", detail: expect.stringContaining("limited to 1GB") }); }); it("多模态证据索引独立显示占用和 64MB 上限", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const evidenceCacheDir = join(root, "evidence-index"); await mkdir(evidenceCacheDir, { recursive: true }); await writeFile(join(evidenceCacheDir, "signals.json"), Buffer.alloc(2 * 1024 * 1024)); let report = await runDoctor({ modelsRoot, cacheDir, evidenceCacheDir, llm: null, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((check) => check.id === "evidence-index")).toMatchObject({ name: "多模态证据索引", status: "ok", detail: expect.stringContaining("2MB"), }); report = await runDoctor({ modelsRoot, cacheDir, evidenceCacheDir, llm: null, zh: false, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((check) => check.id === "evidence-index")).toMatchObject({ name: "Multimodal evidence index", detail: expect.stringContaining("limited to 64MB"), }); }); it("LLM 端点:区分成功、路由错误、凭据错误和网络错误", async () => { const { modelsRoot, cacheDir } = await freshRoot(); const llm = { baseUrl: "http://127.0.0.1:1/v1", apiKey: "k", model: "m" }; const fetchMock = vi.fn(async () => new Response("{}", { status: 200 })); vi.stubGlobal("fetch", fetchMock); let report = await runDoctor({ modelsRoot, cacheDir, llm, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.name === "LLM 端点")?.status).toBe("ok"); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining("/models"), expect.objectContaining({ headers: { Authorization: "Bearer k" } })); vi.stubGlobal("fetch", vi.fn(async () => new Response("{}", { status: 404 }))); report = await runDoctor({ modelsRoot, cacheDir, llm, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.name === "LLM 端点")).toMatchObject({ status: "warn", id: "llm" }); vi.stubGlobal("fetch", vi.fn(async () => new Response("{}", { status: 401 }))); report = await runDoctor({ modelsRoot, cacheDir, llm, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); expect(report.checks.find((c) => c.name === "LLM 端点")).toMatchObject({ status: "fail", id: "llm" }); vi.stubGlobal("fetch", vi.fn(async () => Promise.reject(new Error("ECONNREFUSED")))); report = await runDoctor({ modelsRoot, cacheDir, llm, resolveBinaries: fakeBins, probeBinaryVersion: fakeProbe }); const check = report.checks.find((c) => c.name === "LLM 端点"); expect(check?.status).toBe("warn"); expect(check?.detail).toContain("连不上"); }); }); - src/core/__tests__/draft-cues.test.ts (1481 bytes)
import { expect, it } from "vitest"; import { buildCaptionAss, VERTICAL_LAYOUT } from "../subtitle"; import { draftCuesFromAss, shiftDraftCues, trimDraftSegments } from "../draft-cues"; it("preserves rendered text, plate colour and relative placement while removing ASS control codes", () => { const ass = buildCaptionAss([{ text: "first", startSec: 5, endSec: 6 }], 5, VERTICAL_LAYOUT, "minimal", { titleCard: { text: "Round, one", style: "brandPlate", durationSec: 2 }, highlightHex: "#FFDD00", }); const cues = draftCuesFromAss(ass); const title = cues.find((cue) => cue.role === "Title")!; expect(title).toMatchObject({ text: "Round, one", startSec: 0, endSec: 2, look: { color: "#000000", border: { color: "#FFDD00", box: true } } }); expect(title.look!.y).toBeGreaterThan(0); const caption = cues.find((cue) => cue.role === "Caption")!; expect(caption.text).toBe("first"); expect(caption.look!.y).toBeLessThan(0); }); it("keeps repeated intro footage, later source segments and edited captions aligned after QA trims", () => { const edl = trimDraftSegments([{ startSec: 20, endSec: 22 }, { startSec: 5, endSec: 8 }, { startSec: 20, endSec: 24 }], .5, 7); expect(edl).toEqual([{ startSec: 20.5, endSec: 22 }, { startSec: 5, endSec: 8 }, { startSec: 20, endSec: 22.5 }]); const shifted = shiftDraftCues([{ role: "Caption", startSec: 1, endSec: 3, text: "body" }], 1.5, 4); expect(shifted[0]).toMatchObject({ startSec: 2.5, endSec: 4 }); }); - src/core/__tests__/edit-transcript.test.ts (2520 bytes)
import { describe, it, expect } from "vitest"; import { tokenizeForWords, rebuildWords, editSegmentText } from "../../shared/edit-transcript"; import type { Transcript } from "../../shared/api-types"; describe("tokenizeForWords", () => { it("CJK 逐字,latin 按词,标点附着前词", () => { expect(tokenizeForWords("你好世界")).toEqual(["你", "好", "世", "界"]); expect(tokenizeForWords("hello world")).toEqual(["hello", "world"]); expect(tokenizeForWords("对,就是 GPT-5 啊!")).toEqual(["对,", "就", "是", "GPT-5", "啊!"]); }); it("空串/纯空白返回空;开头标点独立成词后续附着", () => { expect(tokenizeForWords(" ")).toEqual([]); expect(tokenizeForWords("——转折")).toEqual(["——", "转", "折"]); }); }); describe("rebuildWords", () => { it("时间无缝铺满区间,首尾精确对齐", () => { const words = rebuildWords("你好word", 10, 14); expect(words[0].startSec).toBe(10); expect(words[words.length - 1].endSec).toBe(14); expect(words.every((word) => word.timingSource === "edited")).toBe(true); for (let i = 1; i < words.length; i++) { expect(words[i].startSec).toBeCloseTo(words[i - 1].endSec, 10); } }); it("CJK 字比 latin 字符占更多时长(视觉宽度权重)", () => { const words = rebuildWords("好ok", 0, 3); // 好=2,ok=2 → 各 1.5s expect(words[0].endSec).toBeCloseTo(1.5, 5); }); it("空文本/零时长返回空", () => { expect(rebuildWords("", 0, 5)).toEqual([]); expect(rebuildWords("字", 5, 5)).toEqual([]); }); }); describe("editSegmentText", () => { const t: Transcript = { language: "zh", engine: "x", durationSec: 20, segments: [ { id: 1, startSec: 0, endSec: 4, text: "错误的橘子", words: [{ text: "错", startSec: 0, endSec: 4 }] }, { id: 2, startSec: 5, endSec: 9, text: "第二句", words: [] }, ], }; it("替换文本并重建该句词轴,其余句原样,原对象不变", () => { const next = editSegmentText(t, 1, "正确的句子"); expect(next.segments[0].text).toBe("正确的句子"); expect(next.segments[0].words.length).toBe(5); expect(next.segments[0].words[0].startSec).toBe(0); expect(next.segments[0].words[4].endSec).toBe(4); expect(next.segments[1]).toBe(t.segments[1]); expect(t.segments[0].text).toBe("错误的橘子"); // 不可变 }); it("空文本视为误操作,原样返回", () => { expect(editSegmentText(t, 1, " ")).toBe(t); }); }); - src/core/__tests__/edl.test.ts (3253 bytes)
import { describe, it, expect } from "vitest"; import { secToTimecode, buildEdl } from "../edl"; describe("secToTimecode", () => { it("SMPTE 非丢帧时间码,帧数按 fps 换算", () => { expect(secToTimecode(0, 30)).toBe("00:00:00:00"); expect(secToTimecode(65.5, 30)).toBe("00:01:05:15"); expect(secToTimecode(3661.2, 25)).toBe("01:01:01:05"); }); it("非整数 fps(29.97)舍入到整数帧基;负数夹零", () => { expect(secToTimecode(1, 29.97)).toBe("00:00:01:00"); expect(secToTimecode(-5, 30)).toBe("00:00:00:00"); }); }); describe("buildEdl", () => { it("CMX3600 结构:标题/FCM/事件行/注释行,record 侧连续累计", () => { const edl = buildEdl({ title: "回放 - HotClip", sourceName: "回放.mp4", fps: 30, clips: [ { title: "爆点一", segments: [{ startSec: 60, endSec: 70 }] }, { title: "爆点二", segments: [{ startSec: 200, endSec: 215 }] }, ], }); expect(edl).toContain("TITLE: 回放 - HotClip"); expect(edl).toContain("FCM: NON-DROP FRAME"); expect(edl).toContain("001 AX B C 00:01:00:00 00:01:10:00 00:00:00:00 00:00:10:00"); // 第二条 record 从 10s 继续 expect(edl).toContain("002 AX B C 00:03:20:00 00:03:35:00 00:00:10:00 00:00:25:00"); expect(edl).toContain("* FROM CLIP NAME: 回放.mp4"); expect(edl).toContain("* COMMENT: 爆点一"); }); it("跳剪多段:一条切片拆成多个事件,源片跳跃 record 连续", () => { const edl = buildEdl({ title: "t", sourceName: "s.mp4", fps: 30, clips: [ { title: "跳剪片", segments: [ { startSec: 100, endSec: 104 }, { startSec: 106, endSec: 110 }, // 中间 2s 被跳剪剪掉 ], }, ], }); const events = edl.split("\n").filter((l) => /^\d{3} {2}AX/.test(l)); expect(events.length).toBe(2); expect(events[0]).toContain("00:01:40:00 00:01:44:00 00:00:00:00 00:00:04:00"); expect(events[1]).toContain("00:01:46:00 00:01:50:00 00:00:04:00 00:00:08:00"); }); it("零时长段跳过,事件编号仍连续", () => { const edl = buildEdl({ title: "t", sourceName: "s.mp4", fps: 30, clips: [ { title: "a", segments: [{ startSec: 5, endSec: 5 }, { startSec: 10, endSec: 12 }] }, ], }); const events = edl.split("\n").filter((l) => /^\d{3} {2}AX/.test(l)); expect(events.length).toBe(1); expect(events[0].startsWith("001")).toBe(true); }); }); it("uses real frames for 29.97 non-drop timecode and accumulates record frames across cuts", () => { const rate = 30000 / 1001; expect(secToTimecode(3600, rate)).toBe("00:59:56:12"); const sourceFrame = (frame: number) => frame / rate; const segments = Array.from({ length: 100 }, (_, i) => ({ startSec: sourceFrame(108000 + 2 * i + 0.49), endSec: sourceFrame(108000 + 2 * i + 1.51), })); const text = buildEdl({ title: "fps", sourceName: "ntsc.mp4", fps: rate, clips: [{ title: "one", segments }] }); expect(text).toContain("FCM: NON-DROP FRAME"); expect(text.split("\n").filter((l) => l.includes(" AX ")).at(-1)).toMatch(/00:00:06:18 00:00:06:20$/); }); - src/core/__tests__/elevenlabs.test.ts (1064 bytes)
import { describe, it, expect } from "vitest"; import { parseScribeWords } from "../transcribe/elevenlabs"; describe("parseScribeWords", () => { it("maps words, drops spacing tokens and malformed entries", () => { const words = parseScribeWords({ language_code: "zh", words: [ { text: "你好", start: 0.1, end: 0.5, type: "word" }, { text: " ", start: 0.5, end: 0.6, type: "spacing" }, { text: "世界", start: 0.6, end: 1.0, type: "word" }, { text: "坏的", type: "word" }, // no timestamps { text: "", start: 1, end: 2, type: "word" }, // empty ], }); expect(words).toEqual([ { text: "你好", startSec: 0.1, endSec: 0.5, timingSource: "native" }, { text: "世界", startSec: 0.6, endSec: 1.0, timingSource: "native" }, ]); }); it("clamps inverted end times and handles missing words array", () => { expect(parseScribeWords({})).toEqual([]); const [w] = parseScribeWords({ words: [{ text: "a", start: 2, end: 1 }] }); expect(w.endSec).toBe(2); }); }); - src/core/__tests__/emotion.test.ts (6541 bytes)
import { describe, it, expect } from "vitest"; import { planEmotionFrameTimes, grayFaceTensor, softmax, emotionPeakScore, collectEmotionSignal, EMOTION_MAX_FRAMES, EMOTION_MIN_SPACING_SEC, FER_INPUT, type EmotionDeps, } from "../emotion"; import type { MediaSignals } from "../signals"; import type { FaceBox } from "../reframe/yunet"; describe("planEmotionFrameTimes", () => { it("无信号时均匀铺满,间隔与上限成立", () => { const times = planEmotionFrameTimes(3600, undefined); expect(times.length).toBe(EMOTION_MAX_FRAMES); for (let i = 1; i < times.length; i++) { expect(times[i] - times[i - 1]).toBeGreaterThanOrEqual(EMOTION_MIN_SPACING_SEC); } }); it("信号窗口内 2s 步进采样(比全片均匀更密)", () => { const signals: MediaSignals = { loudPeaks: [{ startSec: 100, endSec: 110 }], cutDense: [], }; const times = planEmotionFrameTimes(3600, signals); const inWindow = times.filter((t) => t >= 100 && t <= 110); expect(inWindow.length).toBeGreaterThanOrEqual(3); // 10s 窗口 / 3s 间隔 }); it("时长无效返回空", () => { expect(planEmotionFrameTimes(0.5, undefined)).toEqual([]); }); }); describe("grayFaceTensor", () => { const SIZE = 64; // 用小帧测试(inputSize 参数化) function frame(fill: [number, number, number]): Uint8Array { const buf = new Uint8Array(SIZE * SIZE * 3); for (let i = 0; i < SIZE * SIZE; i++) { buf[i * 3] = fill[0]; buf[i * 3 + 1] = fill[1]; buf[i * 3 + 2] = fill[2]; } return buf; } it("输出 64×64,纯白帧≈255,纯黑帧≈0", () => { const box: FaceBox = { x: 16, y: 16, w: 32, h: 32, score: 0.9 }; const white = grayFaceTensor(frame([255, 255, 255]), SIZE, box); expect(white.length).toBe(FER_INPUT * FER_INPUT); expect(white[FER_INPUT * 32 + 32]).toBeCloseTo(255, 0); const black = grayFaceTensor(frame([0, 0, 0]), SIZE, box); expect(black[FER_INPUT * 32 + 32]).toBe(0); }); it("BT.601 灰度权重:绿色比蓝色亮", () => { const box: FaceBox = { x: 16, y: 16, w: 32, h: 32, score: 0.9 }; // BGR 帧:纯绿 vs 纯蓝 const green = grayFaceTensor(frame([0, 255, 0]), SIZE, box)[2080]; const blue = grayFaceTensor(frame([255, 0, 0]), SIZE, box)[2080]; expect(green).toBeGreaterThan(blue); expect(green).toBeCloseTo(0.587 * 255, 0); expect(blue).toBeCloseTo(0.114 * 255, 0); }); it("脸框贴边时外扩不越界(不抛错,值有限)", () => { const box: FaceBox = { x: 0, y: 0, w: 20, h: 20, score: 0.9 }; const out = grayFaceTensor(frame([128, 128, 128]), SIZE, box); for (const v of out) expect(Number.isFinite(v)).toBe(true); }); }); describe("softmax / emotionPeakScore", () => { it("softmax 归一且保序", () => { const p = softmax([1, 3, 2]); expect(p.reduce((a, b) => a + b, 0)).toBeCloseTo(1); expect(p[1]).toBeGreaterThan(p[2]); expect(p[2]).toBeGreaterThan(p[0]); }); it("大数值不溢出", () => { const p = softmax([1000, 999]); expect(p[0]).toBeGreaterThan(p[1]); expect(Number.isFinite(p[0])).toBe(true); }); it("峰值分取笑/惊/怒最大值,忽略中性/悲伤", () => { // 中性 开心 惊讶 悲伤 愤怒 厌恶 恐惧 轻蔑 expect(emotionPeakScore([0.9, 0.05, 0.02, 0, 0.03, 0, 0, 0])).toBeCloseTo(0.05); expect(emotionPeakScore([0.1, 0.2, 0.6, 0, 0.1, 0, 0, 0])).toBeCloseTo(0.6); expect(emotionPeakScore([0, 0, 0, 1, 0, 0, 0, 0])).toBe(0); // 纯悲伤不算爆点 }); }); describe("collectEmotionSignal", () => { const okFrame = new Uint8Array(640 * 640 * 3); const face: FaceBox = { x: 200, y: 200, w: 100, h: 100, score: 0.9 }; function deps(overrides: Partial<EmotionDeps> = {}): EmotionDeps { return { extractFrame: async () => okFrame, detectFaces: async () => [face], scoreEmotion: async () => [0.1, 0.8, 0.05, 0, 0.05, 0, 0, 0], // 开心 0.8 ...overrides, }; } it("正常路径:人脸+高兴 → 圈出表情峰值时段", async () => { const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 300, modelsRoot: "/tmp/x", deps: deps(), }); expect(outcome).not.toBeNull(); expect(outcome!.stats.facesScored).toBe(outcome!.stats.framesTotal); expect(outcome!.emotionPeaks.length).toBeGreaterThan(0); expect(outcome!.stats.peakCount).toBe(outcome!.emotionPeaks.length); }); it("全程无人脸(风景/游戏素材) → null", async () => { const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 300, modelsRoot: "/tmp/x", deps: deps({ detectFaces: async () => [] }), }); expect(outcome).toBeNull(); }); it("全是中性表情 → 有统计但零峰值(不给假信号)", async () => { const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 300, modelsRoot: "/tmp/x", deps: deps({ scoreEmotion: async () => [0.95, 0.02, 0.01, 0, 0.02, 0, 0, 0] }), }); expect(outcome).not.toBeNull(); expect(outcome!.emotionPeaks).toEqual([]); }); it("抽帧全失败 → null", async () => { const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 300, modelsRoot: "/tmp/x", deps: deps({ extractFrame: async () => null }), }); expect(outcome).toBeNull(); }); it("单帧推理失败跳过,不影响整体", async () => { let n = 0; const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 300, modelsRoot: "/tmp/x", deps: deps({ scoreEmotion: async () => { if (++n % 4 === 0) throw new Error("单帧失败"); return [0.2, 0.7, 0.05, 0, 0.05, 0, 0, 0]; }, }), }); expect(outcome).not.toBeNull(); expect(outcome!.stats.facesScored).toBeLessThan(outcome!.stats.framesTotal); }); it("预算耗尽带着已得结果收工", async () => { let calls = 0; const outcome = await collectEmotionSignal({ videoPath: "/v.mp4", durationSec: 3600, modelsRoot: "/tmp/x", budgetMs: 80, deps: deps({ extractFrame: async () => { calls++; await new Promise((r) => setTimeout(r, 15)); return okFrame; }, }), }); expect(calls).toBeLessThan(EMOTION_MAX_FRAMES); if (outcome) expect(outcome.stats.facesScored).toBeGreaterThanOrEqual(3); }); }); - src/core/__tests__/evidence-index.test.ts (8521 bytes)
import { afterEach, describe, expect, it, vi } from "vitest"; import { mkdtemp, readFile, readdir, rm, stat, utimes, writeFile } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; import { clearEvidenceIndex, createEvidenceEntryKey, fingerprintEvidenceSource, inspectEvidenceIndex, pruneEvidenceIndex, readEvidence, writeEvidence, } from "../evidence-index"; import { collectSignalsEvidence, collectVisionEvidence, detectShotBoundariesEvidence, detectSpeechActivityEvidence } from "../media-evidence"; let root = ""; async function fresh(): Promise<{ dir: string; sourcePath: string }> { root = await mkdtemp(join(tmpdir(), "hotclip-evidence-index-")); const sourcePath = join(root, "source.mp4"); await writeFile(sourcePath, "source-v1"); return { dir: join(root, "evidence"), sourcePath }; } afterEach(async () => { if (root) await rm(root, { recursive: true, force: true }); root = ""; }); describe("evidence index lifecycle", () => { it("keys by exact source version and capability", async () => { const { sourcePath } = await fresh(); const first = await fingerprintEvidenceSource(sourcePath); expect(createEvidenceEntryKey(first, "signals:v1")).not.toBe(createEvidenceEntryKey(first, "shots:v1")); await writeFile(sourcePath, "source-v2-longer"); const changed = await fingerprintEvidenceSource(sourcePath); expect(createEvidenceEntryKey(first, "signals:v1")).not.toBe(createEvidenceEntryKey(changed, "signals:v1")); }); it("atomically writes, validates, touches, and clears only evidence", async () => { const { dir, sourcePath } = await fresh(); const project = join(root, "project.hotclip"); await writeFile(project, "keep"); const source = await fingerprintEvidenceSource(sourcePath); expect(await writeEvidence(dir, source, "test:v1", { ok: true })).toBe(true); const validator = (value: unknown): value is { ok: boolean } => !!value && typeof value === "object" && typeof (value as { ok?: unknown }).ok === "boolean"; expect(await readEvidence(dir, source, "test:v1", validator)).toEqual({ ok: true }); expect(await inspectEvidenceIndex(dir)).toMatchObject({ entries: 1 }); await clearEvidenceIndex(dir); expect(await inspectEvidenceIndex(dir)).toEqual({ bytes: 0, entries: 0 }); expect(await readFile(project, "utf8")).toBe("keep"); }); it("drops invalid entries and prunes least-recently-used JSON to the byte budget", async () => { const { dir, sourcePath } = await fresh(); const source = await fingerprintEvidenceSource(sourcePath); await writeEvidence(dir, source, "a", { text: "a".repeat(200) }); await writeEvidence(dir, source, "b", { text: "b".repeat(200) }); const files = (await readdir(dir)).filter((name) => name.endsWith(".json")).sort(); await utimes(join(dir, files[0]), new Date(1_000), new Date(1_000)); await utimes(join(dir, files[1]), new Date(2_000), new Date(2_000)); const newestSize = (await stat(join(dir, files[1]))).size; expect(await pruneEvidenceIndex(dir, newestSize)).toEqual({ bytes: newestSize, entries: 1 }); const remaining = (await readdir(dir)).find((name) => name.endsWith(".json")); expect(remaining).toBeTruthy(); await writeFile(join(dir, remaining!), "not json"); const capability = remaining === `${createEvidenceEntryKey(source, "a")}.json` ? "a" : "b"; expect(await readEvidence(dir, source, capability, (_value): _value is unknown => true)).toBeUndefined(); }); }); describe("capability adapters", () => { it("reuses Tier-0 signals and range-scoped shot boundaries", async () => { const { dir, sourcePath } = await fresh(); const collect = vi.fn(async () => ({ loudPeaks: [], cutDense: [], motionPeaks: [{ startSec: 2, endSec: 4 }] })); const first = await collectSignalsEvidence({ videoPath: sourcePath, evidenceDir: dir, collect }); const second = await collectSignalsEvidence({ videoPath: sourcePath, evidenceDir: dir, collect }); expect(second).toEqual(first); expect(collect).toHaveBeenCalledTimes(1); const detect = vi.fn(async () => [1.25, 3.5]); expect(await detectShotBoundariesEvidence({ videoPath: sourcePath, startSec: 0, endSec: 5, modelsRoot: root, evidenceDir: dir, detect })).toEqual([1.25, 3.5]); expect(await detectShotBoundariesEvidence({ videoPath: sourcePath, startSec: 0, endSec: 5, modelsRoot: root, evidenceDir: dir, detect })).toEqual([1.25, 3.5]); expect(detect).toHaveBeenCalledTimes(1); await detectShotBoundariesEvidence({ videoPath: sourcePath, startSec: 1, endSec: 5, modelsRoot: root, evidenceDir: dir, detect }); expect(detect).toHaveBeenCalledTimes(2); }); it("isolates cached visual evidence by selected source picture", async () => { const { dir, sourcePath } = await fresh(); const collect = vi.fn(async () => ({ loudPeaks: [], cutDense: [] })); await collectSignalsEvidence({ videoPath: sourcePath, evidenceDir: dir, collect, analysis: { videoStreamIndex: 0 }, }); await collectSignalsEvidence({ videoPath: sourcePath, evidenceDir: dir, collect, analysis: { videoStreamIndex: 1 }, }); await collectSignalsEvidence({ videoPath: sourcePath, evidenceDir: dir, collect, analysis: { videoStreamIndex: 1 }, }); expect(collect).toHaveBeenCalledTimes(2); const detect = vi.fn(async () => [2]); const common = { videoPath: sourcePath, startSec: 0, endSec: 5, modelsRoot: root, evidenceDir: dir, detect, }; await detectShotBoundariesEvidence({ ...common, analysis: { videoStreamIndex: 0 } }); await detectShotBoundariesEvidence({ ...common, analysis: { videoStreamIndex: 1 } }); await detectShotBoundariesEvidence({ ...common, analysis: { videoStreamIndex: 1 } }); expect(detect).toHaveBeenCalledTimes(2); }); it("scopes speech evidence by exact range and selected audio stream", async () => { const { dir, sourcePath } = await fresh(); const detect = vi.fn(async () => [{ startSec: 1, endSec: 2 }]); const common = { mediaPath: sourcePath, startSec: 0, endSec: 5, modelsRoot: root, evidenceDir: dir, detect, }; await detectSpeechActivityEvidence({ ...common, audioStreamIndex: 1 }); await detectSpeechActivityEvidence({ ...common, audioStreamIndex: 1 }); await detectSpeechActivityEvidence({ ...common, audioStreamIndex: 2 }); await detectSpeechActivityEvidence({ ...common, startSec: 1, audioStreamIndex: 2 }); expect(detect).toHaveBeenCalledTimes(3); }); it("reuses visual outcomes across API-key changes but not model changes", async () => { const { dir, sourcePath } = await fresh(); const chat = vi.fn(async (_llm, _system, _user, _image) => JSON.stringify({ cells: Array.from({ length: 9 }, (_, index) => ({ i: index + 1, energy: index + 1, note: `frame-${index + 1}` })), })); const common = { videoPath: sourcePath, durationSec: 300, evidenceDir: dir, composeSheet: async () => "jpeg", chat, config: { baseUrl: "http://localhost:11434/v1", model: "vision-a", apiKey: "first" }, }; const first = await collectVisionEvidence(common); const calls = chat.mock.calls.length; const second = await collectVisionEvidence({ ...common, config: { ...common.config, apiKey: "rotated" } }); expect(second).toEqual(first); expect(chat).toHaveBeenCalledTimes(calls); await collectVisionEvidence({ ...common, config: { ...common.config, model: "vision-b" } }); expect(chat.mock.calls.length).toBeGreaterThan(calls); }); it("does not reuse a vision verdict for a different selected video stream", async () => { const { dir, sourcePath } = await fresh(); const chat = vi.fn(async () => JSON.stringify({ cells: Array.from({ length: 9 }, (_, index) => ({ i: index + 1, energy: 8, note: "frame" })), })); const common = { videoPath: sourcePath, durationSec: 300, evidenceDir: dir, composeSheet: async () => "jpeg", chat, config: { baseUrl: "http://localhost:11434/v1", model: "vision-a" }, }; await collectVisionEvidence({ ...common, analysis: { videoStreamIndex: 0 } }); const firstCalls = chat.mock.calls.length; await collectVisionEvidence({ ...common, analysis: { videoStreamIndex: 1 } }); expect(chat.mock.calls.length).toBeGreaterThan(firstCalls); }); }); - src/core/__tests__/experiments.test.ts (3121 bytes)
import { describe, expect, it } from "vitest"; import type { PerformanceEntry, PublishLedgerItem } from "../../shared/api-types"; import { summarizeExperiments } from "../experiments"; const ledgerItem = (index: number, over: Partial<PublishLedgerItem> = {}): PublishLedgerItem => ({ contentId: `hc_v${index}`, filePath: `/exports/v${index}.mp4`, title: `版本 ${index}`, platform: "douyin", durationSec: 24, exportedAt: "2026-08-24T08:00:00.000Z", experimentId: "hcx_test", variantIndex: index, variantTotal: 2, variantRole: index === 1 ? "control" : "challenger", experimentDimensions: ["packaging"], ...over, }); const metric = (index: number, over: Partial<PerformanceEntry> = {}): PerformanceEntry => ({ contentId: `hc_v${index}`, title: `版本 ${index}`, platform: "douyin", views: 10_000, likes: index === 1 ? 300 : 800, comments: 0, shares: 0, saves: 0, publishedAt: `2026-08-24T${String(8 + index).padStart(2, "0")}:00:00.000Z`, importedAt: "2026-08-25T08:00:00.000Z", matchConfidence: "id", ...over, }); describe("local performance experiments", () => { it("reports a conservative directional lead for a complete same-platform group", () => { const summary = summarizeExperiments([ledgerItem(1), ledgerItem(2)], [metric(1), metric(2)]); expect(summary).toMatchObject({ total: 1, ready: 1, awaiting: 0, insufficient: 0 }); expect(summary.recent[0]).toMatchObject({ status: "directional", platform: "douyin", leaderContentId: "hc_v2", relativeLiftPct: 166.7, absoluteLiftPoints: 5, measuredVariants: 2, }); }); it("waits for every planned variant and all metrics", () => { expect(summarizeExperiments([ledgerItem(1)], [metric(1)]).recent[0].status).toBe("incomplete-group"); expect(summarizeExperiments([ledgerItem(1), ledgerItem(2)], [metric(1)]).recent[0].status).toBe("awaiting-metrics"); }); it.each([ ["ambiguous-metrics", [metric(1), metric(1, { platform: "bilibili" }), metric(2)]], ["platform-mismatch", [metric(1), metric(2, { platform: "bilibili" })]], ["missing-publish-time", [metric(1, { publishedAt: undefined }), metric(2)]], ["outside-window", [metric(1), metric(2, { publishedAt: "2026-08-29T09:00:00.000Z" })]], ["low-sample", [metric(1, { views: 499 }), metric(2)]], ["inconclusive", [metric(1, { likes: 300 }), metric(2, { likes: 310 })]], ] as const)("returns %s instead of overclaiming", (status, entries) => { expect(summarizeExperiments([ledgerItem(1), ledgerItem(2)], [...entries]).recent[0].status).toBe(status); }); it("resolves an unassigned export only when all imported variants share one platform", () => { const ledger = [ledgerItem(1, { platform: "unassigned" }), ledgerItem(2, { platform: "unassigned" })]; expect(summarizeExperiments(ledger, [metric(1, { platform: "xiaohongshu" }), metric(2, { platform: "xiaohongshu" })]).recent[0].platform).toBe("xiaohongshu"); expect(summarizeExperiments(ledger, [metric(1), metric(2, { platform: "bilibili" })]).recent[0].status).toBe("platform-mismatch"); }); }); - src/core/__tests__/export-audio-enhancement.integration.test.ts (3783 bytes)
import { mkdtemp, readFile, rm } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; import { afterEach, describe, expect, it, vi } from "vitest"; vi.mock("../cut", async (importOriginal) => { const actual = await importOriginal<typeof import("../cut")>(); return { ...actual, cutClip: vi.fn(actual.cutClip) }; }); vi.mock("../speech-enhancement", async (importOriginal) => { const actual = await importOriginal<typeof import("../speech-enhancement")>(); return { ...actual, applySmartDenoiseWithFallback: vi.fn(async () => ({ requested: "smart" as const, applied: "learned" as const, modelId: "dpdfnet2-48khz-hr", sampleRate: 48_000, channels: 1, })), }; }); import { cutClip, runFfmpeg } from "../cut"; import { exportClips } from "../export"; import { applySmartDenoiseWithFallback } from "../speech-enhancement"; const dirs: string[] = []; afterEach(async () => { vi.clearAllMocks(); await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); }); async function makeSource(path: string): Promise<void> { await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=24:duration=1.4", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=1.4", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", path, ]); } describe("export smart dialogue enhancement", () => { it("defers denoise and loudness to the final smart pass and records the effective tier", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-export-speech-")); dirs.push(root); const source = join(root, "source.mp4"); const outDir = join(root, "out"); await makeSource(source); const [result] = await exportClips( source, [{ id: 1, title: "speech", startSec: 0, endSec: 1.2 }], outDir, { vertical: false, denoise: true, denoiseMode: "smart", normalizeLoudness: true, modelsRoot: join(root, "models"), qa: false, } ); const options = vi.mocked(cutClip).mock.calls[0]?.[4]; expect(options).toMatchObject({ denoise: false, normalizeLoudness: false }); expect(applySmartDenoiseWithFallback).toHaveBeenCalledOnce(); expect(applySmartDenoiseWithFallback).toHaveBeenCalledWith( result.path, join(root, "models"), true, undefined ); expect(result.audioEnhancement).toBe("learned"); const metadata = JSON.parse(await readFile(join(outDir, "clips.json"), "utf8")) as { options: { denoiseMode: string }; clips: Array<{ render: { loudnessNormalized: boolean; audioEnhancement: { applied: string } } }>; }; expect(metadata.options.denoiseMode).toBe("smart"); expect(metadata.clips[0].render).toMatchObject({ loudnessNormalized: true, audioEnhancement: { applied: "learned", modelId: "dpdfnet2-48khz-hr" }, }); }, 30_000); it("keeps the legacy basic filter inside the base cut", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-export-basic-")); dirs.push(root); const source = join(root, "source.mp4"); const outDir = join(root, "out"); await makeSource(source); const [result] = await exportClips( source, [{ id: 1, title: "basic", startSec: 0, endSec: 1.2 }], outDir, { vertical: false, denoise: true, denoiseMode: "basic", normalizeLoudness: true, qa: false } ); const options = vi.mocked(cutClip).mock.calls[0]?.[4]; expect(options).toMatchObject({ denoise: true, normalizeLoudness: true }); expect(applySmartDenoiseWithFallback).not.toHaveBeenCalled(); expect(result.audioEnhancement).toBe("basic"); }, 30_000); }); - src/core/__tests__/export-color.integration.test.ts (5167 bytes)
import { beforeEach, describe, expect, it, vi } from "vitest"; import { mkdtemp, readFile, rm } from "fs/promises"; import { tmpdir } from "os"; import { join } from "path"; vi.mock("../probe", async (importOriginal) => { const actual = await importOriginal<typeof import("../probe")>(); return { ...actual, probeMedia: vi.fn(actual.probeMedia) }; }); vi.mock("../media-evidence", async (importOriginal) => { const actual = await importOriginal<typeof import("../media-evidence")>(); return { ...actual, collectSignalsEvidence: vi.fn(actual.collectSignalsEvidence) }; }); import { runFfmpeg } from "../cut"; import { exportClips } from "../export"; import { collectSignalsEvidence } from "../media-evidence"; import { probeMedia } from "../probe"; interface ReceiptClip { title: string; colorConverted: boolean; colorConversionSkipped: boolean; colorInspectionFailed: boolean; render: { visualEnhance: unknown; color: { detected: string; action: string } | null; } | null; } async function makeSource(path: string, color: "transfer-only-hdr" | "sdr"): Promise<void> { await runFfmpeg([ "-hide_banner", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=24:duration=2", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=2", "-c:v", "ffv1", "-level", "3", "-pix_fmt", "yuv420p10le", "-color_primaries", color === "transfer-only-hdr" ? "unknown" : "bt709", "-color_trc", color === "transfer-only-hdr" ? "smpte2084" : "bt709", "-colorspace", color === "transfer-only-hdr" ? "unknown" : "bt709", "-color_range", color === "transfer-only-hdr" ? "unknown" : "tv", "-c:a", "pcm_s16le", "-shortest", path, ]); } async function readReceipt(outDir: string): Promise<{ clips: ReceiptClip[] }> { return JSON.parse(await readFile(join(outDir, "clips.json"), "utf8")) as { clips: ReceiptClip[] }; } describe("exportClips HDR colour receipts", () => { beforeEach(async () => { const actual = await vi.importActual<typeof import("../probe")>("../probe"); vi.mocked(probeMedia).mockReset().mockImplementation(actual.probeMedia); vi.mocked(collectSignalsEvidence).mockClear(); }); it("marks transfer-only HDR clips and their compilation as skipped without SDR auto-enhance", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-export-color-")); const source = join(root, "transfer-only.mkv"); const outDir = join(root, "out"); try { await makeSource(source, "transfer-only-hdr"); const results = await exportClips( source, [ { id: 1, title: "first", startSec: 0, endSec: 0.8 }, { id: 2, title: "second", startSec: 1, endSec: 1.8 }, ], outDir, { vertical: false, autoEnhance: true, compilation: true, qa: false } ); expect(results).toHaveLength(3); for (const result of results) { expect(result).toMatchObject({ colorConverted: false, colorConversionSkipped: true, colorInspectionFailed: false, }); } expect(results.find((result) => result.id === 0)?.title).toBe("精华合集"); expect(collectSignalsEvidence).not.toHaveBeenCalled(); const receipt = await readReceipt(outDir); const originals = receipt.clips.filter((clip) => clip.render !== null); expect(originals).toHaveLength(2); for (const clip of originals) { expect(clip.colorConversionSkipped).toBe(true); expect(clip.render?.visualEnhance).toBeNull(); expect(clip.render?.color).toMatchObject({ detected: "pq", action: "passthrough" }); } expect(receipt.clips.find((clip) => clip.title === "精华合集")).toMatchObject({ colorConverted: false, colorConversionSkipped: true, colorInspectionFailed: false, render: null, }); } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); it("surfaces colour inspection failure and suppresses SDR auto-enhance sampling", async () => { const root = await mkdtemp(join(tmpdir(), "hotclip-export-color-probe-")); const source = join(root, "source.mkv"); const outDir = join(root, "out"); try { await makeSource(source, "sdr"); vi.mocked(probeMedia).mockRejectedValueOnce(new Error("synthetic probe failure")); const [result] = await exportClips( source, [{ id: 1, title: "probe failure", startSec: 0, endSec: 0.8 }], outDir, { vertical: false, autoEnhance: true, qa: false } ); expect(result).toMatchObject({ colorConverted: false, colorConversionSkipped: false, colorInspectionFailed: true, }); expect(collectSignalsEvidence).not.toHaveBeenCalled(); const receipt = await readReceipt(outDir); expect(receipt.clips[0]).toMatchObject({ colorConverted: false, colorConversionSkipped: false, colorInspectionFailed: true, render: { visualEnhance: null, color: null }, }); } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); }); - src/core/__tests__/export-task.test.ts (2665 bytes)
import { describe, expect, it, vi } from "vitest"; import { ExportTaskRunner, optionalExportStep } from "../export-task"; import { translateSegments } from "../translate"; describe("desktop export ownership", () => { it("rejects overlapping work without stealing cancellation and releases the slot after cleanup", async () => { const runner = new ExportTaskRunner(); let started!: () => void; const ready = new Promise<void>((resolve) => { started = resolve; }); let release!: () => void; const cleanup = new Promise<void>((resolve) => { release = resolve; }); const first = runner.run(async (signal) => { started(); await new Promise<void>((resolve) => signal.addEventListener("abort", () => resolve(), { once: true })); await cleanup; }); const rejected = expect(first).rejects.toThrow("export:cancelled"); await ready; const other = vi.fn(); await expect(runner.run(other)).rejects.toThrow("export:busy"); expect(other).not.toHaveBeenCalled(); runner.cancel(); await expect(runner.run(other)).rejects.toThrow("export:busy"); release(); await rejected; await expect(runner.run(async () => "next export")).resolves.toBe("next export"); }); it("cancels real translation preparation without continuing to optional fallback", async () => { const runner = new ExportTaskRunner(); let started!: () => void; const ready = new Promise<void>((resolve) => { started = resolve; }); const nextStep = vi.fn(); const job = runner.run(async (signal) => { await optionalExportStep(signal, () => translateSegments([ { id: 1, text: "测试", startSec: 0, endSec: 1 }, ], "en", { baseUrl: "http://127.0.0.1:1", model: "test", apiKey: "" }, async (_llm, _system, _user, inner) => { started(); return new Promise((_resolve, reject) => inner!.addEventListener("abort", () => reject(inner!.reason), { once: true })); }, signal)); nextStep(); }); const rejected = expect(job).rejects.toThrow("export:cancelled"); await ready; runner.cancel(); await rejected; expect(nextStep).not.toHaveBeenCalled(); }); it("keeps ordinary optional failures recoverable, but never launches work after cancellation", async () => { const controller = new AbortController(); await expect(optionalExportStep(controller.signal, async () => { throw new Error("offline"); })).resolves.toBeNull(); controller.abort(new Error("cancelled")); const work = vi.fn(); await expect(optionalExportStep(controller.signal, work)).rejects.toThrow("cancelled"); expect(work).not.toHaveBeenCalled(); }); }); - src/core/__tests__/export.test.ts (3526 bytes)
import { describe, it, expect } from "vitest"; import { sanitizeFilename, clipFilename, summarizeEdit, buildChapters } from "../export"; import { buildConcatList, buildConcatArgs } from "../cut"; describe("summarizeEdit", () => { const plan = (durationSec: number, segs: number) => ({ segments: Array.from({ length: segs }, () => ({})), durationSec, }); it("reports splices, kept/removed seconds, and cut ratio", () => { // 4s clip cut down to 2.1s across 2 kept segments → 1.9s removed, 47.5% expect(summarizeEdit(4, plan(2.1, 2))).toEqual({ splices: 2, keptSec: 2.1, removedSec: 1.9, cutRatio: 0.475, }); }); it("is null when nothing was spliced", () => { expect(summarizeEdit(4, null)).toBeNull(); expect(summarizeEdit(0, plan(2, 1))).toBeNull(); // guard against divide-by-zero }); it("never reports negative removal when the plan kept more than the span", () => { const out = summarizeEdit(2, plan(2.05, 1)); // rounding slack expect(out?.removedSec).toBe(0); expect(out?.cutRatio).toBe(0); }); }); describe("sanitizeFilename", () => { it("keeps CJK/latin/digits/space/dash, strips hostile chars", () => { expect(sanitizeFilename('半杯水都不渗?实测/给你看:第1集')).toBe("半杯水都不渗实测给你看第1集"); expect(sanitizeFilename('a<b>c:"d/e\\f|g?h*i')).toBe("abcdefghi"); expect(sanitizeFilename("Hello World - Ep 2")).toBe("Hello World - Ep 2"); }); it("collapses whitespace and trims", () => { expect(sanitizeFilename(" a b ")).toBe("a b"); }); it("caps length at 60 and falls back when empty", () => { expect(sanitizeFilename("好".repeat(100))).toHaveLength(60); expect(sanitizeFilename("???")).toBe("clip"); expect(sanitizeFilename("", "video")).toBe("video"); }); }); describe("clipFilename", () => { it("prefixes a zero-padded index", () => { expect(clipFilename(1, "爆点标题")).toBe("01-爆点标题.mp4"); expect(clipFilename(12, "t/i:t*le")).toBe("12-title.mp4"); }); }); describe("精华合集 (concat + chapters)", () => { it("buildConcatList:一行一个 file,单引号按 concat demuxer 语法转义", () => { expect(buildConcatList(["/a/01-x.mp4", "/a/02-y.mp4"])).toBe("file '/a/01-x.mp4'\nfile '/a/02-y.mp4'\n"); expect(buildConcatList(["/a/it's.mp4"])).toBe("file '/a/it'\\''s.mp4'\n"); }); it("buildConcatArgs:流复制 + faststart,不重编码", () => { const args = buildConcatArgs("/tmp/l.txt", "/out/合集.mp4"); expect(args.join(" ")).toContain("-f concat -safe 0 -i /tmp/l.txt"); expect(args.join(" ")).toContain("-c copy"); expect(args).toContain("+faststart"); expect(args.join(" ")).not.toContain("libx264"); }); it("buildChapters:0:00 起累计时刻,超一小时带小时位", () => { expect( buildChapters([ { title: "开场爆点", durationSec: 32.7 }, { title: "第二条", durationSec: 41.2 }, { title: "压轴", durationSec: 3600 }, ]) ).toBe("0:00 开场爆点\n0:32 第二条\n1:13 压轴\n"); expect(buildChapters([{ title: "a", durationSec: 10 }, { title: "b", durationSec: 5 }, { title: "c", durationSec: 1 }]).split("\n")[2]).toBe("0:15 c"); // 累计超 1 小时后第三条的时刻带小时位 expect( buildChapters([ { title: "x", durationSec: 3599 }, { title: "y", durationSec: 2 }, { title: "z", durationSec: 1 }, ]).split("\n")[2] ).toBe("1:00:01 z"); }); }); - src/core/__tests__/fillers.test.ts (3370 bytes)
import { describe, expect, it } from "vitest"; import { findFillerWords, dropFillerWords, fillerCutSpans } from "../fillers"; import { computeJumpCut, subtractSpans } from "../gaps"; import type { TranscriptWord } from "../../shared/api-types"; const w = (text: string, s: number, e: number): TranscriptWord => ({ text, startSec: s, endSec: e }); describe("findFillerWords", () => { it("flags hesitation tokens, tolerating attached punctuation", () => { const words = [w("嗯,", 0, 0.2), w("这个", 0.3, 0.6), w("产品", 0.6, 1.0), w("呃", 1.2, 1.4), w("很好", 1.5, 2.0)]; const hits = findFillerWords(words); expect(hits.map((h) => h.index)).toEqual([0, 3]); expect(hits[0].kind).toBe("filler"); }); it("keeps real words that merely resemble fillers in context", () => { const words = [w("然后", 0, 0.3), w("那个", 0.4, 0.7), w("啊", 0.8, 1.0)]; expect(findFillerWords(words)).toEqual([]); }); it("flags the first of an immediate stutter repeat, not slow deliberate repeats", () => { const stutter = [w("这个", 0, 0.3), w("这个", 0.35, 0.65), w("很好", 0.7, 1.0)]; const hits = findFillerWords(stutter); expect(hits).toHaveLength(1); expect(hits[0]).toMatchObject({ index: 0, kind: "stutter" }); // a 1s pause between repeats reads as emphasis — leave it alone const deliberate = [w("很好", 0, 0.3), w("很好", 1.5, 1.8)]; expect(findFillerWords(deliberate)).toEqual([]); }); }); describe("dropFillerWords / fillerCutSpans", () => { it("removes flagged words and merges adjacent spans", () => { const words = [w("嗯", 0, 0.2), w("呃", 0.25, 0.4), w("好", 0.6, 1.0)]; const hits = findFillerWords(words); expect(dropFillerWords(words, hits).map((x) => x.text)).toEqual(["好"]); const spans = fillerCutSpans(hits); expect(spans).toEqual([{ startSec: 0, endSec: 0.4 }]); }); }); describe("subtractSpans", () => { it("splits kept segments around forced cuts and drops slivers", () => { const out = subtractSpans( [{ startSec: 0, endSec: 10 }], [{ startSec: 2, endSec: 2.5 }, { startSec: 9.95, endSec: 10 }] ); expect(out).toEqual([ { startSec: 0, endSec: 2 }, { startSec: 2.5, endSec: 9.95 }, ]); }); }); describe("computeJumpCut with forced filler cuts", () => { it("cuts an audible filler that gap logic and the silence gate would keep", () => { // 嗯 at 1.0-1.3s sits between words with small gaps — no gap cut possible const words = [w("前面", 0.2, 0.9), w("后面", 1.5, 2.2)]; const plan = computeJumpCut(words, 0, 2.5, { forceCutSpans: [{ startSec: 1.0, endSec: 1.3 }], gapThresholdSec: Infinity, }); expect(plan.segments).toHaveLength(2); expect(plan.segments[0].endSec).toBeCloseTo(1.0, 5); expect(plan.segments[1].startSec).toBeCloseTo(1.3, 5); // splice point becomes a caption forced break expect(plan.breaks).toHaveLength(1); }); it("short-cut smoothing must not fill a forced cut back in", () => { const words = [w("前面", 0.2, 0.9), w("后面", 1.2, 2.0)]; const plan = computeJumpCut(words, 0, 2.5, { forceCutSpans: [{ startSec: 0.95, endSec: 1.1 }], // 0.15s < MIN_CUT_SEC gapThresholdSec: Infinity, }); expect(plan.segments).toHaveLength(2); expect(plan.removedSec).toBeGreaterThan(0.1); }); }); - src/core/__tests__/flash-payoff.test.ts (2528 bytes)
import { describe, it, expect } from "vitest"; import { planFlashForward, FLASH_LEAD_SEC, FLASH_TAIL_SEC, FLASH_MIN_SEC } from "../coldopen"; import { missingHookPayoffs } from "../qa"; describe("planFlashForward(爆点闪现)", () => { const kept = [ { startSec: 100, endSec: 130 }, { startSec: 140, endSec: 160 }, ]; it("在保留区间内围绕峰值取闪现窗(峰前铺垫+峰后余韵)", () => { const plan = planFlashForward([150], kept); expect(plan).not.toBeNull(); expect(plan!.startSec).toBeCloseTo(150 - FLASH_LEAD_SEC, 5); expect(plan!.endSec).toBeCloseTo(150 + FLASH_TAIL_SEC, 5); }); it("峰值贴着段边界时窗被夹取,夹到过短则跳过换下一个峰", () => { // 159.9 贴着段尾:窗被夹到 [159.7, 160] = 0.3s,恰好达标 const edge = planFlashForward([159.9], kept); expect(edge).not.toBeNull(); expect(edge!.endSec - edge!.startSec).toBeGreaterThanOrEqual(FLASH_MIN_SEC - 1e-6); // 100.02 贴着段头:窗 [100, 100.52];140.01 贴段头同理——都合法 // 峰值不在任何保留区间(135 落在空隙里)→ 跳过它选下一个 const skipGap = planFlashForward([135, 150], kept); expect(skipGap!.startSec).toBeCloseTo(150 - FLASH_LEAD_SEC, 5); }); it("无可用峰值返回 null(宁可不做不可做错)", () => { expect(planFlashForward([], kept)).toBeNull(); expect(planFlashForward([135], kept)).toBeNull(); // 全部落在空隙 }); }); describe("missingHookPayoffs(钩子兑付校验)", () => { it("钩子承诺的数字必须出现在转写里,缺了报出来", () => { const missing = missingHookPayoffs("只要99块,省下3000元", "今天这个只要九十九,能给你省下3000元"); expect(missing).toEqual(["99"]); // 3000 在片中;99 被转写成汉字→报缺 }); it("转写里的千分位/空格分隔不影响匹配", () => { expect(missingHookPayoffs("直降1999元", "直接给你降 1,999 元")).toEqual([]); }); it("单位数不核对(常被转写成汉字,必然误报)", () => { expect(missingHookPayoffs("3个方法", "三个方法教给你")).toEqual([]); }); it("百分比/折扣类承诺按数字核匹配", () => { expect(missingHookPayoffs("打4.9折,便宜30%", "四点九折,便宜30个点")).toEqual(["4.9折"]); }); it("无钩子/无转写时不评估", () => { expect(missingHookPayoffs(undefined, "随便说点什么")).toEqual([]); expect(missingHookPayoffs("只要99块", undefined)).toEqual([]); }); }); - src/core/__tests__/game-camera.integration.test.ts (4463 bytes)
import { execFileSync } from "node:child_process"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it } from "vitest"; import { cameraRect, normalizeCameraRegion } from "../../shared/game-camera"; import { resolveFfmpegPath } from "../binaries"; import { runFfmpeg } from "../cut"; import { exportClips } from "../export"; import { probeMedia } from "../probe"; const region = { x: 0.85, y: 0.18, width: 0.25 }; // A circular yellow face at the top right, two red edge markers in the game. // The circle's bounding box detects accidental stretching in either pane. function gameFrame(w: number, h: number, face: boolean): Buffer { const data = Buffer.alloc(w * h * 3); const rect = cameraRect(region, w, h); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const marker = (x < w * 0.1 || x > w * 0.9) && y > h * 0.46 && y < h * 0.54; const inCamera = x >= rect.x && y >= rect.y && x < rect.x + rect.width && y < rect.y + rect.height; const inFace = face && Math.hypot(x - w * region.x, y - h * region.y) <= rect.height * 0.25; const rgb = inFace ? [245, 220, 20] : inCamera ? [28, 58, 150] : marker ? [238, 12, 24] : [45, 96, 56]; data.set(rgb, (y * w + x) * 3); } } return Buffer.concat([Buffer.from(`P6\n${w} ${h}\n255\n`), data]); } it.each([ ["landscape", 640, 360, true], ["portrait", 360, 640, true], ["no-face", 640, 360, false], ] as const)("exports the camera and entire game for %s footage", async (name, w, h, face) => { const root = await mkdtemp(join(tmpdir(), "hotclip-camera-")); try { const still = join(root, "game.ppm"); const source = join(root, "game.mp4"); await writeFile(still, gameFrame(w, h, face)); await runFfmpeg(["-y", "-loop", "1", "-framerate", "10", "-i", still, "-f", "lavfi", "-i", "sine=frequency=440", "-t", "2", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-c:a", "aac", source]); const spec = { id: 1, title: "Game", startSec: 0, endSec: 2, words: [{ text: "Camera test", startSec: 0.1, endSec: 1.8 }] }; const out = join(root, "out"); const [result] = await exportClips(source, [{ ...spec, pieces: name === "landscape" ? [{ startSec: 0, endSec: 0.8 }, { startSec: 1.1, endSec: 2 }] : undefined, }], out, { vertical: true, gameCamera: region, trimUi: true, faceTrack: true, autoZoom: true, captionStyle: "keyword", qa: false }); expect(await probeMedia(result.path)).toMatchObject({ width: 1080, height: 1920, hasAudio: true }); const manifest = JSON.parse(await readFile(join(out, "clips.json"), "utf8")); expect(manifest.clips[0].render).toMatchObject({ reframe: "game-camera", captionsBurned: true }); const rgb = execFileSync(resolveFfmpegPath(), ["-v", "error", "-ss", "0.4", "-i", result.path, "-frames:v", "1", "-vf", "scale=270:480", "-pix_fmt", "rgb24", "-f", "rawvideo", "pipe:1"]); const upper: number[][] = []; const lower: number[][] = []; const redX: number[] = []; let captionPixels = 0; for (let y = 0; y < 480; y++) { for (let x = 0; x < 270; x++) { const i = (y * 270 + x) * 3; const r = rgb[i], g = rgb[i + 1], b = rgb[i + 2]; if (r > 165 && g > 140 && b < 72) (y < 192 ? upper : lower).push([x, y]); if (y > 192 && r > 155 && g < 85 && b < 85) redX.push(x); if (y > 220 && r > 190 && g > 190 && b > 190) captionPixels++; } } expect(captionPixels).toBeGreaterThan(10); const visibleWidth = Math.min(270, 288 * w / h); expect(Math.min(...redX)).toBeLessThan(135 - visibleWidth * 0.43); expect(Math.max(...redX)).toBeGreaterThan(135 + visibleWidth * 0.43); for (const points of [upper, lower]) { if (!face) { expect(points).toHaveLength(0); continue; } const xs = points.map(([x]) => x), ys = points.map(([, y]) => y); const width = Math.max(...xs) - Math.min(...xs), height = Math.max(...ys) - Math.min(...ys); expect(width / height).toBeGreaterThan(0.8); expect(width / height).toBeLessThan(1.2); } } finally { await rm(root, { recursive: true, force: true }); } }, 30_000); it("clamps manual regions and rejects invalid values", () => { expect(normalizeCameraRegion({ x: -1, y: 2, width: 5 })).toEqual({ x: 0, y: 1, width: 1 }); expect(normalizeCameraRegion({ x: NaN, y: 0, width: 0.2 })).toBeUndefined(); }); - src/core/__tests__/gaps.test.ts (6260 bytes)
import { describe, it, expect } from "vitest"; import { computeJumpCut, mergeShortCuts } from "../gaps"; import { buildJumpCutArgs } from "../cut"; import type { PeakTrack } from "../audio-peaks"; import type { TranscriptWord } from "../../shared/api-types"; function w(text: string, startSec: number, endSec: number): TranscriptWord { return { text, startSec, endSec }; } describe("computeJumpCut", () => { // speech 10-12s, 2s silence, speech 14-16s const words = [w("前", 10, 11), w("半", 11, 12), w("后", 14, 15), w("半", 15, 16)]; it("cuts the silence into two kept segments with padding", () => { const plan = computeJumpCut(words, 10, 16.5); expect(plan.segments).toHaveLength(2); const [a, b] = plan.segments; expect(a.startSec).toBeCloseTo(10, 2); // clamped by clip start (10 - 0.15 lead-in) expect(a.endSec).toBeCloseTo(12.18, 2); // + pad after expect(b.startSec).toBeCloseTo(13.88, 2); // - pad before expect(b.endSec).toBeCloseTo(16.3, 2); // + tail expect(plan.removedSec).toBeCloseTo(6.5 - plan.durationSec, 5); expect(plan.removedSec).toBeGreaterThan(1.4); }); it("remaps words onto the compressed output timeline", () => { const plan = computeJumpCut(words.map((word) => ({ ...word, timingSource: "aligned" as const })), 10, 16.5); const [, , w3] = plan.words; // third word starts at seg2 start → output time = seg1 duration + pad-before offset const seg1Dur = plan.segments[0].endSec - plan.segments[0].startSec; expect(w3.startSec).toBeCloseTo(seg1Dur + (14 - plan.segments[1].startSec), 3); // monotonic and starting near zero expect(plan.words[0].startSec).toBeCloseTo(0, 2); for (let i = 1; i < plan.words.length; i++) { expect(plan.words[i].startSec).toBeGreaterThanOrEqual(plan.words[i - 1].startSec); } expect(plan.words.every((word) => word.timingSource === "aligned")).toBe(true); }); it("no gaps → single segment trimmed to speech bounds", () => { const tight = [w("a", 10.5, 11), w("b", 11.1, 12)]; const plan = computeJumpCut(tight, 10, 20); expect(plan.segments).toHaveLength(1); expect(plan.segments[0].startSec).toBeCloseTo(10.35, 2); // lead-in pad expect(plan.segments[0].endSec).toBeCloseTo(12.3, 2); // tail pad }); it("no words → keeps the full clip", () => { const plan = computeJumpCut([], 5, 10); expect(plan.segments).toEqual([{ startSec: 5, endSec: 10 }]); expect(plan.durationSec).toBe(5); }); // peaks: 30 blocks/s track covering 10-16.5s, quiet everywhere by default function trackWithBurst(burstFrom?: number, burstTo?: number): PeakTrack { const hopSec = 1 / 30; const values = new Float32Array(Math.ceil(6.5 / hopSec)).fill(0.01); if (burstFrom !== undefined && burstTo !== undefined) { for (let i = Math.floor((burstFrom - 10) / hopSec); i * hopSec + 10 < burstTo; i++) { values[i] = 0.4; } } return { values, startSec: 10, hopSec }; } it("AND gate: quiet gap still gets cut when peaks are provided", () => { const plan = computeJumpCut(words, 10, 16.5, { peaks: trackWithBurst() }); expect(plan.segments).toHaveLength(2); }); it("AND gate: loud wordless gap (laughter/applause) survives the cut", () => { const plan = computeJumpCut(words, 10, 16.5, { peaks: trackWithBurst(12.5, 13.5) }); expect(plan.segments).toHaveLength(1); expect(plan.removedSec).toBeLessThan(0.5); }); it("VAD gate protects speech that ASR missed inside a quiet word gap", () => { const plan = computeJumpCut(words, 10, 16.5, { peaks: trackWithBurst(), speechSpans: [ { startSec: 9.9, endSec: 12.1 }, { startSec: 12.7, endSec: 13.3 }, { startSec: 13.9, endSec: 16.1 }, ], }); expect(plan.segments).toHaveLength(1); expect(plan.speechProtectedGaps).toBe(1); }); it("omitting VAD preserves the legacy result shape and cut", () => { const legacy = computeJumpCut(words, 10, 16.5, { peaks: trackWithBurst() }); expect(legacy.segments).toHaveLength(2); expect(legacy).not.toHaveProperty("speechProtectedGaps"); }); it("keeps VAD-confirmed phoneme tails outside coarse ASR word edges", () => { const edgeWords = [w("开", 10.2, 10.6), w("尾", 11.5, 11.8)]; const plan = computeJumpCut(edgeWords, 9.5, 12.5, { speechSpans: [{ startSec: 9.9, endSec: 10.65 }, { startSec: 11.45, endSec: 12.2 }], }); expect(plan.segments[0].startSec).toBeCloseTo(9.82, 5); expect(plan.segments.at(-1)?.endSec).toBeCloseTo(12.32, 5); }); }); describe("mergeShortCuts", () => { it("fills back splices shorter than the minimum cut", () => { const merged = mergeShortCuts( [ { startSec: 0, endSec: 2 }, { startSec: 2.1, endSec: 4 }, // 0.1s cut — churn { startSec: 5, endSec: 6 }, // 1s cut — real ], 0.2 ); expect(merged).toEqual([ { startSec: 0, endSec: 4 }, { startSec: 5, endSec: 6 }, ]); }); it("passes through when nothing is short", () => { const segs = [ { startSec: 0, endSec: 2 }, { startSec: 3, endSec: 4 }, ]; expect(mergeShortCuts(segs, 0.2)).toEqual(segs); }); }); describe("buildJumpCutArgs", () => { const segments = [ { startSec: 10, endSec: 12.2 }, { startSec: 13.9, endSec: 16.3 }, ]; it("seeks to clip start and trims with seek-relative times", () => { const args = buildJumpCutArgs("/v/in.mp4", "/v/out.mp4", 10, segments); expect(args[args.indexOf("-ss") + 1]).toBe("00:00:10.000"); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toContain("[0:v:0]trim=start=0.000:end=2.200"); expect(fc).toContain("[0:a:0]atrim=start=3.900:end=6.300"); expect(fc).toContain("concat=n=2:v=1:a=1"); expect(args).toContain("[vout]"); expect(args).toContain("[aout]"); }); it("chains reframe + subtitles after the concat", () => { const args = buildJumpCutArgs("/v/in.mp4", "/v/out.mp4", 10, segments, { vertical: true, subtitlePath: "/tmp/a.ass", }); const fc = args[args.indexOf("-filter_complex") + 1]; expect(fc).toMatch(/\[vc\]crop=.*scale=1080:1920.*subtitles=filename='\/tmp\/a\.ass'\[vout\]/); }); }); - src/core/__tests__/gate.test.ts (4888 bytes)
/** * 质量门规则层(v0.13):确定性硬伤检查——开头悬空/结尾没收住/多人抢话。 * 铁律:只降档到 review、绝不 drop、绝不升档(fail-open,规则误判代价必须小)。 */ import { describe, it, expect } from "vitest"; import { openingDangles, endingUnfinished, speakerTangled, ruleGateIssues, applyRuleGate } from "../highlight/gate"; import type { Transcript } from "../transcribe/types"; import type { HighlightCandidate } from "../../shared/api-types"; function cand(over: Partial<HighlightCandidate>): HighlightCandidate { return { id: 1, startSec: 0, endSec: 20, text: "完整的一句话。", title: "t", hook: "h", score: 90, reason: "", boundary: "exact", keywords: [], recommended: true, reviewNote: "", ...over, }; } const EMPTY_TX: Transcript = { language: "zh", engine: "test", durationSec: 60, segments: [] }; describe("openingDangles(开头悬空)", () => { it("悬空接续词开头 = 半截话", () => { for (const s of ["所以我们就退款了。", "但是他不同意。", "然后呢就翻车了。", "而且价格还更贵。", "So we refunded it."]) { expect(openingDangles(s), s).toBe(true); } }); it("正常开场不误伤(其实/说白了是常见金句开头)", () => { for (const s of ["其实这个东西很简单。", "你以为贵的就好吗?", "说白了就是信息差。", "90%的人都不知道这件事。", "Sochi is beautiful."]) { expect(openingDangles(s), s).toBe(false); } }); it("跳过开头引号再判", () => { expect(openingDangles("「但是他说过不降价」")).toBe(true); }); }); describe("endingUnfinished(结尾没收住)", () => { it("逗号/顿号/冒号收尾 = 话没说完", () => { for (const s of ["我们先看第一个,", "包括材质、", "原因有三:", "so we tried,"]) { expect(endingUnfinished(s), s).toBe(true); } }); it("正常收尾与无标点收尾都不算(ASR 丢句尾标点太常见)", () => { for (const s of ["这就是全部真相。", "你敢信?", "太值了!", "就这样定了"]) { expect(endingUnfinished(s), s).toBe(false); } }); }); describe("speakerTangled(多人抢话)", () => { const seg = (id: number, startSec: number, speaker: number): Transcript["segments"][number] => ({ id, startSec, endSec: startSec + 2, text: "话", words: [], speaker, }); it("切换密度超阈值才算(20 秒内 8 次切换 = 24 次/分钟)", () => { const tangled: Transcript = { ...EMPTY_TX, segments: Array.from({ length: 9 }, (_, i) => seg(i + 1, i * 2.2, i % 2)), }; expect(speakerTangled(tangled, { startSec: 0, endSec: 20 })).toBe(true); }); it("单说话人/未分离一律 false", () => { const single: Transcript = { ...EMPTY_TX, segments: Array.from({ length: 9 }, (_, i) => seg(i + 1, i * 2.2, 0)) }; const unlabeled: Transcript = { ...EMPTY_TX, segments: Array.from({ length: 9 }, (_, i) => ({ ...seg(i + 1, i * 2.2, 0), speaker: undefined })), }; expect(speakerTangled(single, { startSec: 0, endSec: 20 })).toBe(false); expect(speakerTangled(unlabeled, { startSec: 0, endSec: 20 })).toBe(false); }); it("正常一问一答不误伤(60 秒 4 次切换)", () => { const qa: Transcript = { ...EMPTY_TX, segments: Array.from({ length: 5 }, (_, i) => seg(i + 1, i * 12, i % 2)), }; expect(speakerTangled(qa, { startSec: 0, endSec: 60 })).toBe(false); }); }); describe("ruleGateIssues / applyRuleGate", () => { it("信号候选跳过文本规则(它不是按原话切的)", () => { expect(ruleGateIssues(EMPTY_TX, cand({ boundary: "signal", text: "但是," }), true)).toEqual([]); }); it("publish/缺省降为 review 并写明原因;review 原因追加;drop 不动档", () => { const bad = "但是这个东西呢,"; const out = applyRuleGate(EMPTY_TX, [ cand({ id: 1, text: bad, gate: "publish" }), cand({ id: 2, text: bad }), // 复评没跑,gate 缺省 cand({ id: 3, text: bad, gate: "drop", gateNotes: ["凑数"] }), cand({ id: 4, text: "完整的一句话。", gate: "publish" }), ], true); expect(out[0].gate).toBe("review"); expect(out[0].gateNotes).toEqual(["开头像半截话(悬空接续词)", "结尾没收住(截在逗号上)"]); expect(out[1].gate).toBe("review"); expect(out[2].gate).toBe("drop"); // 绝不升档 expect(out[2].gateNotes).toEqual(["凑数", "开头像半截话(悬空接续词)", "结尾没收住(截在逗号上)"]); expect(out[3].gate).toBe("publish"); expect(out[3].gateNotes).toBeUndefined(); }); it("规则降档不改变 recommended(选中状态只跟 LLM 复评走)", () => { const out = applyRuleGate(EMPTY_TX, [cand({ text: "但是这样,", gate: "publish", recommended: true })], true); expect(out[0].recommended).toBe(true); }); }); - src/core/__tests__/genre.test.ts (4957 bytes)
import { describe, it, expect } from "vitest"; import { GENRE_PRESETS, GENRE_CUSTOM_MAX_CHARS, genrePreset, genreSection, normalizeGenreId, } from "../genre"; describe("genrePreset", () => { it("按 id 取到对应预设", () => { expect(genrePreset("game").id).toBe("game"); expect(genrePreset("show").labelZh).toContain("舞见"); }); it("未知/空 id 回落通用档,不抛异常", () => { expect(genrePreset("不存在的品类").id).toBe("auto"); expect(genrePreset(undefined).id).toBe("auto"); }); it("每个内置预设中英文都有标签,且除 auto/custom 外都有判据", () => { for (const g of GENRE_PRESETS) { expect(g.labelZh).not.toBe(""); expect(g.labelEn).not.toBe(""); if (g.id !== "auto" && g.id !== "custom") { expect(g.criteriaZh.length).toBeGreaterThan(50); expect(g.criteriaEn.length).toBeGreaterThan(50); } } }); it("id 不重复(下拉里不许出现两个同名项)", () => { const ids = GENRE_PRESETS.map((g) => g.id); expect(new Set(ids).size).toBe(ids.length); }); }); describe("分区口径对齐平台真实分类", () => { // 分区表按 B站直播 Area/getList(11 个一级分区)+ 抖音/斗鱼公开分区归并而来。 // 这些是我自己拍脑袋列时漏掉、看了真实分类才补上的品类——回归时要守住。 it("覆盖 B站一级分区里容易被漏掉的几类", () => { const ids = GENRE_PRESETS.map((g) => g.id); for (const must of ["vtuber", "radio", "pet", "food", "esports", "craft", "cowatch", "looks", "sports"]) { expect(ids).toContain(must); } }); it("每个预设都标了证据类别,且三类都有代表", () => { const classes = new Set(GENRE_PRESETS.map((g) => g.evidence)); expect(classes).toEqual(new Set(["words", "reaction", "visual"])); // 文字稿最没用的那几类必须不是 words,否则信号通道根本不会跑 for (const id of ["show", "pet", "food", "craft"]) { expect(genrePreset(id).evidence).toBe("visual"); } for (const id of ["game", "outdoor", "vtuber", "radio", "esports"]) { expect(genrePreset(id).evidence).toBe("reaction"); } }); it("旧偏好里的 id 仍然认(分区重排不能让本机设置失效)", () => { expect(normalizeGenreId("live-sell")).toBe("shopping"); expect(normalizeGenreId("gaming")).toBe("game"); expect(normalizeGenreId("lecture")).toBe("knowledge"); expect(normalizeGenreId("show")).toBe("show"); expect(normalizeGenreId(undefined)).toBeUndefined(); // 走到 genreSection 也要真的拿到新判据 expect(genreSection("live-sell", true)).toContain("带货直播"); }); }); describe("genreSection", () => { it("通用档不注入任何内容(保持原有通用判据)", () => { expect(genreSection("auto", true)).toBe(""); expect(genreSection(undefined, false)).toBe(""); }); it("按语言给对应判据", () => { expect(genreSection("shopping", true)).toContain("带货直播"); expect(genreSection("shopping", false)).toContain("Live-selling"); }); it("自定义判据盖过内置预设(选个最接近的再改两句)", () => { const out = genreSection("game", true, "只要老板骂人的片段"); expect(out).toContain("只要老板骂人的片段"); expect(out).not.toContain("极限操作"); }); it("空白自定义不算数,仍用预设", () => { expect(genreSection("game", true, " ")).toContain("极限操作"); expect(genreSection("game", true, "")).toContain("极限操作"); }); it("自定义判据超长会被截断(不许塞爆提示词)", () => { const huge = "很长的判据".repeat(1000); const out = genreSection("custom", true, huge); expect(out.length).toBeLessThan(GENRE_CUSTOM_MAX_CHARS + 100); }); it("custom 预设自身没有内置判据,没填自定义就不注入", () => { expect(genreSection("custom", true)).toBe(""); }); it("文字稿不可信的品类都明说了别信文字稿", () => { expect(genreSection("game", true)).toContain("不要只看文字稿"); expect(genreSection("show", true)).toContain("文字稿基本没有信息量"); expect(genreSection("pet", true)).toContain("文字稿完全不能作为判据"); expect(genreSection("outdoor", true)).toContain("户外收音差"); }); it("虚拟主播档点明表情信号无效(脸是模型不是人)", () => { expect(genreSection("vtuber", true)).toContain("人脸表情信号基本无效"); }); it("电台档点明没有画面(纯音频成片走波形图)", () => { expect(genreSection("radio", true)).toContain("没有画面可用"); }); it("一起看档把版权风险写在最前面——这条比选得好不好重要", () => { const out = genreSection("cowatch", true); expect(out).toContain("版权"); expect(out).toContain("不要把影视画面本身切成片"); }); }); - src/core/__tests__/glossary.test.ts (7243 bytes)
import { describe, it, expect } from "vitest"; import { mkdtemp, writeFile, readFile } from "fs/promises"; import { join } from "path"; import { tmpdir } from "os"; import { sanitizeGlossary, applyGlossaryToText, applyGlossaryToTranscript, countGlossaryHits, diffReplacement, upsertGlossaryEntry, } from "../../shared/glossary"; import { loadGlossary, saveGlossary, glossaryPath } from "../glossary-store"; import type { Transcript } from "../../shared/api-types"; function makeTranscript(texts: string[]): Transcript { let t = 0; return { language: "zh", engine: "test", durationSec: texts.length * 3, segments: texts.map((text, i) => { const seg = { id: i + 1, startSec: t, endSec: t + 3, text, words: Array.from(text).map((ch, j) => ({ text: ch, startSec: t + (3 * j) / text.length, endSec: t + (3 * (j + 1)) / text.length, })), }; t += 3; return seg; }), }; } describe("sanitizeGlossary", () => { it("丢弃空词/自指词/非法项,同错词去重留首条", () => { expect( sanitizeGlossary([ { wrong: " 川普 ", right: "特朗普" }, { wrong: "川普", right: "别的" }, // 重复错词,丢弃 { wrong: "同词", right: "同词" }, // 自指,丢弃 { wrong: "", right: "x" }, { wrong: "x", right: "" }, null, 42, ]) ).toEqual([{ wrong: "川普", right: "特朗普" }]); }); it("非数组输入返回空表", () => { expect(sanitizeGlossary("broken")).toEqual([]); expect(sanitizeGlossary(undefined)).toEqual([]); }); }); describe("applyGlossaryToText", () => { it("中文子串替换", () => { expect(applyGlossaryToText("今天川普又发言了,川普说", [{ wrong: "川普", right: "特朗普" }])).toBe( "今天特朗普又发言了,特朗普说" ); }); it("拉丁词整词匹配:AI 不动 MAIN,且忽略大小写", () => { const entries = [{ wrong: "chatgpt", right: "ChatGPT" }]; expect(applyGlossaryToText("我用 Chatgpt 和 CHATGPT 写代码", entries)).toBe("我用 ChatGPT 和 ChatGPT 写代码"); expect(applyGlossaryToText("the MAIN point", [{ wrong: "AI", right: "人工智能" }])).toBe("the MAIN point"); }); it("多条同时命中取最长错词优先", () => { const entries = [ { wrong: "open ai", right: "OpenAI" }, { wrong: "ai", right: "AI" }, ]; expect(applyGlossaryToText("open ai 和 ai 都出现", entries)).toBe("OpenAI 和 AI 都出现"); }); it("单趟替换:替换结果不被其他词条二次改写", () => { const entries = [ { wrong: "甲", right: "乙" }, { wrong: "乙", right: "丙" }, ]; expect(applyGlossaryToText("甲乙", entries)).toBe("乙丙"); }); it("错词含正则特殊字符不炸", () => { expect(applyGlossaryToText("学 c++ 的人", [{ wrong: "c++", right: "C++" }])).toBe("学 C++ 的人"); }); it("多个错误变体映射同一对词", () => { const entries = [ { wrong: "赛博朋克贰零柒柒", right: "赛博朋克2077" }, { wrong: "赛博胖客2077", right: "赛博朋克2077" }, ]; expect(applyGlossaryToText("玩赛博胖客2077和赛博朋克贰零柒柒", entries)).toBe("玩赛博朋克2077和赛博朋克2077"); }); }); describe("applyGlossaryToTranscript", () => { const entries = [{ wrong: "川普", right: "特朗普" }]; it("只重建被改句,未改句保持原引用;整体没改返回原引用", () => { const t = makeTranscript(["川普发言了", "今天天气不错"]); const { transcript: out, replaced } = applyGlossaryToTranscript(t, entries); expect(replaced).toBe(1); expect(out.segments[0].text).toBe("特朗普发言了"); expect(out.segments[0].glossaryApplied).toBe(true); expect(out.segments[1]).toBe(t.segments[1]); // 原引用 const untouched = applyGlossaryToTranscript(makeTranscript(["今天天气不错"]), entries); expect(untouched.replaced).toBe(0); }); it("被改句词级时间轴单调、首尾对齐原句区间", () => { const t = makeTranscript(["川普发言了"]); const { transcript: out } = applyGlossaryToTranscript(t, entries); const words = out.segments[0].words; expect(words[0].startSec).toBeCloseTo(t.segments[0].startSec, 6); expect(words[words.length - 1].endSec).toBeCloseTo(t.segments[0].endSec, 6); for (let i = 1; i < words.length; i++) { expect(words[i].startSec).toBeGreaterThanOrEqual(words[i - 1].startSec); expect(words[i].startSec).toBeCloseTo(words[i - 1].endSec, 6); } }); it("保留说话人标注:句级 speaker 回填到重建的词", () => { const t = makeTranscript(["川普发言了"]); t.segments[0].speaker = 1; const { transcript: out } = applyGlossaryToTranscript(t, entries); expect(out.segments[0].words.every((w) => w.speaker === 1)).toBe(true); }); it("countGlossaryHits 统计命中句数", () => { const t = makeTranscript(["川普一", "无关", "川普二"]); expect(countGlossaryHits(t, entries)).toBe(2); expect(countGlossaryHits(t, [])).toBe(0); }); }); describe("diffReplacement", () => { it("提取中文单点修改", () => { expect(diffReplacement("今天川普又发言了", "今天特朗普又发言了")).toEqual({ wrong: "川普", right: "特朗普" }); }); it("拉丁词边界外扩到整词", () => { expect(diffReplacement("we use chatgpt daily", "we use ChatGPT daily")).toEqual({ wrong: "chatgpt", right: "ChatGPT", }); }); it("无修改/纯插入/纯删除/整句重写返回 null", () => { expect(diffReplacement("一样的", "一样的")).toBeNull(); expect(diffReplacement("前后", "前中后")).toBeNull(); // 纯插入 expect(diffReplacement("前中后", "前后")).toBeNull(); // 纯删除 expect( diffReplacement("这句话完全不一样了吧这句话完全不一样了吧", "换成了一整句别的话内容和原来毫无重合之处") ).toBeNull(); // 两侧都超 16 字:整句重写而非术语纠错 }); }); describe("upsertGlossaryEntry", () => { it("同错词覆盖旧对词,新词追加", () => { const base = [{ wrong: "a词", right: "旧" }]; expect(upsertGlossaryEntry(base, { wrong: "a词", right: "新" })).toEqual([{ wrong: "a词", right: "新" }]); expect(upsertGlossaryEntry(base, { wrong: "b词", right: "乙" })).toEqual([ { wrong: "a词", right: "旧" }, { wrong: "b词", right: "乙" }, ]); }); }); describe("glossary-store", () => { it("读写往返;缺文件/坏文件返回空表", async () => { const dir = await mkdtemp(join(tmpdir(), "hotclip-glossary-")); expect(await loadGlossary(dir)).toEqual([]); await saveGlossary(dir, [{ wrong: "川普", right: "特朗普" }, { wrong: "x", right: "x" }]); expect(await loadGlossary(dir)).toEqual([{ wrong: "川普", right: "特朗普" }]); // 自指项被清洗 expect(JSON.parse(await readFile(glossaryPath(dir), "utf8"))).toHaveLength(1); await writeFile(glossaryPath(dir), "{broken json", "utf8"); expect(await loadGlossary(dir)).toEqual([]); // fail-open }); }); - src/core/__tests__/highlight-stitch.test.ts (10055 bytes)
/** * 多片段拼接的检测侧:parts 解析 → 逐段反查 → 规整 → 时长/重叠口径。 * 「前后打脸」这类爆点必须引用相隔很远的两处内容,靠的就是这条路径。 */ import { describe, it, expect } from "vitest"; import { resolveSelection, type RawSelection } from "../highlight/match"; import { parseParts, parseSelections, parseMomentPicks, dropOverlaps } from "../highlight/detect"; import { highlightSystemPrompt, buildHighlightPrompt, buildReviewPrompt } from "../highlight/prompt"; import { PIECE_JOINER } from "../../shared/pieces"; import type { Transcript, TranscriptWord } from "../transcribe/types"; import type { HighlightCandidate } from "../../shared/api-types"; /** 逐字 0.2s 的中文转写;句间留 gapSec 好把两段拉开距离。 */ function makeTranscript(sentences: string[], gapSec = 0.5): Transcript { let t = 0; let id = 0; const segments = sentences.map((text) => { id++; const words: TranscriptWord[] = Array.from(text).map((ch, i) => ({ text: ch, startSec: Number((t + i * 0.2).toFixed(3)), endSec: Number((t + (i + 1) * 0.2).toFixed(3)), })); const seg = { id, startSec: words[0].startSec, endSec: words[words.length - 1].endSec, text, words }; t = seg.endSec + gapSec; return seg; }); return { language: "zh", segments, engine: "test", durationSec: t }; } /** 前后打脸的典型素材:先立誓,中间一大段无关内容,后面自己打脸。 */ const FLIP = makeTranscript( [ "我今天把话放在这儿,这个价格绝对绝对不会降。", "接下来我们聊点别的东西吧朋友们。", "这段中间的内容跟前后都没有关系。", "行吧那我今天就给大家降到七十九块钱。", ], 30 // 句间隔 30 秒,足够拉开成两段 ); const base = { title: "t", hook: "h", score: 90, reason: "r", keywords: [] }; describe("parseParts", () => { it("少于两段视为没写(退回单段定位)", () => { expect(parseParts([{ quoteStart: "只有一段" }])).toBeUndefined(); expect(parseParts(undefined)).toBeUndefined(); expect(parseParts("不是数组")).toBeUndefined(); }); it("解析两段,句 id 缺失时填 -1(下游退回引文反查)", () => { const out = parseParts([ { startSegmentId: 1, endSegmentId: 1, quoteStart: "我今天", quoteEnd: "不会降。" }, { quoteStart: "行吧那我", quoteEnd: "七十九块钱。" }, ]); expect(out).toHaveLength(2); expect(out![0].startSegmentId).toBe(1); expect(out![1].startSegmentId).toBe(-1); }); it("剔掉既没引文也没句 id 的空段;剩不足两段就整个不算", () => { expect(parseParts([{ quoteStart: "有引文" }, { title: "空的" }])).toBeUndefined(); }); it("parseSelections 把 parts 带进 RawSelection", () => { const out = parseSelections( JSON.stringify({ clips: [ { title: "打脸", score: 90, startSegmentId: 1, endSegmentId: 4, quoteStart: "我今天", quoteEnd: "七十九块钱。", parts: [ { startSegmentId: 1, endSegmentId: 1, quoteStart: "我今天", quoteEnd: "不会降。" }, { startSegmentId: 4, endSegmentId: 4, quoteStart: "行吧那我", quoteEnd: "七十九块钱。" }, ], }, ], }) ); expect(out[0].parts).toHaveLength(2); }); }); describe("resolveSelection with parts", () => { const sel: RawSelection = { ...base, startSegmentId: 1, endSegmentId: 4, quoteStart: "我今天把话放在这儿", quoteEnd: "七十九块钱。", parts: [ { startSegmentId: 1, endSegmentId: 1, quoteStart: "我今天把话放在这儿", quoteEnd: "绝对不会降。" }, { startSegmentId: 4, endSegmentId: 4, quoteStart: "行吧那我今天", quoteEnd: "七十九块钱。" }, ], }; it("两段各自反查,跨度取首尾,段清单按时间序", () => { const r = resolveSelection(FLIP, sel); expect(r).not.toBeNull(); expect(r!.pieces).toHaveLength(2); expect(r!.startSec).toBeCloseTo(FLIP.segments[0].startSec, 2); expect(r!.endSec).toBeCloseTo(FLIP.segments[3].endSec, 2); expect(r!.pieces![0].endSec).toBeLessThan(r!.pieces![1].startSec); }); it("展示文本用省略标记连接——评审和用户必须看得出中间跳了", () => { const r = resolveSelection(FLIP, sel); expect(r!.text).toContain(PIECE_JOINER.trim()); expect(r!.text).toContain("不会降"); expect(r!.text).toContain("七十九"); // 中间那两句无关内容不能混进来 expect(r!.text).not.toContain("聊点别的"); }); it("乱序给的 parts 会被排回时间序(成片不能倒放剧情)", () => { const r = resolveSelection(FLIP, { ...sel, parts: [sel.parts![1], sel.parts![0]] }); expect(r!.pieces![0].startSec).toBeLessThan(r!.pieces![1].startSec); expect(r!.text.indexOf("不会降")).toBeLessThan(r!.text.indexOf("七十九")); }); it("有一段反查不到就退回单段(顶层引文仍然管用),不是整条丢掉", () => { const r = resolveSelection(FLIP, { ...sel, parts: [sel.parts![0], { startSegmentId: 9, endSegmentId: 9, quoteStart: "这句根本不存在", quoteEnd: "也不存在" }], }); expect(r).not.toBeNull(); expect(r!.pieces).toBeUndefined(); expect(r!.startSec).toBeCloseTo(FLIP.segments[0].startSec, 2); }); it("两段挨得太近会被合并 → 不足两段 → 退回单段", () => { const near = makeTranscript(["前面这句话说得很满。", "后面这句立刻就打脸了。"], 0.4); const r = resolveSelection(near, { ...base, startSegmentId: 1, endSegmentId: 2, quoteStart: "前面这句", quoteEnd: "打脸了。", parts: [ { startSegmentId: 1, endSegmentId: 1, quoteStart: "前面这句话", quoteEnd: "很满。" }, { startSegmentId: 2, endSegmentId: 2, quoteStart: "后面这句", quoteEnd: "打脸了。" }, ], }); expect(r!.pieces).toBeUndefined(); }); it("没有 parts 时行为与历史完全一致", () => { const r = resolveSelection(FLIP, { ...base, startSegmentId: 1, endSegmentId: 1, quoteStart: "我今天把话", quoteEnd: "不会降。" }); expect(r!.pieces).toBeUndefined(); expect(r!.boundary).toBe("anchored"); }); }); describe("dropOverlaps 的拼接口径", () => { const c = (id: number, s: number, e: number, score: number, pieces?: Array<{ startSec: number; endSec: number }>): HighlightCandidate => ({ id, startSec: s, endSec: e, pieces, text: "", title: "", hook: "", score, reason: "", boundary: "exact", keywords: [], recommended: true, reviewNote: "", }); it("拼接片按段比重叠,不会把跨度中间的候选全吃掉", () => { const stitch = c(1, 0, 600, 80, [{ startSec: 0, endSec: 15 }, { startSec: 580, endSec: 600 }]); const middle = c(2, 200, 220, 70); // 落在跨度里,但不碰任何一段 const kept = dropOverlaps([stitch, middle]); expect(kept).toHaveLength(2); }); it("真压在某一段上的候选照样被去重", () => { const stitch = c(1, 0, 600, 80, [{ startSec: 0, endSec: 15 }, { startSec: 580, endSec: 600 }]); const clash = c(2, 10, 30, 70); // 与第一段重叠 expect(dropOverlaps([stitch, clash])).toHaveLength(1); }); }); describe("prompt 里的拼接约定", () => { const tx = makeTranscript(["第一句。", "第二句。"]); it("系统提示词讲清「只在必须对照时才拼」并保留时长锚点", () => { const p = highlightSystemPrompt(tx); expect(p).toContain("parts"); expect(p).toContain("时长 8~40 秒"); // 时长档 replace 的锚点,不能被拼接段落挤掉 expect(p).toContain("不能制造原话里没有的意思"); }); it("输出格式说明里给了 parts 的形状,但主示例保持单段", () => { const p = buildHighlightPrompt(tx); expect(p).toContain('"parts"'); // OUTPUT_SHAPE 主示例(clips → keywords)里不出现 parts——否则模型会以为每条都该拼 expect(p.slice(p.indexOf('"clips"'), p.indexOf('"keywords"'))).not.toContain("parts"); }); it("复评提示词报的是成片时长并标出拼接段数", () => { const prompt = buildReviewPrompt(tx, [ { id: 1, title: "打脸", startSec: 0, endSec: 600, text: `前${PIECE_JOINER}后`, pieces: [{ startSec: 0, endSec: 10 }, { startSec: 585, endSec: 600 }] }, ]); expect(prompt).toContain("时长25秒"); // 10+15,不是跨度 600 expect(prompt).toContain("2 段拼接"); }); }); describe("chatCompleteJson 的一次重试", () => { /** 造一个「第一次吐杂质、第二次干净」的端点(真实观测到的故障形态)。 */ function flakyOnce(): { calls: number; complete: (c: string) => string } { let calls = 0; return { get calls() { return calls; }, complete: () => { calls++; // 实测出现过:`"score":数和 90`、`"momentId": vii`、`"score": —` return calls === 1 ? '{"clips":[{"momentId": vii,"title":"x","score": —}]}' : '{"clips":[{"momentId":2,"title":"干净的","score":88}]}'; }, }; } it("第一次解析失败会重发一次,第二次干净就照常返回", async () => { const f = flakyOnce(); let n = 0; const parsed = await (async (): Promise<ReturnType<typeof parseMomentPicks>> => { let lastErr: unknown; for (let i = 0; i < 2; i++) { const content = f.complete(""); n++; try { return parseMomentPicks(content); } catch (e) { lastErr = e; } } throw lastErr; })(); expect(n).toBe(2); expect(parsed[0].title).toBe("干净的"); }); it("杂质 token 确实会让整份响应解析失败(所以才需要重试)", () => { expect(() => parseMomentPicks('{"clips":[{"momentId": vii,"score": —}]}')).toThrow(); expect(() => parseSelections('{"clips":[{"endSegmentId": to 3}]}')).toThrow(); }); }); - src/core/__tests__/highlight.test.ts (18379 bytes)
import { describe, it, expect } from "vitest"; import { normalizeText, buildTokenIndex, matchQuote, resolveSelection } from "../highlight/match"; import { parseSelections, dropOverlaps, parseReviews, applyReviews, normalizeScores, clipLengthBounds, mergeProductKeywords } from "../highlight/detect"; import { buildHighlightPrompt, renderTranscriptLines, extractJson, isChineseTranscript, isMultiSpeaker, highlightSystemPrompt, renderSignals, } from "../highlight/prompt"; import type { Transcript, TranscriptWord } from "../transcribe/types"; import type { HighlightCandidate } from "../../shared/api-types"; /** Build a transcript from sentences: each char = one 0.2s token (zh-style). */ function makeTranscript(sentences: string[]): Transcript { let t = 0; let id = 0; const segments = sentences.map((text) => { id++; const words: TranscriptWord[] = Array.from(text).map((ch, i) => ({ text: ch, startSec: t + i * 0.2, endSec: t + (i + 1) * 0.2, })); const seg = { id, startSec: words[0].startSec, endSec: words[words.length - 1].endSec, text, words }; t = seg.endSec + 0.5; return seg; }); return { language: "zh", segments, engine: "test", durationSec: t }; } describe("normalizeText", () => { it("drops punctuation/whitespace, lowercases latin, keeps CJK", () => { expect(normalizeText("你好,世界! Hello, World!")).toBe("你好世界helloworld"); }); }); describe("matchQuote", () => { const tx = makeTranscript(["今天天气真好。", "我们来聊聊赚钱这件事。", "记住这三个字:别上头。"]); const index = buildTokenIndex(tx.segments.flatMap((s) => s.words)); it("exact contiguous match returns precise token times", () => { const m = matchQuote(index, "我们来聊聊", "赚钱这件事。"); expect(m).not.toBeNull(); expect(m!.boundary).toBe("exact"); expect(m!.startSec).toBeCloseTo(tx.segments[1].startSec, 3); expect(m!.endSec).toBeCloseTo(tx.segments[1].endSec, 3); }); it("anchored match spans head→tail across sentences", () => { const m = matchQuote(index, "我们来聊聊赚钱", "别上头。"); expect(m).not.toBeNull(); expect(m!.boundary).toBe("anchored"); expect(m!.startSec).toBeCloseTo(tx.segments[1].startSec, 3); expect(m!.endSec).toBeCloseTo(tx.segments[2].endSec, 3); }); it("quote punctuation differences do not break matching", () => { const m = matchQuote(index, "记住这三个字", "别上头"); expect(m).not.toBeNull(); }); it("returns null when text is absent", () => { expect(matchQuote(index, "根本不存在的话", "也不存在")).toBeNull(); }); }); describe("resolveSelection", () => { const tx = makeTranscript(["开场白很平淡。", "但是接下来这句话炸了。", "这就是全网疯传的那个观点。", "后面又归于平静。"]); it("resolves via quotes scoped to declared segments", () => { const r = resolveSelection(tx, { title: "t", hook: "h", score: 90, reason: "r", keywords: [], startSegmentId: 2, endSegmentId: 3, quoteStart: "但是接下来", quoteEnd: "那个观点。", }); expect(r).not.toBeNull(); expect(r!.boundary).toBe("anchored"); expect(r!.startSec).toBeCloseTo(tx.segments[1].startSec, 3); expect(r!.endSec).toBeCloseTo(tx.segments[2].endSec, 3); expect(r!.text).toContain("炸了"); }); it("falls back to segment boundaries when quotes are hallucinated", () => { const r = resolveSelection(tx, { title: "t", hook: "h", score: 50, reason: "r", keywords: [], startSegmentId: 2, endSegmentId: 3, quoteStart: "LLM自己编的话", quoteEnd: "完全对不上", }); expect(r).not.toBeNull(); expect(r!.boundary).toBe("segment"); expect(r!.startSec).toBeCloseTo(tx.segments[1].startSec, 3); }); it("returns null when nothing is locatable", () => { const r = resolveSelection(tx, { title: "t", hook: "h", score: 50, reason: "r", keywords: [], startSegmentId: 99, endSegmentId: 98, quoteStart: "不存在", quoteEnd: "也不存在", }); expect(r).toBeNull(); }); }); describe("parseSelections", () => { it("parses fenced JSON and clamps score", () => { const out = parseSelections('```json\n{"clips":[{"title":"钩子","score":150,"startSegmentId":1,"endSegmentId":2,"quoteStart":"开头","quoteEnd":"结尾"}]}\n```'); expect(out).toHaveLength(1); expect(out[0].score).toBe(100); }); it("drops rows without any locator and throws on non-JSON", () => { const out = parseSelections('{"clips":[{"title":"没定位"},{"quoteStart":"有引文","startSegmentId":1,"endSegmentId":1,"quoteEnd":"x"}]}'); expect(out).toHaveLength(1); expect(() => parseSelections("总之就是不输出JSON")).toThrow(); }); }); describe("parseReviews / applyReviews", () => { it("parses verdicts and tolerates fenced JSON", () => { const out = parseReviews('```json\n{"reviews":[{"id":1,"keep":false,"score":30,"note":"平淡"},{"id":2,"keep":true,"score":88}]}\n```'); expect(out).toEqual([ // 老输出没有 verdict:keep=false 推导为保守的 review 档(不是 drop) { id: 1, keep: false, gate: "review", score: 30, note: "平淡" }, { id: 2, keep: true, gate: "publish", score: 88, note: "" }, ]); }); it("parses the three-tier verdict field and keep follows it", () => { const out = parseReviews( '{"reviews":[{"id":1,"verdict":"publish","score":90},{"id":2,"verdict":"review","keep":true,"score":70,"note":"结尾没收住"},{"id":3,"verdict":"drop","score":20,"note":"凑数"},{"id":4,"verdict":"胡说","keep":true,"score":50}]}' ); // keep 一律由 verdict 推导(publish 才 true),模型把 keep 填错也不影响档位 expect(out.map((r) => [r.gate, r.keep])).toEqual([ ["publish", true], ["review", false], ["drop", false], ["publish", true], // 非法 verdict 回退 keep 推导 ]); }); it("applies verdicts and fails open for unreviewed ids", () => { const base = { startSec: 0, endSec: 10, text: "", title: "", hook: "", reason: "", boundary: "exact" as const, keywords: [], recommended: true, reviewNote: "", }; const cands = [ { ...base, id: 1, score: 90 }, { ...base, id: 2, score: 80 }, ]; const out = applyReviews(cands, [{ id: 1, keep: false, gate: "drop", score: 35, note: "弱钩子" }]); expect(out[0]).toMatchObject({ recommended: false, score: 35, reviewNote: "弱钩子", gate: "drop", gateNotes: ["弱钩子"] }); expect(out[1]).toMatchObject({ recommended: true, score: 80 }); expect(out[1].gate).toBeUndefined(); }); it("throws on malformed reviewer output", () => { expect(() => parseReviews("完全不是JSON")).toThrow(); }); it("parses four-dimension verdicts into a weighted composite", () => { const out = parseReviews( '{"reviews":[{"id":1,"keep":true,"hook":80,"hookNote":"强","flow":60,"flowNote":"顺","value":100,"valueNote":"高","trend":40,"trendNote":"一般","teaser":"倒半杯水会怎样?","note":"总评"}]}' ); expect(out[0].dims).toEqual({ hook: 80, flow: 60, value: 100, trend: 40 }); // 80*.35 + 60*.25 + 100*.25 + 40*.15 = 74 expect(out[0].score).toBe(74); expect(out[0].dimNotes?.value).toBe("高"); expect(out[0].teaser).toBe("倒半杯水会怎样?"); }); it("carries dims and teaser onto candidates via applyReviews", () => { const base = { startSec: 0, endSec: 10, text: "", title: "", hook: "", reason: "", boundary: "exact" as const, keywords: [], recommended: true, reviewNote: "", }; const out = applyReviews( [{ ...base, id: 1, score: 90 }], [{ id: 1, keep: true, gate: "publish", score: 74, note: "", dims: { hook: 80, flow: 60, value: 100, trend: 40 }, teaser: "钩子句" }] ); expect(out[0].scoreDims).toEqual({ hook: 80, flow: 60, value: 100, trend: 40 }); expect(out[0].teaser).toBe("钩子句"); }); }); describe("normalizeScores", () => { const base = { startSec: 0, endSec: 10, text: "", title: "", hook: "", reason: "", boundary: "exact" as const, keywords: [], recommended: true, reviewNote: "", }; it("maps recommended clips onto 76-99 by rank, preserving order", () => { const out = normalizeScores([ { ...base, id: 1, score: 74 }, { ...base, id: 2, score: 88 }, { ...base, id: 3, score: 60 }, ]); expect(out.map((c) => c.id)).toEqual([1, 2, 3]); // array order untouched expect(out.find((c) => c.id === 2)?.score).toBe(99); // best expect(out.find((c) => c.id === 1)?.score).toBe(88); // middle: 99-23*1/2 rounded expect(out.find((c) => c.id === 3)?.score).toBe(76); // worst }); it("a single recommended clip gets 97, rejected clips sit below 75", () => { const out = normalizeScores([ { ...base, id: 1, score: 40 }, { ...base, id: 2, score: 90, recommended: false }, ]); expect(out.find((c) => c.id === 1)?.score).toBe(97); expect(out.find((c) => c.id === 2)?.score).toBe(62); }); }); describe("dropOverlaps", () => { const c = (id: number, s: number, e: number, score: number): HighlightCandidate => ({ id, startSec: s, endSec: e, text: "", title: "", hook: "", score, reason: "", boundary: "exact", keywords: [], recommended: true, reviewNote: "", }); it("keeps higher-scored clip among overlaps, renumbers by time order", () => { const kept = dropOverlaps([c(1, 0, 20, 70), c(2, 10, 30, 90), c(3, 40, 60, 50)]); expect(kept).toHaveLength(2); expect(kept[0].score).toBe(90); expect(kept.map((k) => k.id)).toEqual([1, 2]); }); }); describe("prompt builders", () => { const tx = makeTranscript(["第一句。", "第二句。"]); it("renders [id] MM:SS lines", () => { const lines = renderTranscriptLines(tx).split("\n"); expect(lines[0]).toMatch(/^\[1\] 00:00 第一句。$/); expect(lines[1]).toMatch(/^\[2\] 00:0\d 第二句。$/); }); it("prompt forbids timestamps and demands verbatim quotes", () => { const p = buildHighlightPrompt(tx); expect(p).toContain("quoteStart"); expect(p).toContain("startSegmentId"); expect(p).toContain("逐句稿"); }); it("extractJson handles fences and prose-wrapped objects", () => { expect(extractJson('```json\n{"a":1}\n```')).toBe('{"a":1}'); expect(extractJson('好的,结果如下 {"a":1} 请查收')).toBe('{"a":1}'); }); it("prefixes speaker labels and injects attribution guidance when multi-speaker", () => { const multi = makeTranscript(["嘉宾说的。", "主持人问的。"]); multi.segments[0].speaker = 0; multi.segments[1].speaker = 1; expect(isMultiSpeaker(multi)).toBe(true); const lines = renderTranscriptLines(multi).split("\n"); expect(lines[0]).toContain("S1: 嘉宾说的。"); expect(lines[1]).toContain("S2: 主持人问的。"); expect(buildHighlightPrompt(multi)).toContain("多人对谈"); }); it("stays single-speaker (no S-prefix) when only one speaker present", () => { const one = makeTranscript(["只有一个人。", "还是这个人。"]); one.segments[0].speaker = 0; one.segments[1].speaker = 0; expect(isMultiSpeaker(one)).toBe(false); expect(renderTranscriptLines(one)).not.toContain("S1:"); expect(buildHighlightPrompt(one)).not.toContain("多人对谈"); }); }); describe("prompt language routing(中英分流)", () => { const zhTx = makeTranscript(["今天聊聊怎么把长视频切成爆款。", "关键就三个字。"]); const enTx: Transcript = { language: "en", engine: "test", durationSec: 10, segments: [ { id: 1, startSec: 0, endSec: 4, text: "Today we talk about turning long videos into viral shorts.", words: [{ text: "Today", startSec: 0, endSec: 0.4 }], }, ], }; it("zh transcript → Chinese system + user prompt", () => { expect(isChineseTranscript(zhTx)).toBe(true); expect(highlightSystemPrompt(zhTx)).toContain("切片操盘手"); expect(buildHighlightPrompt(zhTx)).toContain("逐句稿"); }); it("en transcript → English system + user prompt, no Chinese leakage", () => { expect(isChineseTranscript(enTx)).toBe(false); const sys = highlightSystemPrompt(enTx); const user = buildHighlightPrompt(enTx); expect(sys).toContain("clipping strategist"); expect(user).toContain("Transcript"); expect(/[一-鿿]/.test(sys)).toBe(false); // user prompt carries only the transcript text itself, which here is English expect(/[一-鿿]/.test(user)).toBe(false); }); it("auto language falls back to CJK-dominance detection", () => { const autoTx: Transcript = { ...zhTx, language: "auto" }; expect(isChineseTranscript(autoTx)).toBe(true); const autoEn: Transcript = { ...enTx, language: "auto" }; expect(isChineseTranscript(autoEn)).toBe(false); }); }); describe("时长档 (clip length)", () => { it("standard 保持原 system prompt;short/long 改写时长行(中英)", () => { const zh = makeTranscript(["第一句话。", "第二句话。"]); expect(highlightSystemPrompt(zh)).toContain("时长 8~40 秒"); expect(highlightSystemPrompt(zh, "short")).toContain("时长 10~30 秒(硬要求,宁短勿超)"); expect(highlightSystemPrompt(zh, "long")).toContain("时长 40~90 秒"); const en: Transcript = { ...makeTranscript(["First sentence here.", "Second sentence there."]), language: "en" }; expect(highlightSystemPrompt(en, "short")).toContain("Length 10–30 seconds (hard requirement)"); expect(highlightSystemPrompt(en, "long")).toContain("Length 40–90 seconds"); }); it("clipLengthBounds:目标外放容差,绝对下限 4 秒", () => { expect(clipLengthBounds("standard")).toEqual({ lo: 4, hi: 60 }); expect(clipLengthBounds("short")).toEqual({ lo: 5, hi: 45 }); expect(clipLengthBounds("long")).toEqual({ lo: 20, hi: 135 }); expect(clipLengthBounds()).toEqual(clipLengthBounds("standard")); }); }); describe("renderSignals / signal injection", () => { const signals = { loudPeaks: [{ startSec: 192, endSec: 198 }], cutDense: [{ startSec: 500, endSec: 515 }], }; it("renders bilingual signal blocks with MM:SS ranges", () => { const zh = renderSignals(signals, true); expect(zh).toContain("画面与声音信号"); expect(zh).toContain("03:12-03:18"); const en = renderSignals(signals, false); expect(en).toContain("Audiovisual signals"); expect(en).toContain("08:20-08:35"); }); it("empty signals render nothing", () => { expect(renderSignals({ loudPeaks: [], cutDense: [] }, true)).toBe(""); expect(renderSignals(undefined, true)).toBe(""); }); it("emotionPeaks 存在时渲染表情峰值行", () => { const withEmotion = { ...signals, emotionPeaks: [{ startSec: 30, endSec: 42 }] }; const zh = renderSignals(withEmotion, true); expect(zh).toContain("人脸表情峰值时段"); expect(zh).toContain("00:30-00:42"); expect(renderSignals(withEmotion, false)).toContain("Facial-emotion peaks"); expect(renderSignals(signals, true)).not.toContain("表情峰值"); }); it("visualPeaks 存在时渲染视觉爆点行", () => { const withVision = { ...signals, visualPeaks: [{ startSec: 60, endSec: 72 }] }; const zh = renderSignals(withVision, true); expect(zh).toContain("视觉模型判定的画面爆点时刻"); expect(zh).toContain("01:00-01:12"); expect(renderSignals(withVision, false)).toContain("Vision-model visual peaks"); // 不带 visualPeaks 时该行不出现(老调用方零感知) expect(renderSignals(signals, true)).not.toContain("视觉模型"); }); it("buildHighlightPrompt embeds the signal section when provided", () => { const tx = makeTranscript(["第一句话。", "第二句话。"]); expect(buildHighlightPrompt(tx, 6, signals)).toContain("画面与声音信号"); expe - src/core/__tests__/jianying.test.ts (4173 bytes)
/** * 剪映草稿导出:draft_content.json 结构与 pyJianYingDraft 5.9 口径对齐。 */ import { describe, it, expect } from "vitest"; import { buildDraftContent, buildDraftMetaInfo } from "../jianying"; /** 递增 id 生成器:测试可复现。 */ const seqIds = (): (() => string) => { let n = 0; return () => { n += 1; return `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`; }; }; const INPUT = { sourcePath: "/v/我的直播回放.mp4", sourceName: "我的直播回放.mp4", sourceDurationSec: 5427.5, width: 1920, height: 1080, fps: 30, clip: { title: "测试切片", segments: [ { startSec: 100.2, endSec: 112.8 }, { startSec: 115.0, endSec: 120.5 }, ], }, }; describe("buildDraftContent", () => { const content = buildDraftContent(INPUT, seqIds()) as Record<string, any>; it("画布=原画幅,时长=保留段总和(微秒),单视频轨", () => { expect(content.canvas_config).toEqual({ height: 1080, ratio: "original", width: 1920 }); expect(content.duration).toBe(Math.round(12.6e6) + Math.round(5.5e6)); expect(content.tracks).toHaveLength(1); expect(content.tracks[0].type).toBe("video"); }); it("片段 source 反链源片区间,target 连续拼接,每段独立可拖", () => { const segs = content.tracks[0].segments; expect(segs).toHaveLength(2); expect(segs[0].source_timerange).toEqual({ start: 100_200_000, duration: 12_600_000 }); expect(segs[0].target_timerange.start).toBe(0); expect(segs[1].source_timerange.start).toBe(115_000_000); expect(segs[1].target_timerange.start).toBe(segs[0].target_timerange.duration); expect(segs[0].speed).toBe(1.0); expect(segs[0].visible).toBe(true); expect(segs[0].render_index).toBe(0); }); it("素材反链源片绝对路径,时长为源片全长;speed 素材与片段一一对应且被引用", () => { const mats = content.materials; expect(mats.videos).toHaveLength(1); expect(mats.videos[0].path).toBe("/v/我的直播回放.mp4"); expect(mats.videos[0].duration).toBe(5_427_500_000); expect(mats.videos[0].type).toBe("video"); expect(mats.speeds).toHaveLength(2); const speedIds = mats.speeds.map((s: { id: string }) => s.id); const segs = content.tracks[0].segments; expect(segs[0].extra_material_refs).toEqual([speedIds[0]]); expect(segs[1].extra_material_refs).toEqual([speedIds[1]]); // 片段都指向同一份素材 expect(new Set(segs.map((s: { material_id: string }) => s.material_id)).size).toBe(1); expect(segs[0].material_id).toBe(mats.videos[0].id); }); it("materials 空分类齐全(缺键剪映按损坏草稿处理);平台标 5.9 模板", () => { for (const key of ["audios", "texts", "transitions", "canvases", "effects", "masks", "stickers"]) { expect(Array.isArray(content.materials[key])).toBe(true); } expect(content.platform.app_version).toBe("5.9.0"); expect(content.version).toBe(360000); expect(content.new_version).toBe("110.0.0"); }); it("零长/负长的段被过滤;素材时长兜底不小于片段截取终点", () => { const weird = buildDraftContent( { ...INPUT, sourceDurationSec: 0, // 容器时长未知 clip: { title: "t", segments: [{ startSec: 5, endSec: 5 }, { startSec: 10, endSec: 20 }] }, }, seqIds() ) as Record<string, any>; expect(weird.tracks[0].segments).toHaveLength(1); expect(weird.materials.videos[0].duration).toBeGreaterThanOrEqual(20_000_000); }); it("同一 id 生成器序列 → 输出可复现", () => { const a = JSON.stringify(buildDraftContent(INPUT, seqIds())); const b = JSON.stringify(buildDraftContent(INPUT, seqIds())); expect(a).toBe(b); }); }); describe("buildDraftMetaInfo", () => { it("draft_id 为大写 UUID;draft_materials 七个分类槽位", () => { const meta = buildDraftMetaInfo(seqIds()) as Record<string, any>; expect(meta.draft_id).toMatch(/^[0-9A-F-]{36}$/); expect(meta.draft_materials.map((m: { type: number }) => m.type)).toEqual([0, 1, 2, 3, 6, 7, 8]); expect(meta.draft_fold_path).toBe(""); }); }); - src/core/__tests__/llm-models.test.ts (2874 bytes)
import { afterEach, describe, it, expect, vi } from "vitest"; import { listModels, parseModelIds, MODEL_LIST_MAX, MODEL_LIST_TIMEOUT_MS } from "../llm-models"; afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); }); describe("listModels 请求边界", () => { it("正文挂起在 12 秒内结束并允许手填", async () => { vi.useFakeTimers(); const cancel = vi.fn(); vi.stubGlobal("fetch", vi.fn(async () => new Response(new ReadableStream({ cancel })))); const pending = listModels("http://[::1]:11434/v1", ""); await vi.advanceTimersByTimeAsync(MODEL_LIST_TIMEOUT_MS); expect(await pending).toEqual({ ids: [], error: expect.stringContaining("超时") }); expect(cancel).toHaveBeenCalledTimes(1); expect(vi.getTimerCount()).toBe(0); }); it("接口错误隐藏密钥且不自动重发模型列表请求", async () => { const fetchMock = vi.fn(async () => new Response('{"error":{"message":"busy for sk-private"}}', { status: 503 })); vi.stubGlobal("fetch", fetchMock); expect(await listModels("https://example.com/v1", "sk-private")).toEqual({ ids: [], error: "HTTP 503: busy for [redacted]" }); expect(fetchMock).toHaveBeenCalledTimes(1); }); it("响应头声明过大的模型列表时关闭流并允许手填", async () => { const cancel = vi.fn(); vi.stubGlobal("fetch", vi.fn(async () => new Response(new ReadableStream({ cancel }), { headers: { "content-length": "9000000" } }))); expect(await listModels("http://127.0.0.1:11434/v1", "")).toEqual({ ids: [], error: expect.stringContaining("响应过大") }); expect(cancel).toHaveBeenCalledTimes(1); }); }); describe("parseModelIds", () => { it("认 OpenAI 标准形状 {data:[{id}]}", () => { expect(parseModelIds({ data: [{ id: "deepseek-v4-flash" }, { id: "deepseek-v4-pro" }] })).toEqual([ "deepseek-v4-flash", "deepseek-v4-pro", ]); }); it("也认裸数组和 {models:[]}(个别家不按标准来)", () => { expect(parseModelIds(["glm-4.7"])).toEqual(["glm-4.7"]); expect(parseModelIds({ models: [{ id: "qwen-plus" }] })).toEqual(["qwen-plus"]); }); it("去重、去空白、按字母序排——下拉里不该出现重复项", () => { expect(parseModelIds({ data: [{ id: "b" }, { id: " b " }, { id: "a" }, { id: "" }] })).toEqual(["a", "b"]); }); it("形状不对时返回空数组而不是抛异常(这只是个填表帮手)", () => { expect(parseModelIds(null)).toEqual([]); expect(parseModelIds({ error: "unauthorized" })).toEqual([]); expect(parseModelIds("nope")).toEqual([]); }); it("聚合平台几百个模型时截断,不撑爆下拉", () => { const many = Array.from({ length: MODEL_LIST_MAX + 50 }, (_, i) => ({ id: `m${String(i).padStart(4, "0")}` })); expect(parseModelIds({ data: many })).toHaveLength(MODEL_LIST_MAX); }); }); - src/core/__tests__/llm-preflight.test.ts (3447 bytes)
/** * LLM 配置预检判定(issue #6):三类必败局要拦准,其余一律放行—— * fail-open 是底线,预检永远不能拦住一套本来能跑的配置。 */ import { describe, expect, it } from "vitest"; import { preflightVerdict, isLocalBaseUrl } from "../../shared/llm-preflight"; const OLLAMA = "http://localhost:11434/v1"; const CLOUD = "https://api.atlascloud.ai/v1"; describe("isLocalBaseUrl", () => { it("localhost 与 127.0.0.1 算本地,云端域名不算", () => { expect(isLocalBaseUrl(OLLAMA)).toBe(true); expect(isLocalBaseUrl("http://127.0.0.1:1234/v1")).toBe(true); expect(isLocalBaseUrl(CLOUD)).toBe(false); }); it("支持 IPv6 回环,但不把路径、用户名或相似域名当作本机", () => { expect(isLocalBaseUrl("http://[::1]:11434/v1")).toBe(true); for (const url of ["https://localhost.example.com/v1", "https://example.com/127.0.0.1", "https://[email protected]/v1", "file://localhost/path", "localhost"]) { expect(isLocalBaseUrl(url)).toBe(false); } }); }); describe("preflightVerdict", () => { it("本地连不上 → local-down(Ollama 没装/没启动)", () => { expect(preflightVerdict({ ids: [], error: "fetch failed" }, OLLAMA, "qwen3:8b")).toEqual({ kind: "local-down" }); expect(preflightVerdict({ ids: [], error: "connect ECONNREFUSED 127.0.0.1:11434" }, OLLAMA, "qwen3:8b")).toEqual({ kind: "local-down", }); expect(preflightVerdict({ ids: [], error: "Model response timed out" }, "http://[::1]:11434/v1", "qwen3:8b")).toEqual({ kind: "local-down" }); }); it("云端连不上 → unreachable(网络/地址问题)", () => { expect(preflightVerdict({ ids: [], error: "fetch failed" }, CLOUD, "m")).toEqual({ kind: "unreachable" }); expect(preflightVerdict({ ids: [], error: "The operation was aborted due to timeout" }, CLOUD, "m")).toEqual({ kind: "unreachable", }); }); it("HTTP 401/403 → auth(Key 空/错/过期)", () => { expect(preflightVerdict({ ids: [], error: "HTTP 401: invalid api key" }, CLOUD, "m")).toEqual({ kind: "auth" }); expect(preflightVerdict({ ids: [], error: "HTTP 403: forbidden" }, CLOUD, "m")).toEqual({ kind: "auth" }); }); it("端点没实现 /models(404/空清单) → unknown,放行", () => { expect(preflightVerdict({ ids: [], error: "HTTP 404: not found" }, CLOUD, "m")).toEqual({ kind: "unknown" }); expect( preflightVerdict({ ids: [], error: "该端点没有返回模型清单 / endpoint returned no models" }, CLOUD, "m") ).toEqual({ kind: "unknown" }); }); it("本地模型没拉 → model-missing 并附已装清单", () => { const res = { ids: ["llama3:latest", "qwen2:7b"], error: null }; expect(preflightVerdict(res, OLLAMA, "qwen3:8b")).toEqual({ kind: "model-missing", installed: ["llama3:latest", "qwen2:7b"], }); }); it("Ollama 省略 :latest 标签也算命中", () => { const res = { ids: ["llama3:latest"], error: null }; expect(preflightVerdict(res, OLLAMA, "llama3")).toEqual({ kind: "ok" }); }); it("本地模型在清单里 → ok", () => { expect(preflightVerdict({ ids: ["qwen3:8b"], error: null }, OLLAMA, "qwen3:8b")).toEqual({ kind: "ok" }); }); it("云端清单可能不全,不据此拦人 → 模型不在清单也 ok", () => { expect(preflightVerdict({ ids: ["other-model"], error: null }, CLOUD, "my-model")).toEqual({ kind: "ok" }); }); }); - tsconfig.json (107 bytes)
{ "files": [], "references": [{ "path": "./tsconfig.node.json" }, { "path": "./tsconfig.web.json" }] } - tsconfig.node.json (502 bytes)
{ "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "lib": ["ES2023"], "types": ["node"], "strict": true, "noEmit": true, "skipLibCheck": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "paths": { "@core/*": ["./src/core/*"] } }, "include": ["src/main/**/*", "src/preload/**/*", "src/core/**/*", "src/shared/**/*", "electron.vite.config.ts"] } - tsconfig.web.json (533 bytes)
{ "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "bundler", "lib": ["ES2023", "DOM", "DOM.Iterable"], "jsx": "react-jsx", "strict": true, "noEmit": true, "skipLibCheck": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "useDefineForClassFields": true, "paths": { "@/*": ["./src/renderer/src/*"] } }, "include": ["src/renderer/src/**/*", "src/preload/*.d.ts", "src/shared/**/*"] } - vite.renderer.config.ts (467 bytes)
import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; import { resolve } from "path"; /** Browser-mock development surface: exercises the renderer without installing or launching Electron. */ export default defineConfig({ root: resolve(__dirname, "src/renderer"), plugins: [react(), tailwindcss()], resolve: { alias: { "@": resolve(__dirname, "src/renderer/src"), }, }, }); - vitest.config.ts (286 bytes)
import { defineConfig } from "vitest/config"; import { resolve } from "path"; export default defineConfig({ test: { include: ["src/**/__tests__/**/*.test.ts"], environment: "node", }, resolve: { alias: { "@core": resolve(__dirname, "src/core"), }, }, });