Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix: normalize simplified Chinese subtitle outputs

Leggett2d840062f7b0
diff --git a/apps/daemon/src/creator/krillin/cli-runner.ts b/apps/daemon/src/creator/krillin/cli-runner.ts
index ae126d65..ddf13662 100644
--- a/apps/daemon/src/creator/krillin/cli-runner.ts
+++ b/apps/daemon/src/creator/krillin/cli-runner.ts
@@ -18,6 +18,7 @@ import {
resolveInside,
type KrillinRuntimeManifest
} from './manifest.js';
+import { normalizeSimplifiedChineseSubtitleOutputs } from './subtitle-script.js';
import type { YtDlpRuntime } from '../yt-dlp/runtime.js';
export type MaterializedKrillinArtifact = {
@@ -529,7 +530,7 @@ async function collectArtifacts(
response: KrillinCliResponse
): Promise<KrillinResultArtifact[]> {
const mappings = outputMappings(input.stage.stageRun.stageId);
- const result: KrillinResultArtifact[] = [];
+ const candidates: Array<{ field: string; kind: string; path: string }> = [];
const jobRoot = await realpath(resolve(input.jobsRoot, input.stage.job.id));
for (const [field, kind] of mappings) {
if (kind === 'source_video' && input.stage.stageRun.stageId !== 'subtitle' && input.artifacts.some(artifact => artifact.kind === 'source_video')) continue;
@@ -545,6 +546,17 @@ async function collectArtifacts(
if (!isInside(jobRoot, path)) {
throw new CreatorExecutorError('krillin_output_escape', `KrillinAI output escapes the current Job root: ${field}`);
}
+ const info = await stat(path);
+ if (!info.isFile() || info.size === 0) continue;
+ candidates.push({ field, kind, path });
+ }
+ await normalizeSimplifiedChineseSubtitleOutputs(candidates, {
+ originLanguage: input.options.originLanguage,
+ targetLanguage: input.options.targetLanguage,
+ bilingualTop: booleanOption(input.options, 'bilingualTop', true)
+ });
+ const result: KrillinResultArtifact[] = [];
+ for (const { field, kind, path } of candidates) {
const info = await stat(path);
if (!info.isFile() || info.size === 0) continue;
result.push({
diff --git a/apps/daemon/src/creator/krillin/subtitle-script.ts b/apps/daemon/src/creator/krillin/subtitle-script.ts
new file mode 100644
index 00000000..429df3f4
--- /dev/null
+++ b/apps/daemon/src/creator/krillin/subtitle-script.ts
@@ -0,0 +1,147 @@
+import { readFile, writeFile } from 'node:fs/promises';
+import { Converter } from 'opencc-js/t2cn';
+
+type SubtitleOutput = {
+ kind: string;
+ path: string;
+};
+
+type NormalizeOptions = {
+ originLanguage?: unknown;
+ targetLanguage?: unknown;
+ bilingualTop: boolean;
+};
+
+const toSimplified = Converter({ from: 't', to: 'cn' });
+
+export async function normalizeSimplifiedChineseSubtitleOutputs(
+ outputs: readonly SubtitleOutput[],
+ options: NormalizeOptions
+): Promise<void> {
+ const sourceNeedsNormalization = isSimplifiedChinese(options.originLanguage);
+ const targetNeedsNormalization = isSimplifiedChinese(options.targetLanguage);
+ if (!sourceNeedsNormalization && !targetNeedsNormalization) return;
+
+ const source = outputs.find(output => output.kind === 'source_subtitle');
+ const target = outputs.find(output => output.kind === 'target_subtitle');
+ const [sourceText, targetText] = await Promise.all([
+ source === undefined ? undefined : readFile(source.path, 'utf8'),
+ target === undefined ? undefined : readFile(target.path, 'utf8')
+ ]);
+ const sourceKeys = new Set(sourceText === undefined ? [] : subtitleTextKeys(sourceText));
+ const targetKeys = new Set(targetText === undefined ? [] : subtitleTextKeys(targetText));
+
+ await Promise.all(outputs
+ .filter(output => ['source_subtitle', 'target_subtitle', 'bilingual_subtitle', 'vertical_subtitle'].includes(output.kind))
+ .map(async output => {
+ const content = output.kind === 'source_subtitle' && sourceText !== undefined
+ ? sourceText
+ : output.kind === 'target_subtitle' && targetText !== undefined
+ ? targetText
+ : await readFile(output.path, 'utf8');
+ const role = output.kind === 'source_subtitle'
+ ? sourceNeedsNormalization ? 'source' : 'none'
+ : output.kind === 'target_subtitle'
+ ? targetNeedsNormalization ? 'target' : 'none'
+ : 'mixed';
+ const normalized = normalizeSrtContent(content, {
+ role,
+ sourceKeys,
+ targetKeys,
+ bilingualTop: options.bilingualTop,
+ sourceNeedsNormalization,
+ targetNeedsNormalization
+ });
+ if (normalized !== content) await writeFile(output.path, normalized, 'utf8');
+ }));
+}
+
+export function normalizeSrtContent(
+ content: string,
+ options: {
+ role: 'source' | 'target' | 'mixed' | 'none';
+ sourceKeys?: ReadonlySet<string>;
+ targetKeys?: ReadonlySet<string>;
+ bilingualTop?: boolean;
+ sourceNeedsNormalization?: boolean;
+ targetNeedsNormalization?: boolean;
+ }
+): string {
+ if (options.role === 'none') return content;
+ const lineEnding = content.includes('\r\n') ? '\r\n' : '\n';
+ const lines = content.split(/\r?\n/);
+ for (let index = 0; index + 2 < lines.length;) {
+ const cueIndex = lines[index];
+ const timestamp = lines[index + 1];
+ if (cueIndex === undefined || timestamp === undefined
+ || !/^\d+$/.test(cueIndex.trim()) || !isSrtTimestampLine(timestamp)) {
+ index++;
+ continue;
+ }
+ let end = index + 2;
+ while (end < lines.length && lines[end]?.trim() !== '') end++;
+ const textLines = lines.slice(index + 2, end);
+ for (let offset = 0; offset < textLines.length; offset++) {
+ const textLine = textLines[offset];
+ if (textLine === undefined) continue;
+ const key = subtitleTextKey(textLine);
+ let shouldNormalize = options.role === 'source'
+ ? options.sourceNeedsNormalization === true
+ : options.role === 'target'
+ ? options.targetNeedsNormalization === true
+ : false;
+ if (options.role === 'mixed') {
+ const isSource = options.sourceKeys?.has(key) === true;
+ const isTarget = options.targetKeys?.has(key) === true;
+ if (isSource && !isTarget) shouldNormalize = options.sourceNeedsNormalization === true;
+ else if (isTarget && !isSource) shouldNormalize = options.targetNeedsNormalization === true;
+ else {
+ const targetOffset = options.bilingualTop === false ? textLines.length - 1 : 0;
+ shouldNormalize = offset === targetOffset
+ ? options.targetNeedsNormalization === true
+ : options.sourceNeedsNormalization === true;
+ }
+ }
+ if (shouldNormalize) lines[index + 2 + offset] = toSimplified(textLine);
+ }
+ index = end;
+ }
+ const result = lines.join(lineEnding);
+ return content.endsWith(lineEnding) && !result.endsWith(lineEnding) ? `${result}${lineEnding}` : result;
+}
+
+function subtitleTextKeys(content: string): string[] {
+ const lines = content.split(/\r?\n/);
+ const keys: string[] = [];
+ for (let index = 0; index + 2 < lines.length;) {
+ const cueIndex = lines[index];
+ const timestamp = lines[index + 1];
+ if (cueIndex === undefined || timestamp === undefined
+ || !/^\d+$/.test(cueIndex.trim()) || !isSrtTimestampLine(timestamp)) {
+ index++;
+ continue;
+ }
+ let end = index + 2;
+ while (end < lines.length && lines[end]?.trim() !== '') end++;
+ for (const line of lines.slice(index + 2, end)) {
+ const key = subtitleTextKey(line);
+ if (key) keys.push(key);
+ }
+ index = end;
+ }
+ return keys;
+}
+
+function subtitleTextKey(value: string): string {
+ return value.replace(/<[^>]+>|\{\\[^}]+\}/g, '').trim();
+}
+
+function isSrtTimestampLine(value: string): boolean {
+ return /^\s*\d{2}:\d{2}:\d{2}[,.]\d{3}\s+-->\s+\d{2}:\d{2}:\d{2}[,.]\d{3}/.test(value);
+}
+
+function isSimplifiedChinese(value: unknown): boolean {
+ if (typeof value !== 'string') return false;
+ const normalized = value.trim().toLowerCase().replace('-', '_');
+ return normalized === 'zh_cn' || normalized === 'zh_hans';
+}
diff --git a/apps/daemon/test/unit/creator-subtitle-script.test.ts b/apps/daemon/test/unit/creator-subtitle-script.test.ts
new file mode 100644
index 00000000..1cb5a560
--- /dev/null
+++ b/apps/daemon/test/unit/creator-subtitle-script.test.ts
@@ -0,0 +1,47 @@
+import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { describe, expect, it } from 'vitest';
+import {
+ normalizeSimplifiedChineseSubtitleOutputs,
+ normalizeSrtContent
+} from '../../src/creator/krillin/subtitle-script.js';
+
+const sourceSrt = `1\n00:00:00,000 --> 00:00:02,000\n這個濾光片的功能就是\n`;
+
+describe('creator subtitle script normalization', () => {
+ it('converts a zh_cn source subtitle to simplified Chinese', () => {
+ expect(normalizeSrtContent(sourceSrt, {
+ role: 'source',
+ sourceNeedsNormalization: true
+ })).toContain('这个滤光片的功能就是');
+ });
+
+ it('normalizes only the source line in a bilingual cue', () => {
+ const mixed = `1\n00:00:00,000 --> 00:00:02,000\nThe filter works like this.\n這個濾光片的功能就是\n`;
+ expect(normalizeSrtContent(mixed, {
+ role: 'mixed',
+ sourceKeys: new Set(['這個濾光片的功能就是']),
+ targetKeys: new Set(['The filter works like this.']),
+ bilingualTop: true,
+ sourceNeedsNormalization: true,
+ targetNeedsNormalization: false
+ })).toContain('The filter works like this.\n这个滤光片的功能就是');
+ });
+
+ it('leaves traditional Chinese untouched when it is the selected language', async () => {
+ const directory = await mkdtemp(join(tmpdir(), 'creator-subtitle-script-'));
+ const path = join(directory, 'source.srt');
+ try {
+ await writeFile(path, sourceSrt, 'utf8');
+ await normalizeSimplifiedChineseSubtitleOutputs([{ kind: 'source_subtitle', path }], {
+ originLanguage: 'zh_tw',
+ targetLanguage: 'en',
+ bilingualTop: true
+ });
+ await expect(readFile(path, 'utf8')).resolves.toBe(sourceSrt);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+});