feat: subtitle: support ai segmentation

This commit is contained in:
Gabe
2025-10-09 23:55:06 +08:00
parent 2d0ea09e06
commit 9bafc937d5
12 changed files with 294 additions and 50 deletions

View File

@@ -354,6 +354,24 @@ Output: {"translations":[{"id":1,"text":"一个<b>React</b>组件","sourceLangua
Fail-safe: On any error, return {"translations":[]}.`;
const defaultSubtitlePrompt = `Goal: Convert raw subtitle event JSON into a clean, sentence-based JSON array.
Output (valid JSON array, output ONLY this array):
[{
"text": "string", // Full sentence with correct punctuation
"translation": "string", // Translation in ${INPUT_PLACE_TO}
"start": int, // Start time (ms)
"end": int, // End time (ms)
"duration": int // end - start
}]
Guidelines:
1. **Segmentation**: Merge sequential 'utf8' strings from 'segs' into full sentences, merging groups logically.
2. **Punctuation**: Ensure proper sentence-final punctuation (., ?, !); add if missing.
3. **Translation**: Translate 'text' into ${INPUT_PLACE_TO}, place result in 'translation'.
4. **Special Cases**: '[Music]' (and similar cues) are standalone entries. Translate appropriately (e.g., '[音乐]', '[Musique]').
`;
const defaultRequestHook = `async (args, { url, body, headers, userMsg, method } = {}) => {
console.log("request hook args:", args);
// return { url, body, headers, userMsg, method };
@@ -375,6 +393,7 @@ const defaultApi = {
key: "",
model: "", // 模型名称
systemPrompt: defaultSystemPrompt,
subtitlePrompt: defaultSubtitlePrompt,
userPrompt: "",
tone: BUILTIN_STONES[0], // 翻译风格
placeholder: BUILTIN_PLACEHOLDERS[0], // 占位符

View File

@@ -1484,9 +1484,9 @@ export const I18N = {
zh_TW: `佔位標名`,
},
system_prompt_helper: {
zh: `在未完全理解默认Prompt的情况下请勿随意修改否则可能翻译失败`,
en: `If you do not fully understand the default prompt, please do not modify it at will, otherwise the translation may fail.`,
zh_TW: `在未完全理解預設Prompt的情況下請勿隨意修改否則可能翻譯失敗`,
zh: `在未完全理解默认Prompt的情况下请勿随意修改否则可能无法工作`,
en: `Do not modify the default prompt without fully understanding it, otherwise it may not work.`,
zh_TW: `在未完全理解預設Prompt的情況下請勿隨意修改否則可能無法運作`,
},
if_pre_init: {
zh: `是否预初始化`,
@@ -1568,4 +1568,19 @@ export const I18N = {
en: `Translation style`,
zh_TW: `譯文樣式`,
},
ai_segmentation: {
zh: `AI智能断句`,
en: `AI intelligent punctuation`,
zh_TW: `AI智慧斷句`,
},
subtitle_helper_1: {
zh: `1、目前仅支持Youtube英文字幕双语翻译且仅支持浏览器扩展。`,
en: `1. Currently only supports bilingual translation of Youtube English subtitles, and only supports browser extensions.`,
zh_TW: `1.目前僅支援Youtube英文字幕雙語翻譯且僅支援瀏覽器擴充功能。`,
},
subtitle_helper_2: {
zh: `2、插件内置基础断句逻辑如不理想可以启用AI智能断句但需考虑视频长度与AI接口的处理能力可能等待的时间会很长甚至失败。`,
en: `2. The plug-in has built-in basic segmentation logic. If it is not ideal, you can enable AI intelligent segmentation. However, you need to consider the video length and the processing power of the AI interface. The waiting time may be very long or even fail.`,
zh_TW: `2.插件內建基礎斷句邏輯如不理想可以啟用AI智能斷句但需考慮視訊長度與AI介面的處理能力可能等待的時間會很長甚至失敗。`,
},
};

View File

@@ -111,6 +111,7 @@ const SUBTITLE_TRANSLATION_STYLE = `font-size: clamp(1.5rem, 3cqw, 3rem);`;
export const DEFAULT_SUBTITLE_SETTING = {
enabled: true, // 是否开启
apiSlug: OPT_TRANS_MICROSOFT,
segSlug: "-", // AI智能断句
// fromLang: "en",
toLang: "zh-CN",
isBilingual: true, // 是否双语显示

View File

@@ -1,6 +1,7 @@
import { APP_LCNAME } from "./app";
export const URL_CACHE_TRAN = `https://${APP_LCNAME}/translate`;
export const URL_CACHE_SUBTITLE = `https://${APP_LCNAME}/subtitle`;
export const URL_CACHE_DELANG = `https://${APP_LCNAME}/detectlang`;
export const URL_CACHE_BINGDICT = `https://${APP_LCNAME}/bingdict`;