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 short source subtitle fragments

Leggett2e13b89a0129
diff --git a/apps/daemon/src/creator/krillin/subtitle-script.ts b/apps/daemon/src/creator/krillin/subtitle-script.ts
index 429df3f4..8bff38fd 100644
--- a/apps/daemon/src/creator/krillin/subtitle-script.ts
+++ b/apps/daemon/src/creator/krillin/subtitle-script.ts
@@ -46,6 +46,7 @@ export async function normalizeSimplifiedChineseSubtitleOutputs(
: 'mixed';
const normalized = normalizeSrtContent(content, {
role,
+ kind: output.kind === 'vertical_subtitle' ? 'vertical_subtitle' : 'bilingual_subtitle',
sourceKeys,
targetKeys,
bilingualTop: options.bilingualTop,
@@ -60,6 +61,7 @@ export function normalizeSrtContent(
content: string,
options: {
role: 'source' | 'target' | 'mixed' | 'none';
+ kind?: 'bilingual_subtitle' | 'vertical_subtitle';
sourceKeys?: ReadonlySet<string>;
targetKeys?: ReadonlySet<string>;
bilingualTop?: boolean;
@@ -91,10 +93,13 @@ export function normalizeSrtContent(
? options.targetNeedsNormalization === true
: false;
if (options.role === 'mixed') {
- const isSource = options.sourceKeys?.has(key) === true;
- const isTarget = options.targetKeys?.has(key) === true;
+ const isSource = subtitleKeyMatches(options.sourceKeys, key);
+ const isTarget = subtitleKeyMatches(options.targetKeys, key);
if (isSource && !isTarget) shouldNormalize = options.sourceNeedsNormalization === true;
else if (isTarget && !isSource) shouldNormalize = options.targetNeedsNormalization === true;
+ else if (options.kind === 'vertical_subtitle' && textLines.length === 1) {
+ shouldNormalize = options.sourceNeedsNormalization === true;
+ }
else {
const targetOffset = options.bilingualTop === false ? textLines.length - 1 : 0;
shouldNormalize = offset === targetOffset
@@ -136,6 +141,14 @@ function subtitleTextKey(value: string): string {
return value.replace(/<[^>]+>|\{\\[^}]+\}/g, '').trim();
}
+function subtitleKeyMatches(keys: ReadonlySet<string> | undefined, value: string): boolean {
+ if (value === '' || keys === undefined) return false;
+ for (const key of keys) {
+ if (key === value || key.includes(value) || value.includes(key)) return true;
+ }
+ return false;
+}
+
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);
}
diff --git a/apps/daemon/test/unit/creator-subtitle-script.test.ts b/apps/daemon/test/unit/creator-subtitle-script.test.ts
index 1cb5a560..8ed926cb 100644
--- a/apps/daemon/test/unit/creator-subtitle-script.test.ts
+++ b/apps/daemon/test/unit/creator-subtitle-script.test.ts
@@ -29,6 +29,18 @@ describe('creator subtitle script normalization', () => {
})).toContain('The filter works like this.\n这个滤光片的功能就是');
});
+ it('normalizes a short source fragment in a vertical subtitle cue', () => {
+ const shortMixed = `1\n00:00:00,000 --> 00:00:01,000\n管帽管壳與管帽\n`;
+ expect(normalizeSrtContent(shortMixed, {
+ role: 'mixed',
+ kind: 'vertical_subtitle',
+ sourceKeys: new Set(['管帽管壳與管帽結合在一起的彩色部分']),
+ targetKeys: new Set(['The colored section']),
+ sourceNeedsNormalization: true,
+ targetNeedsNormalization: false
+ })).toContain('管帽管壳与管帽');
+ });
+
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');