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.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
fix(video-translation): align subtitle previews with burned-in styles
Leggettfd616f4d3fd2
diff --git a/apps/daemon/src/api/routes.creator.ts b/apps/daemon/src/api/routes.creator.tsindex 68012467..5658a761 100644--- a/apps/daemon/src/api/routes.creator.ts+++ b/apps/daemon/src/api/routes.creator.ts@@ -23,7 +23,9 @@ import { rm, stat } from 'node:fs/promises';import { basename, extname, resolve, sep } from 'node:path';import type { Readable } from 'node:stream';import type { FastifyInstance, FastifyReply } from 'fastify';-import { ZodError } from 'zod';+import { z, ZodError } from 'zod';+import { creatorSubtitleStyleSchema } from '../creator/presets/module-schemas.js';+import type { SubtitlePreviewService } from '../creator/krillin/subtitle-preview.js';import type { CreatorEventHub } from '../creator/events.js';import {creatorAgentEventKind,@@ -108,8 +110,39 @@ export async function registerCreatorRoutes(presets?: CreatorPresetRegistry;presetCatalogRoot?: string;issueService?: CreatorIssueService;+ subtitlePreviewService?: SubtitlePreviewService;}): Promise<void> {+ server.get('/creator/subtitle-preview', async (request, reply) => {+ try {+ const query = readObject(request.query);+ const input = z.object({+ width: z.number().int().min(32).max(1920),+ height: z.number().int().min(32).max(1920),+ subtitleStyle: creatorSubtitleStyleSchema,+ bilingual: z.boolean(),+ subtitlePosition: z.enum(['top', 'bottom']),+ translationText: z.string().min(1).max(128).regex(/^[^\r\n{}\\]+$/u),+ originalText: z.string().min(1).max(128).regex(/^[^\r\n{}\\]+$/u)+ }).strict().parse(JSON.parse(readString(query.options, 'options')));+ if (options.subtitlePreviewService === undefined) {+ return reply.code(503).send(apiError('creator_subtitle_style_unsupported', 'Subtitle renderer is unavailable'));+ }+ const controller = new AbortController();+ const cancel = () => { if (!reply.raw.writableEnded) controller.abort(); };+ reply.raw.once('close', cancel);+ try {+ const png = await options.subtitlePreviewService.render(input, controller.signal);+ return reply.type('image/png').header('Cache-Control', 'no-store').send(png);+ } finally {+ reply.raw.removeListener('close', cancel);+ }+ } catch (error) {+ if (error instanceof SyntaxError) return reply.code(400).send(apiError('VALIDATION_FAILED', 'Invalid preview options'));+ if (error instanceof ZodError || error instanceof TypeError) return sendCreatorError(reply, error);+ return reply.code(503).send(apiError('creator_subtitle_style_unsupported', error instanceof Error ? error.message : 'Subtitle preview failed'));+ }+ });if (options.stickmanVisualAssets !== undefined) {server.get('/creator/visual-assets', async (request, reply) => {try {diff --git a/apps/daemon/src/api/server.ts b/apps/daemon/src/api/server.tsindex e869bf54..78656470 100644--- a/apps/daemon/src/api/server.ts+++ b/apps/daemon/src/api/server.ts@@ -49,6 +49,7 @@ import {startKrillinRuntimeVerification} from '../creator/krillin/runtime-verifier.js';import { createKrillinTtsService } from '../creator/krillin/tts-service.js';+import { createSubtitlePreviewService } from '../creator/krillin/subtitle-preview.js';import {createKrillinCodexLlmGateway,KRILLIN_LLM_ROUTE_PREFIX@@ -1461,7 +1462,13 @@ export async function buildServer(input: BuildServerInput) {presets: creatorPresetRegistry,presetCatalogRoot: creatorPresetCatalogRoot,preflight: creatorPreflight,- issueService: creatorIssueService+ issueService: creatorIssueService,+ subtitlePreviewService: createSubtitlePreviewService({+ resourceRoot: creatorRuntimeRoot,+ workRoot: join(creatorJobsRoot, '.previews'),+ ensureRuntimeReady: () => ensureKrillinRuntimeReady(),+ getYtDlpRuntime: () => getCreatorYtDlpRuntime?.()+ })});await registerAttachmentRoutes(server, attachmentService, {maxSizeBytes: input.attachmentMaxSizeBytesdiff --git a/apps/daemon/src/creator/krillin/adapter.ts b/apps/daemon/src/creator/krillin/adapter.tsindex d66264b8..75a14275 100644--- a/apps/daemon/src/creator/krillin/adapter.ts+++ b/apps/daemon/src/creator/krillin/adapter.ts@@ -234,14 +234,32 @@ export function buildKrillinStageOptions(input: CreatorExecutorInput): Record<stprepareVideo: input.job.templateId !== 'video-translation',audioOnly: input.job.templateId === 'video-translation' && state.composeVideo !== true,bilingual: input.stageRun.stageId === 'subtitles' || state.bilingual === true,- bilingualTop: state.subtitlePosition === 'top',+ bilingualTop: state.subtitlePosition !== 'bottom',ttsProvider: typeof state.ttsProvider === 'string' ? state.ttsProvider : undefined,ttsModel: typeof state.ttsModel === 'string' ? state.ttsModel : undefined,voiceCode: typeof state.voiceCode === 'string' ? state.voiceCode : undefined,verticalTitle: typeof state.verticalTitle === 'string' ? state.verticalTitle : undefined,verticalSubtitle: typeof state.verticalSubtitle === 'string' ? state.verticalSubtitle : undefined,dubbed: state.dubbing === true || state.dubbed === true,- subtitleStyle: buildKrillinSubtitleStyle(state.subtitleStyle)+ subtitleStyle: buildKrillinSubtitleStyle(state.subtitleStyle ?? (+ input.job.templateId === 'video-translation' ? compactObject({+ fontPreset: state.subtitleFont ?? 'sans',+ fontWeight: state.subtitleWeight,+ fontSize: state.subtitleSize,+ primaryColor: state.subtitleColor,+ secondaryColor: state.subtitleSecondaryColor,+ outlineColor: state.subtitleOutlineColor,+ outlineWidth: state.subtitleOutlineWidth,+ shadow: compactObject({+ enabled: state.subtitleShadowEnabled,+ color: state.subtitleShadowColor,+ opacity: state.subtitleShadowOpacity,+ offsetX: state.subtitleShadowOffsetX,+ offsetY: state.subtitleShadowOffsetY,+ blur: state.subtitleShadowBlur+ })+ }) as CreatorJson : undefined+ ))});}diff --git a/apps/daemon/src/creator/krillin/cli-runner.ts b/apps/daemon/src/creator/krillin/cli-runner.tsindex 9625b6aa..fd2270da 100644--- a/apps/daemon/src/creator/krillin/cli-runner.ts+++ b/apps/daemon/src/creator/krillin/cli-runner.ts@@ -288,7 +288,7 @@ export function buildKrillinCliCommandArguments(const target = artifactPath(artifacts, 'target_subtitle');const bilingual = artifactPath(artifacts, 'bilingual_subtitle');const subtitle = command === 'render-vertical'- ? vertical ?? target ?? bilingual+ ? vertical ?? (booleanOption(options, 'bilingual', false) ? bilingual ?? target : target ?? bilingual): booleanOption(options, 'bilingual', false) && bilingual? bilingual: target ?? bilingual;@@ -301,6 +301,11 @@ export function buildKrillinCliCommandArguments(...mediaCache,...(video ? ['--video', video] : ['--source-url', sourceUrl!]),'--subtitle', subtitle,+ '--line-mode', subtitle === bilingual || subtitle === vertical+ ? booleanOption(options, 'bilingualTop', true) ? 'bilingual-target-top' : 'bilingual-target-bottom'+ : 'target-only',+ ...(subtitle === vertical && target ? ['--mixed-target-subtitle', target] : []),+ ...(!booleanOption(options, 'bilingual', false) && subtitle !== target ? ['--target-only'] : []),...(dubbed && !dubbedVideo && dubbedAudio ? ['--audio', dubbedAudio] : []),...(dubbed ? ['--dubbed'] : []),...(command === 'render-vertical'diff --git a/apps/daemon/src/creator/krillin/subtitle-preview.ts b/apps/daemon/src/creator/krillin/subtitle-preview.tsnew file mode 100644index 00000000..2153c7dc--- /dev/null+++ b/apps/daemon/src/creator/krillin/subtitle-preview.ts@@ -0,0 +1,76 @@+import type { CreatorSubtitlePreviewRequest } from '@opencreator/protocol';+import { execFile } from 'node:child_process';+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';+import { join } from 'node:path';+import { promisify } from 'node:util';+import type { YtDlpRuntime } from '../yt-dlp/runtime.js';+import { buildKrillinSubtitleStyle } from './adapter.js';+import { createKrillinCliEnvironment } from './cli-runner.js';+import { readKrillinRuntimeManifest, resolveInside } from './manifest.js';++const execute = promisify(execFile);++export function createSubtitlePreviewService(input: {+ resourceRoot: string;+ workRoot: string;+ ensureRuntimeReady(): Promise<void>;+ getYtDlpRuntime?(): YtDlpRuntime | undefined;+}) {+ return {+ async render(request: CreatorSubtitlePreviewRequest, signal?: AbortSignal): Promise<Buffer> {+ await input.ensureRuntimeReady();+ const manifest = readKrillinRuntimeManifest(input.resourceRoot);+ const cli = manifest.resources.find(resource => /(?:^|\/)krillinai-cli(?:\.exe)?$/i.test(resource.path));+ if (cli === undefined) throw new Error('KrillinAI CLI is unavailable');+ await mkdir(input.workRoot, { recursive: true });+ const workdir = await mkdtemp(join(input.workRoot, 'subtitle-preview-'));+ try {+ await mkdir(join(workdir, 'config'));+ await writeFile(join(workdir, 'config/config.toml'), '[app]\nproxy = ""\n', { mode: 0o600 });+ const stylePath = join(workdir, 'style.json');+ const subtitlePath = join(workdir, 'preview.srt');+ const lines = request.bilingual+ ? request.subtitlePosition === 'top'+ ? [request.translationText, request.originalText]+ : [request.originalText, request.translationText]+ : [request.translationText];+ await writeFile(stylePath, JSON.stringify(buildKrillinSubtitleStyle(request.subtitleStyle)));+ await writeFile(subtitlePath, `1\n00:00:00,000 --> 00:00:01,000\n${lines.join('\n')}\n\n`);+ const bin = join(input.resourceRoot, 'bin');+ const ytDlp = input.getYtDlpRuntime?.();+ try {+ await execute(resolveInside(input.resourceRoot, cli.path), [+ request.width >= request.height ? 'render-horizontal' : 'render-vertical',+ '--workdir', workdir, '--subtitle', subtitlePath,+ '--subtitle-style-file', stylePath,+ '--line-mode', request.bilingual ? `bilingual-target-${request.subtitlePosition}` : 'target-only',+ '--preview-width', String(request.width), '--preview-height', String(request.height)+ ], {+ cwd: workdir,+ env: {+ ...createKrillinCliEnvironment(process.env, bin, input.resourceRoot, bin, ytDlp),+ ...(ytDlp !== undefined && ytDlp.prefixArgs.length > 0+ ? { OPENCREATOR_YT_DLP_COMMAND: JSON.stringify([ytDlp.executable, ...ytDlp.prefixArgs]) }+ : {})+ },+ timeout: 15_000,+ maxBuffer: 1024 * 1024,+ windowsHide: true,+ signal+ });+ } catch (cause) {+ const stdout = (cause as { stdout?: string }).stdout;+ let response: { error?: { message?: string } } | undefined;+ try { response = JSON.parse(stdout?.trim().split('\n').at(-1) ?? ''); } catch { /* Keep process failures without JSON diagnostics. */ }+ if (response?.error?.message) throw new Error(response.error.message);+ throw cause;+ }+ return await readFile(join(workdir, 'subtitle-preview.png'));+ } finally {+ await rm(workdir, { recursive: true, force: true });+ }+ }+ };+}++export type SubtitlePreviewService = ReturnType<typeof createSubtitlePreviewService>;diff --git a/apps/daemon/test/integration/creator-krillin-cli-runner.test.ts b/apps/daemon/test/integration/creator-krillin-cli-runner.test.tsindex 3da06efb..115334bd 100644--- a/apps/daemon/test/integration/creator-krillin-cli-runner.test.ts+++ b/apps/daemon/test/integration/creator-krillin-cli-runner.test.ts@@ -591,7 +591,7 @@ describe('KrillinAI CLI runner', () => {'render-vertical','stage-run-vertical-fallback',artifacts.filter(artifact => artifact.kind !== 'vertical_subtitle')- ))).toBe(target);+ ))).toBe(bilingual);expect(selectedSubtitle(await run('render-horizontal', 'stage-run-horizontal'))).toBe(bilingual);});});diff --git a/apps/daemon/test/integration/creator-subtitle-preview-api.test.ts b/apps/daemon/test/integration/creator-subtitle-preview-api.test.tsnew file mode 100644index 00000000..78c2e8c4--- /dev/null+++ b/apps/daemon/test/integration/creator-subtitle-preview-api.test.ts@@ -0,0 +1,29 @@+import Fastify from 'fastify';+import { describe, expect, it, vi } from 'vitest';+import { registerCreatorRoutes } from '../../src/api/routes.creator.js';++describe('subtitle preview API', () => {+ it('validates preview options and returns rendered PNG data without creating a job', async () => {+ const server = Fastify();+ const render = vi.fn(async () => Buffer.from('png-data'));+ await registerCreatorRoutes(server, {} as never, {} as never, {+ jobsRoot: '/unused', dispatcher: {} as never, subtitlePreviewService: { render }+ });+ const input = {+ width: 960, height: 540, bilingual: true, subtitlePosition: 'bottom',+ translationText: 'Translation', originalText: 'Source', subtitleStyle: { fontPreset: 'rounded' }+ };+ const url = (options: unknown) => `/creator/subtitle-preview?${new URLSearchParams({ options: JSON.stringify(options) })}`;+ try {+ const result = await server.inject({ method: 'GET', url: url(input) });+ expect(result.statusCode).toBe(200);+ expect(result.headers['content-type']).toContain('image/png');+ expect(result.body).toBe('png-data');+ expect(render).toHaveBeenCalledWith(expect.objectContaining({ ...input, subtitleStyle: expect.objectContaining(input.subtitleStyle) }), expect.any(AbortSignal));+ for (const invalid of [{ ...input, width: 9000 }, { ...input, translationText: '{\\rMinor}' }, { ...input, subtitleStyle: { rawAssStyle: 'arbitrary' } }]) {+ expect((await server.inject({ method: 'GET', url: url(invalid) })).statusCode).toBe(400);+ }+ expect(render).toHaveBeenCalledOnce();+ } finally { await server.close(); }+ });+});diff --git a/apps/daemon/test/integration/creator-subtitle-render.test.ts b/apps/daemon/test/integration/creator-subtitle-render.test.tsindex b52ef64f..385ee705 100644--- a/apps/daemon/test/integration/creator-subtitle-render.test.ts+++ b/apps/daemon/test/integration/creator-subtitle-render.test.ts@@ -14,6 +14,10 @@ import { tmpdir } from 'node:os';import { delimiter, dirname, join, resolve } from 'node:path';import { afterAll, describe, expect, it } from 'vitest';import { buildKrillinSubtitleStyle } from '../../src/creator/krillin/adapter.js';+import { createSubtitlePreviewService } from '../../src/creator/krillin/subtitle-preview.js';+import { readKrillinRuntimeManifest } from '../../src/creator/krillin/manifest.js';+import { resolveYtDlpRuntime } from '../../src/creator/yt-dlp/runtime.js';+import sharp from 'sharp';const repoRoot = resolve(process.cwd(), '../..');const runtimeRoot = resolve(@@ -32,6 +36,53 @@ afterAll(() => {});describe.runIf(hasPackagedRuntime)('Creator packaged subtitle rendering', () => {+ it.each([+ ['sans', 640, 360, 'top', false],+ ['serif', 640, 360, 'bottom', false],+ ['rounded', 360, 640, 'bottom', false],+ ['sans', 360, 640, 'bottom', true]+ ] as const)('uses the same rendered glyphs for %s preview and %dx%d burn-in with translation on %s (legacy mixed: %s)', async (fontPreset, width, height, position, mixed) => {+ const style = {+ fontPreset, fontWeight: 'bold', fontSize: 'medium',+ primaryColor: '#7EE7FF', secondaryColor: '#FFD45C', outlineColor: '#102030', outlineWidth: 2,+ shadow: { enabled: true, color: '#000000', opacity: 0.6, offsetX: -4, offsetY: 6, blur: 2 }+ } as const;+ const result = renderStyle(style, { width, height, position, mixed });+ const service = createSubtitlePreviewService({+ resourceRoot: runtimeRoot, workRoot: result.paths.workdir,+ ensureRuntimeReady: async () => {},+ getYtDlpRuntime: () => resolveYtDlpRuntime({ resourceRoot: runtimeRoot, manifest: readKrillinRuntimeManifest(runtimeRoot) })+ });+ const png = await service.render({ width, height, subtitleStyle: style, bilingual: true, subtitlePosition: position, translationText: 'Creator subtitle test', originalText: '创作字幕测试' });+ const rgba = await sharp(png).ensureAlpha().raw().toBuffer();+ const assPath = result.assPath;+ const frame = run(ffmpegPath, [+ '-v', 'error', '-f', 'lavfi', '-i', `color=c=0x202020:s=${width}x${height}:d=2,format=rgba`, '-ss', '0.5',+ '-vf', `ass=filename='${assPath}':fontsdir='${join(runtimeRoot, 'fonts')}'`,+ '-frames:v', '1', '-f', 'rawvideo', '-pix_fmt', 'rgb24', 'pipe:1'+ ], null);+ let changedPixels = 0;+ let error = 0;+ for (let pixel = 0; pixel < width * height; pixel += 1) {+ const alpha = rgba[pixel * 4 + 3]! / 255;+ if (alpha === 0 && frame[pixel * 3] === 32 && frame[pixel * 3 + 1] === 32 && frame[pixel * 3 + 2] === 32) continue;+ changedPixels += 1;+ for (let channel = 0; channel < 3; channel += 1) {+ const composite = Math.round(rgba[pixel * 4 + channel]! * alpha + 32 * (1 - alpha));+ error += Math.abs(composite - frame[pixel * 3 + channel]!);+ }+ }+ expect(changedPixels).toBeGreaterThan(100);+ expect(rgba[3]).toBe(0);+ expect(error / (changedPixels * 3)).toBeLessThan(2);+ });++ it('applies directional shadow overrides to actual pixels', () => {+ const style = { shadow: { enabled: true, opacity: 0.6, offsetX: -6, offsetY: 6, blur: 0 } };+ const first = renderStyle(style);+ const second = renderStyle({ shadow: { ...style.shadow, offsetX: 6, offsetY: -6 } });+ expect(first.frame.sha256).not.toBe(second.frame.sha256);+ });it('renders visibly different fixed frames and fails when a packaged font is missing', () => {const first = renderStyle({fontPreset: 'sans',@@ -88,7 +139,7 @@ describe.runIf(hasPackagedRuntime)('Creator packaged subtitle rendering', () =>});});-function renderStyle(style: Parameters<typeof buildKrillinSubtitleStyle>[0]) {+function renderStyle(style: Parameters<typeof buildKrillinSubtitleStyle>[0], options: { width: number; height: number; position: string; mixed?: boolean } = { width: 640, height: 360, position: 'top' }) {const root = mkdtempSync(join(tmpdir(), 'opencreator-subtitle-render-'));roots.push(root);const launcher = join(root, 'launcher');@@ -99,28 +150,38 @@ function renderStyle(style: Parameters<typeof buildKrillinSubtitleStyle>[0]) {const inputVideo = join(workdir, 'background.mp4');const subtitle = join(workdir, 'bilingual.srt');const stylePath = join(workdir, 'subtitle-style.json');+ const targetPath = join(workdir, 'target.srt');+ const timeline = '00:00:00,200 --> 00:00:01,800';+ writeFileSync(targetPath, `1\n${timeline}\nCreator subtitle test\n\n`);writeFileSync(subtitle,- '1\n00:00:00,200 --> 00:00:01,800\nCreator subtitle test\n创作字幕测试\n\n'+ options.mixed+ ? `1\n${timeline}\nCreator subtitle test\n\n2\n${timeline}\n创作字幕测试\n\n`+ : `1\n${timeline}\n${(options.position === 'top' ? ['Creator subtitle test', '创作字幕测试'] : ['创作字幕测试', 'Creator subtitle test']).join('\n')}\n\n`);writeFileSync(stylePath, `${JSON.stringify(buildKrillinSubtitleStyle(style), null, 2)}\n`);run(ffmpegPath, ['-v', 'error','-y','-f', 'lavfi',- '-i', 'color=c=#202020:s=640x360:d=2:r=25',+ '-i', `color=c=#202020:s=${options.width}x${options.height}:d=2:r=25`,'-c:v', 'libx264','-pix_fmt', 'yuv420p',inputVideo]);- const result = runCli({ launcher, workdir, inputVideo, subtitle }, stylePath, runtimeRoot);+ const horizontal = options.width >= options.height;+ const result = runCli({ launcher, workdir, inputVideo, subtitle }, stylePath, runtimeRoot, [+ `--line-mode=bilingual-target-${options.position}`,+ ...(options.mixed ? ['--mixed-target-subtitle', targetPath] : [])+ ], horizontal ? 'render-horizontal' : 'render-vertical');expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);- const outputVideo = join(workdir, 'horizontal_bilingual.mp4');- const assPath = join(workdir, 'formatted_horizontal_bilingual.ass');+ const outputVideo = join(workdir, `${horizontal ? 'horizontal' : 'vertical'}_bilingual.mp4`);+ const assPath = join(workdir, `formatted_${horizontal ? 'horizontal' : 'vertical'}_bilingual.ass`);expect(existsSync(outputVideo)).toBe(true);expect(existsSync(assPath)).toBe(true);return {ass: readFileSync(assPath, 'utf8'),+ assPath,frame: compareFramePixels(inputVideo, outputVideo),paths: { launcher, workdir, inputVideo, subtitle },stylePath@@ -130,7 +191,9 @@ function renderStyle(style: Parameters<typeof buildKrillinSubtitleStyle>[0]) {function runCli(paths: { launcher: string; workdir: string; inputVideo: string; subtitle: string },stylePath: string,- resourceRoot: string+ resourceRoot: string,+ extraArgs: string[] = [],+ command = 'render-horizontal') {const manifest = JSON.parse(readFileSync(join(runtimeRoot, 'manifest.json'), 'utf8'));const env = {@@ -150,12 +213,13 @@ function runCli(: {})};return spawnSync(cliPath, [- 'render-horizontal',+ command,'--workdir', paths.workdir,'--task-id', 'subtitle-render-test','--video', paths.inputVideo,'--subtitle', paths.subtitle,- '--subtitle-style-file', stylePath+ '--subtitle-style-file', stylePath,+ ...extraArgs], {cwd: paths.launcher,env,diff --git a/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts b/apps/daemon/test/unit/creator-krillin-cli-protocol.test.tsindex 1900dc63..454b7586 100644--- a/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts+++ b/apps/daemon/test/unit/creator-krillin-cli-protocol.test.ts@@ -13,6 +13,34 @@ import { createKrillinConfigToml } from '../../src/creator/krillin/config-bridgeimport { buildKrillinStageOptions } from '../../src/creator/krillin/adapter.js';describe('KrillinAI CLI protocol', () => {+ it.each(['top', 'bottom'])('passes semantic subtitle ordering to the render command with translation on %s', subtitlePosition => {+ const stage = { job: { templateId: 'video-translation', state: { bilingual: true, subtitlePosition } }, stageRun: { stageId: 'render-horizontal', id: 'render' }, workdir: '/job/render' };+ const args = buildKrillinCliCommandArguments(stage as never, [+ { id: 'source', kind: 'source_video', path: '/job/video.mp4' },+ { id: 'target', kind: 'target_subtitle', path: '/job/target.srt' },+ { id: 'bilingual', kind: 'bilingual_subtitle', path: '/job/bilingual.srt' }+ ], buildKrillinStageOptions(stage as never), '/job/style.json');+ expect(args[args.indexOf('--line-mode') + 1]).toBe(`bilingual-target-${subtitlePosition}`);+ expect(args).toContain('--subtitle-style-file');+ });++ it('passes saved legacy styles instead of silently using runtime defaults', () => {+ const stage = { job: { templateId: 'video-translation', state: { subtitleFont: 'rounded', subtitleSize: 'large', subtitleColor: '#123456', subtitleShadowOffsetX: -4, subtitleShadowOffsetY: 6, subtitleShadowBlur: 2 } }, stageRun: { stageId: 'render-horizontal' } };+ expect(buildKrillinStageOptions(stage as never).subtitleStyle).toMatchObject({ horizontal: { major: { font_name: 'OpenCreator Rounded Bold', font_size: 18, primary_color: '#123456', override_tags: '\\xshad-4\\yshad6\\blur2' } } });+ });++ it.each([true, false])('keeps edited vertical subtitles and renders the requested bilingual=%s mode', bilingual => {+ const stage = { job: { templateId: 'video-translation', state: { bilingual, subtitlePosition: 'bottom' } }, stageRun: { stageId: 'render-vertical', id: 'render' }, workdir: '/job/render' };+ const args = buildKrillinCliCommandArguments(stage as never, [+ { id: 'source', kind: 'source_video', path: '/job/video.mp4' },+ { id: 'target', kind: 'target_subtitle', path: '/job/target.srt' },+ { id: 'vertical', kind: 'vertical_subtitle', path: '/job/edited-vertical.srt' }+ ], buildKrillinStageOptions(stage as never), undefined);+ expect(args[args.indexOf('--subtitle') + 1]).toBe('/job/edited-vertical.srt');+ expect(args[args.indexOf('--mixed-target-subtitle') + 1]).toBe('/job/target.srt');+ expect(args[args.indexOf('--line-mode') + 1]).toBe('bilingual-target-bottom');+ expect(args.includes('--target-only')).toBe(!bilingual);+ });it.each([true, false])('never prepares video in the translation subtitle stage, even when composeVideo=%s', composeVideo => {const stage = { job: { templateId: 'video-translation', state: { sourceUrl: 'https://youtu.be/demo', sourceLanguage: 'en', targetLanguage: 'zh_cn', composeVideo } }, stageRun: { stageId: 'subtitle', id: 'stage' }, workdir: '/job/stage' };const options = buildKrillinStageOptions(stage as never);diff --git a/apps/web/e2e/video-translation-style-preview.spec.ts b/apps/web/e2e/video-translation-style-preview.spec.tsindex a00b2c1b..33bc391e 100644--- a/apps/web/e2e/video-translation-style-preview.spec.ts+++ b/apps/web/e2e/video-translation-style-preview.spec.ts@@ -6,7 +6,7 @@ const poster = Buffer.from('base64');-test('字幕样式预览显示视频封面并按源比例或竖屏输出比例排版', async ({ page, runtime }) => {+test('字幕样式预览显示视频封面并按源比例或竖屏输出比例排版', async ({ page, runtime }, testInfo) => {await page.route('**/video-metadata?*', route => route.fulfill({json: {platform: 'bilibili',@@ -42,14 +42,38 @@ test('字幕样式预览显示视频封面并按源比例或竖屏输出比例const image = preview.locator('img.video-translation-subtitle-preview-media');const frame = preview.locator(':scope > div');const settings = page.locator('.video-translation-wizard-main .video-translation-wizard-body');+ const subtitles = preview.locator('img.video-translation-subtitle-rendered-preview');await expect(preview).toHaveAttribute('data-ratio', '4:3');await expect(image).toBeVisible();await expect.poll(() => image.evaluate(element => (element as HTMLImageElement).naturalWidth)).toBeGreaterThan(0);+ await expect(subtitles).toBeVisible();+ await expect.poll(() => subtitles.evaluate(element => {+ const img = element as HTMLImageElement;+ if (!img.complete || img.naturalWidth === 0) return 0;+ const canvas = document.createElement('canvas');+ canvas.width = img.naturalWidth;+ canvas.height = img.naturalHeight;+ const ctx = canvas.getContext('2d')!;+ ctx.drawImage(img, 0, 0);+ const rgba = ctx.getImageData(0, 0, canvas.width, canvas.height).data;+ let painted = 0;+ for (let offset = 3; offset < rgba.length; offset += 4) if (rgba[offset]! > 0) painted++;+ return painted;+ })).toBeGreaterThan(100);+ const initialSource = await subtitles.getAttribute('src');+ await page.getByRole('combobox', { name: '字幕字体' }).selectOption('serif');+ await expect.poll(() => subtitles.getAttribute('src')).not.toBe(initialSource);+ await expect.poll(() => subtitles.evaluate(element => (element as HTMLImageElement).naturalWidth)).toBe(960);+ await expect.poll(() => subtitles.evaluate(element => (element as HTMLImageElement).naturalHeight)).toBe(720);+ const initialBox = await frame.boundingBox();+ expect(initialBox!.width).toBeLessThanOrEqual(page.viewportSize()!.width);+ await preview.screenshot({ path: testInfo.outputPath('subtitle-style-preview.png') });+ await page.screenshot({ path: testInfo.outputPath('subtitle-style-source.png'), fullPage: true });await page.setViewportSize({ width: 1920, height: 1000 });await expect.poll(() => settings.evaluate(element => element.getBoundingClientRect().width)).toBeGreaterThan(720);expect(await frame.evaluate(element => {const overlay = getComputedStyle(element, '::after');- const cues = element.querySelector('.video-translation-subtitle-preview-cues');+ const cues = element.querySelector('.video-translation-subtitle-rendered-preview, .video-translation-subtitle-preview-status');return {background: overlay.backgroundColor,overlayLayer: overlay.zIndex,@@ -72,7 +96,11 @@ test('字幕样式预览显示视频封面并按源比例或竖屏输出比例await page.getByRole('navigation', { name: '翻译流程' }).getByRole('button', { name: /字幕样式$/ }).click();await expect(preview).toHaveAttribute('data-ratio', '9:16');await expect(preview).toHaveAttribute('data-converted', 'true');+ await expect(subtitles).toBeVisible();+ await expect.poll(() => subtitles.evaluate(element => (element as HTMLImageElement).naturalWidth)).toBe(540);+ await expect.poll(() => subtitles.evaluate(element => (element as HTMLImageElement).naturalHeight)).toBe(960);const verticalBox = await frame.boundingBox();expect(verticalBox).not.toBeNull();expect(verticalBox!.width / verticalBox!.height).toBeCloseTo(9 / 16, 1);+ await page.screenshot({ path: testInfo.outputPath('subtitle-style-vertical.png'), fullPage: true });});diff --git a/apps/web/src/features/dashboard/VideoTranslationStylePreview.test.tsx b/apps/web/src/features/dashboard/VideoTranslationStylePreview.test.tsxnew file mode 100644index 00000000..f1098237--- /dev/null+++ b/apps/web/src/features/dashboard/VideoTranslationStylePreview.test.tsx@@ -0,0 +1,55 @@+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import type { CreatorSubtitlePreviewRequest } from '@opencreator/protocol';+import { LanguageProvider } from '../../i18n/LanguageProvider.js';+import VideoTranslationStylePreview from './VideoTranslationStylePreview.js';++const input: CreatorSubtitlePreviewRequest = {+ width: 960, height: 540, bilingual: true, subtitlePosition: 'bottom',+ subtitleStyle: { fontPreset: 'serif', primaryColor: '#123456' },+ translationText: 'Translation', originalText: 'Original'+};+const createObjectURL = vi.fn(() => 'blob:rendered-subtitles');+const revokeObjectURL = vi.fn();+beforeEach(() => {+ vi.useFakeTimers();+ class PreviewURL extends URL {+ static createObjectURL = createObjectURL;+ static revokeObjectURL = revokeObjectURL;+ }+ vi.stubGlobal('URL', PreviewURL);+ createObjectURL.mockClear();+ revokeObjectURL.mockClear();+});+afterEach(() => { cleanup(); vi.useRealTimers(); vi.unstubAllGlobals(); });+const png = () => new Response(new Blob(['png'], { type: 'image/png' }), { headers: { 'content-type': 'image/png' } });+const tick = () => act(async () => { await vi.advanceTimersByTimeAsync(201); });++describe('rendered subtitle style preview', () => {+ it('renders the returned image and cancels outdated requests when style changes', async () => {+ const renderPreview = vi.fn(async () => png());+ const view = render(<LanguageProvider initialPreference="en-US"><VideoTranslationStylePreview input={input} render={renderPreview} /></LanguageProvider>);+ await tick();+ expect(renderPreview).toHaveBeenCalledExactlyOnceWith(input, { signal: expect.any(AbortSignal) });+ expect(screen.getByRole('img')).toHaveAttribute('src', 'blob:rendered-subtitles');+ const signal = (renderPreview.mock.calls[0] as unknown as [unknown, { signal: AbortSignal }])[1].signal;+ const updated = { ...input, subtitleStyle: { ...input.subtitleStyle, fontPreset: 'rounded' } };+ view.rerender(<LanguageProvider initialPreference="en-US"><VideoTranslationStylePreview input={updated} render={renderPreview} /></LanguageProvider>);+ expect(signal.aborted).toBe(true);+ expect(screen.queryByRole('img')).not.toBeInTheDocument();+ expect(revokeObjectURL).toHaveBeenCalledWith('blob:rendered-subtitles');+ await tick();+ expect(renderPreview).toHaveBeenLastCalledWith(updated, { signal: expect.any(AbortSignal) });+ });++ it('offers a working retry after rendering fails', async () => {+ const renderPreview = vi.fn().mockRejectedValueOnce(new Error('unavailable')).mockResolvedValueOnce(png());+ render(<LanguageProvider initialPreference="en-US"><VideoTranslationStylePreview input={input} render={renderPreview} /></LanguageProvider>);+ await tick();+ expect(screen.getByText('Subtitle preview failed')).toBeVisible();+ fireEvent.click(screen.getByRole('button', { name: 'Retry subtitle preview' }));+ await tick();+ expect(screen.getByRole('img')).toBeVisible();+ expect(renderPreview).toHaveBeenCalledTimes(2);+ });+});diff --git a/apps/web/src/features/dashboard/VideoTranslationStylePreview.tsx b/apps/web/src/features/dashboard/VideoTranslationStylePreview.tsxnew file mode 100644index 00000000..394813dc--- /dev/null+++ b/apps/web/src/features/dashboard/VideoTranslationStylePreview.tsx@@ -0,0 +1,49 @@+import { useEffect, useState } from 'react';+import { RefreshCw } from 'lucide-react';+import type { CreatorSubtitlePreviewRequest } from '@opencreator/protocol';+import type { CreatorWebService } from '../../services/creator-service.js';+import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';++export default function VideoTranslationStylePreview(props: {+ input: CreatorSubtitlePreviewRequest;+ render?: CreatorWebService['openSubtitlePreview'];+}) {+ const l = useLocalizedCopy();+ const key = JSON.stringify(props.input);+ const [retry, setRetry] = useState(0);+ const [preview, setPreview] = useState<{ key: string; url?: string; failed?: boolean }>();+ useEffect(() => {+ const controller = new AbortController();+ let objectUrl: string | undefined;+ const timer = setTimeout(() => {+ void (async () => {+ try {+ if (props.render === undefined) throw new Error('Subtitle renderer is unavailable');+ const response = await props.render(JSON.parse(key) as CreatorSubtitlePreviewRequest, { signal: controller.signal });+ if (!response.ok || !response.headers.get('content-type')?.startsWith('image/png')) {+ throw new Error('Invalid subtitle preview');+ }+ const blob = await response.blob();+ if (controller.signal.aborted) return;+ objectUrl = URL.createObjectURL(blob);+ setPreview({ key, url: objectUrl });+ } catch {+ if (!controller.signal.aborted) setPreview({ key, failed: true });+ }+ })();+ }, 200);+ return () => {+ clearTimeout(timer);+ controller.abort();+ if (objectUrl !== undefined) URL.revokeObjectURL(objectUrl);+ };+ }, [key, props.render, retry]);+ const current = preview?.key === key ? preview : undefined;+ if (current?.url !== undefined) {+ return <img className="video-translation-subtitle-rendered-preview" src={current.url} alt={l('字幕样式预览', 'Subtitle style preview')} />;+ }+ return <div className="video-translation-subtitle-preview-status" role="status">+ <span>{current?.failed ? l('字幕预览加载失败', 'Subtitle preview failed') : l('正在生成字幕预览...', 'Rendering subtitle preview...')}</span>+ {current?.failed ? <button type="button" title={l('重试', 'Retry')} aria-label={l('重试字幕预览', 'Retry subtitle preview')} onClick={() => { setPreview(undefined); setRetry(value => value + 1); }}><RefreshCw size={16} /></button> : null}+ </div>;+}diff --git a/apps/web/src/features/dashboard/VideoTranslationWorkspace.control.test.tsx b/apps/web/src/features/dashboard/VideoTranslationWorkspace.control.test.tsxindex 63e03a72..32521384 100644--- a/apps/web/src/features/dashboard/VideoTranslationWorkspace.control.test.tsx+++ b/apps/web/src/features/dashboard/VideoTranslationWorkspace.control.test.tsx@@ -471,12 +471,12 @@ describe('VideoTranslationWorkspace task controls', () => {}});- it('edits every subtitle style field and persists one structured patch', async () => {+ it.each([1, 2])('edits every subtitle style field and persists one structured patch for template v%s', async templateVersion => {let currentJob = job({status: 'draft',revision: 0,stages: [],- templateVersion: 2,+ templateVersion,state: {currentStep: 2,furthestStep: 2,diff --git a/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx b/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsxindex becdaf7a..41e39beb 100644--- a/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx+++ b/apps/web/src/features/dashboard/VideoTranslationWorkspace.tsx@@ -33,6 +33,7 @@ import VideoSourceInput from './VideoSourceInput.js';import { BilibiliPartSelector } from './BilibiliPartSelector.js';import { parseVideoSource } from './VideoSourcePreview.js';import { VideoTranslationSubtitleImport } from './VideoTranslationSubtitleImport.js';+import VideoTranslationStylePreview from './VideoTranslationStylePreview.js';import VideoTranslationResultWorkspace, {type SubtitleCue,type SubtitleResultOutput,@@ -336,15 +337,6 @@ function subtitleWeightLabel(value: SubtitleWeight, l: LocalizeCopy) {} as const)[value];}-function subtitleFontFamily(value: SubtitleFont, weight: SubtitleWeight) {- const suffix = ({ regular: 'Regular', medium: 'Medium', bold: 'Bold' } as const)[weight];- if (value === 'serif') return `"OpenCreator Serif ${suffix}", Georgia, serif`;- if (value === 'rounded') {- return `"OpenCreator Rounded ${suffix}", "OpenCreator Sans ${suffix}", system-ui, sans-serif`;- }- return `"OpenCreator Sans ${suffix}", system-ui, sans-serif`;-}-function localSourceFingerprint(source: TranslationSourceSnapshot) {return {name: source.videoFile?.name ?? source.videoFileName,@@ -1798,9 +1790,7 @@ export default function VideoTranslationWorkspace(props: {bilingual,subtitlePosition,preferPlatformCaptions,- ...(creatorSession.job.templateVersion >= 2- ? { subtitleStyle }- : { subtitleFont, subtitleSize, subtitleColor }),+ subtitleStyle,dubbing,ttsProvider,ttsModel,@@ -3377,16 +3367,6 @@ export default function VideoTranslationWorkspace(props: {<divstyle={{'--subtitle-preview-aspect-ratio': `${previewWidth} / ${previewHeight}`,- '--subtitle-preview-color': subtitleColor,- '--subtitle-preview-secondary-color': subtitleSecondaryColor,- '--subtitle-preview-outline-color': subtitleOutlineColor,- '--subtitle-preview-outline-width': `${subtitleOutlineWidth}px`,- '--subtitle-preview-shadow': subtitleShadowEnabled- ? `${subtitleShadowOffsetX}px ${subtitleShadowOffsetY}px ${subtitleShadowBlur}px color-mix(in srgb, ${subtitleShadowColor} ${Math.round(subtitleShadowOpacity * 100)}%, transparent)`- : 'none',- '--subtitle-preview-font-size': ({ small: '14px', medium: '16px', large: '18px' } as const)[subtitleSize],- '--subtitle-preview-font-family': subtitleFontFamily(subtitleFont, subtitleWeight),- '--subtitle-preview-font-weight': ({ regular: 400, medium: 500, bold: 700 } as const)[subtitleWeight]} as CSSProperties}>{previewPosterUrl ? (@@ -3401,15 +3381,34 @@ export default function VideoTranslationWorkspace(props: {{previewVideoSrc ? (<SubtitlePreviewVideo key={previewVideoSrc} src={previewVideoSrc} onDimensions={updateSourceOrientation} />) : null}- <div className="video-translation-subtitle-preview-cues">- {bilingual && subtitlePosition === 'bottom' ? (- <small data-subtitle-kind="original">{l('这是一段原文字幕', 'This is an original subtitle')}</small>- ) : null}- <strong data-subtitle-kind="translation">{l('这是一段译文字幕', 'This is a translated subtitle')}</strong>- {bilingual && subtitlePosition === 'top' ? (- <small data-subtitle-kind="original">{l('这是一段原文字幕', 'This is an original subtitle')}</small>- ) : null}- </div>+ <VideoTranslationStylePreview+ render={creatorSession?.openSubtitlePreview}+ input={{+ width: Math.max(32, Math.round(960 * previewWidth / Math.max(previewWidth, previewHeight))),+ height: Math.max(32, Math.round(960 * previewHeight / Math.max(previewWidth, previewHeight))),+ bilingual,+ subtitlePosition,+ translationText: l('这是一段译文字幕', 'This is a translated subtitle'),+ originalText: l('这是一段原文字幕', 'This is an original subtitle'),+ subtitleStyle: {+ fontPreset: subtitleFont,+ fontWeight: subtitleWeight,+ fontSize: subtitleSize,+ primaryColor: subtitleColor,+ secondaryColor: subtitleSecondaryColor,+ outlineColor: subtitleOutlineColor,+ outlineWidth: subtitleOutlineWidth,+ shadow: {+ enabled: subtitleShadowEnabled,+ color: subtitleShadowColor,+ opacity: subtitleShadowOpacity,+ offsetX: subtitleShadowOffsetX,+ offsetY: subtitleShadowOffsetY,+ blur: subtitleShadowBlur+ }+ }+ }}+ /></div></div></div>diff --git a/apps/web/src/features/dashboard/creator-session-store.tsx b/apps/web/src/features/dashboard/creator-session-store.tsxindex cc0f97af..e8bd483a 100644--- a/apps/web/src/features/dashboard/creator-session-store.tsx+++ b/apps/web/src/features/dashboard/creator-session-store.tsx@@ -69,6 +69,7 @@ export type CreatorSessionContextValue = {uploadArticleImage(file: File): Promise<CreatorArtifact>;uploadSourceDocument(file: File): Promise<void>;openArtifact(artifactId: string): Promise<Response>;+ openSubtitlePreview?: CreatorWebService['openSubtitlePreview'];openArtifactJson<T = unknown>(artifactId: string): Promise<T>;agentSession: CreatorAgentSession | null;turns: CreatorAgentTurn[];@@ -159,6 +160,7 @@ export function CreatorSessionProvider(props: {| 'listIssues'| 'reportClientIssue'| 'openArtifact'+ | 'openSubtitlePreview'| 'uploadReferenceImage'| 'uploadArticleImage'| 'uploadSourceVideo'@@ -985,6 +987,7 @@ export function CreatorSessionProvider(props: {uploadSourceVideo,uploadArticleImage,openArtifact,+ openSubtitlePreview: props.service.openSubtitlePreview,openArtifactJson,agentSession,turns,@@ -995,7 +998,7 @@ export function CreatorSessionProvider(props: {steerAgentTurn,interruptAgentTurn,respondAgentApproval- }), [connection, reconnect, agentBusy, agentSession, askPendingIssue, applyAction, applyRemoteSnapshot, approvals, cancelJob, captureCreatorFailure, clearError, confirmedJob, conflictedFields, draft, error, flush, focusIssue, focusedIssue, interruptAgentTurn, issues, items, openArtifact, openArtifactJson, preflight, repairIssue, respondAgentApproval, resumeJob, runAgentTurn, runPreflight, steerAgentTurn, turns, updateDraft, uploadArticleImage, uploadReferenceImage, uploadSourceDocument, uploadSourceVideo]);+ }), [connection, reconnect, agentBusy, agentSession, askPendingIssue, applyAction, applyRemoteSnapshot, approvals, cancelJob, captureCreatorFailure, clearError, confirmedJob, conflictedFields, draft, error, flush, focusIssue, focusedIssue, interruptAgentTurn, issues, items, openArtifact, openArtifactJson, preflight, props.service.openSubtitlePreview, repairIssue, respondAgentApproval, resumeJob, runAgentTurn, runPreflight, steerAgentTurn, turns, updateDraft, uploadArticleImage, uploadReferenceImage, uploadSourceDocument, uploadSourceVideo]);return (<CreatorSessionContext.Provider value={value}>diff --git a/apps/web/src/features/dashboard/dashboard-css.test.ts b/apps/web/src/features/dashboard/dashboard-css.test.tsindex 4d466a79..50229e91 100644--- a/apps/web/src/features/dashboard/dashboard-css.test.ts+++ b/apps/web/src/features/dashboard/dashboard-css.test.ts@@ -132,7 +132,7 @@ describe('dashboard CSS contracts', () => {const player = cssBlocks('.video-translation-subtitle-preview > div');const overlay = cssBlocks('.video-translation-subtitle-preview > div::after');const media = cssBlocks('.video-translation-subtitle-preview-media');- const cues = cssBlocks('.video-translation-subtitle-preview-cues');+ const cues = cssBlocks('.video-translation-subtitle-rendered-preview,\n.video-translation-subtitle-preview-status');const verticalPreview = cssBlocks('.video-translation-subtitle-preview[data-orientation="portrait"] > div');expect(layout[0]).toContain('align-items: start;');diff --git a/apps/web/src/features/dashboard/dashboard.css b/apps/web/src/features/dashboard/dashboard.cssindex c0372a0d..8dbbfdd4 100644--- a/apps/web/src/features/dashboard/dashboard.css+++ b/apps/web/src/features/dashboard/dashboard.css@@ -1275,17 +1275,35 @@object-fit: contain;}-.video-translation-subtitle-preview-cues {+.video-translation-subtitle-rendered-preview,+.video-translation-subtitle-preview-status {position: absolute;z-index: 2;inset: 0;+ width: 100%;+ height: 100%;+ object-fit: contain;+}++.video-translation-subtitle-preview-status {display: flex;- flex-direction: column;align-items: center;- justify-content: flex-end;- gap: 7px;- padding: 28px 24px;- box-sizing: border-box;+ justify-content: center;+ gap: 8px;+ color: #ffffff;+ font-size: 12px;+}++.video-translation-subtitle-preview-status button {+ display: grid;+ place-items: center;+ width: 28px;+ height: 28px;+ border: 1px solid #69717d;+ border-radius: 4px;+ background: #303640;+ color: #ffffff;+ cursor: pointer;}.video-translation-subtitle-preview[data-orientation="portrait"] > div {@@ -1293,28 +1311,6 @@justify-self: center;}-.video-translation-subtitle-preview small,-.video-translation-subtitle-preview strong {- max-width: 100%;- line-height: 1.35;- overflow-wrap: anywhere;- -webkit-text-stroke: var(--subtitle-preview-outline-width) var(--subtitle-preview-outline-color);- paint-order: stroke fill;- text-shadow: var(--subtitle-preview-shadow);-}--.video-translation-subtitle-preview small {- color: var(--subtitle-preview-secondary-color);- font-size: 13px;- font-weight: var(--subtitle-preview-font-weight);-}--.video-translation-subtitle-preview strong {- color: var(--subtitle-preview-color);- font-size: var(--subtitle-preview-font-size);- font-weight: var(--subtitle-preview-font-weight);-}-.video-translation-summary {position: sticky;top: 24px;diff --git a/apps/web/src/services/creator-service.ts b/apps/web/src/services/creator-service.tsindex 7e3c1ace..2141c649 100644--- a/apps/web/src/services/creator-service.ts+++ b/apps/web/src/services/creator-service.ts@@ -20,6 +20,7 @@ import type {CreatorPresetListResponse,CreatorPreflightResponse,CreatorStageRun,+ CreatorSubtitlePreviewRequest,CreatorSourceUploadResponse,CreatorTemplateListResponse,CreatorVisualAssetCatalogResponse,@@ -51,6 +52,10 @@ export function createCreatorService(client: ClientLike) {const lastEventIdByJob = new Map<string, string>();const consumedEventIds = new Map<string, Set<string>>();return {+ openSubtitlePreview(input: CreatorSubtitlePreviewRequest, options?: { signal?: AbortSignal }): Promise<Response> {+ if (client.rawGet === undefined) throw new Error('Subtitle preview transport is unavailable');+ return client.rawGet(`/creator/subtitle-preview?${new URLSearchParams({ options: JSON.stringify(input) })}`, options);+ },listTemplates(): Promise<CreatorTemplateListResponse> {return client.get('/creator/templates') as Promise<CreatorTemplateListResponse>;},diff --git a/packages/protocol/src/creator.ts b/packages/protocol/src/creator.tsindex e498015f..62af0727 100644--- a/packages/protocol/src/creator.ts+++ b/packages/protocol/src/creator.ts@@ -761,3 +761,12 @@ function isCreatorJsonValue(value: unknown): value is CreatorJson {return isUnknownRecord(value)&& Object.values(value).every(isCreatorJsonValue);}+export type CreatorSubtitlePreviewRequest = {+ width: number;+ height: number;+ subtitleStyle: Record<string, CreatorJson>;+ bilingual: boolean;+ subtitlePosition: 'top' | 'bottom';+ translationText: string;+ originalText: string;+};diff --git a/runtime/krillinai/internal/cli/commands.go b/runtime/krillinai/internal/cli/commands.goindex 27c02416..9d0e9583 100644--- a/runtime/krillinai/internal/cli/commands.go+++ b/runtime/krillinai/internal/cli/commands.go@@ -175,6 +175,10 @@ Flags:--audio <file> Optional input audio--subtitle <file> Subtitle file to burn in--subtitle-style-file <file> JSON subtitle style override file+ --line-mode <mode> target-only, bilingual-target-top, or bilingual-target-bottom+ --preview-width <px> --preview-height <px> Render a transparent subtitle PNG+ --target-only Render only the translation from bilingual input+ --mixed-target-subtitle <file> Target timeline for legacy mixed vertical SRT--dubbed Render dubbed variant--dry-run Validate command without external calls-h, --help Show this help@@ -190,6 +194,10 @@ Flags:--audio <file> Optional input audio--subtitle <file> Subtitle file to burn in--subtitle-style-file <file> JSON subtitle style override file+ --line-mode <mode> target-only, bilingual-target-top, or bilingual-target-bottom+ --preview-width <px> --preview-height <px> Render a transparent subtitle PNG+ --target-only Render only the translation from bilingual input+ --mixed-target-subtitle <file> Target timeline for legacy mixed vertical SRT--dubbed Render dubbed variant--major-title <text> Vertical video major title--minor-title <text> Vertical video minor title@@ -594,26 +602,42 @@ func parseRender(name string, args []string, horizontal bool) (Command, error) {majorTitle := fs.String("major-title", "", "vertical major title")minorTitle := fs.String("minor-title", "", "vertical minor title")subtitleStyleFile := fs.String("subtitle-style-file", "", "subtitle style JSON file")+ lineMode := fs.String("line-mode", string(pipeline.LineModeTargetOnly), "subtitle language ordering")+ previewWidth := fs.Int("preview-width", 0, "transparent subtitle preview width")+ previewHeight := fs.Int("preview-height", 0, "transparent subtitle preview height")+ mixedTargetSubtitle := fs.String("mixed-target-subtitle", "", "target timeline for mixed vertical subtitle roles")+ targetOnly := fs.Bool("target-only", false, "render only the translation from bilingual input")dryRun := fs.Bool("dry-run", false, "validate command without running external services")if err := fs.Parse(args); err != nil {return Command{}, err}+ if *lineMode != string(pipeline.LineModeTargetOnly) && *lineMode != string(pipeline.LineModeBilingualTargetTop) && *lineMode != string(pipeline.LineModeBilingualTargetBottom) {+ return Command{}, fmt.Errorf("unsupported subtitle line mode: %s", *lineMode)+ }+ if (*previewWidth != 0 || *previewHeight != 0) && (*previewWidth < 32 || *previewWidth > 1920 || *previewHeight < 32 || *previewHeight > 1920) {+ return Command{}, errors.New("preview dimensions must both be between 32 and 1920")+ }return Command{Name: name,DryRun: *dryRun,SubtitleStyleFile: *subtitleStyleFile,Render: pipeline.RenderRequest{- Workdir: *workdir,- TaskID: *taskID,- Video: *video,- SourceURL: *sourceURL,- MediaWorkdir: *mediaWorkdir,- Audio: *audio,- Subtitle: *subtitle,- Horizontal: horizontal,- Dubbed: *dubbed,- MajorTitle: *majorTitle,- MinorTitle: *minorTitle,+ Workdir: *workdir,+ TaskID: *taskID,+ Video: *video,+ SourceURL: *sourceURL,+ MediaWorkdir: *mediaWorkdir,+ Audio: *audio,+ Subtitle: *subtitle,+ Horizontal: horizontal,+ Dubbed: *dubbed,+ MajorTitle: *majorTitle,+ MinorTitle: *minorTitle,+ LineMode: pipeline.LineMode(*lineMode),+ PreviewWidth: *previewWidth,+ PreviewHeight: *previewHeight,+ MixedTargetSubtitle: *mixedTargetSubtitle,+ TargetOnly: *targetOnly,},}, nil}diff --git a/runtime/krillinai/internal/cli/commands_test.go b/runtime/krillinai/internal/cli/commands_test.goindex 5daba4d0..9569d909 100644--- a/runtime/krillinai/internal/cli/commands_test.go+++ b/runtime/krillinai/internal/cli/commands_test.go@@ -138,6 +138,21 @@ func TestParseRenderCommandAcceptsSubtitleStyleFile(t *testing.T) {}}+func TestParseRenderPreviewAndLineMode(t *testing.T) {+ cmd, err := Parse([]string{"render-vertical", "--workdir", "preview", "--subtitle", "preview.srt", "--line-mode", "bilingual-target-bottom", "--preview-width", "540", "--preview-height", "960"})+ if err != nil {+ t.Fatal(err)+ }+ if cmd.Render.LineMode != pipeline.LineModeBilingualTargetBottom || cmd.Render.PreviewWidth != 540 || cmd.Render.PreviewHeight != 960 {+ t.Fatalf("preview parameters lost: %#v", cmd.Render)+ }+ for _, args := range [][]string{{"render-horizontal", "--line-mode", "unknown"}, {"render-horizontal", "--preview-width", "960"}} {+ if _, err := Parse(args); err == nil {+ t.Fatalf("invalid preview accepted: %v", args)+ }+ }+}+func TestParseVoicesCommand(t *testing.T) {cmd, err := Parse([]string{"voices",diff --git a/runtime/krillinai/internal/pipeline/render.go b/runtime/krillinai/internal/pipeline/render.goindex 4663c808..2e997ce6 100644--- a/runtime/krillinai/internal/pipeline/render.go+++ b/runtime/krillinai/internal/pipeline/render.go@@ -11,19 +11,24 @@ import ()type RenderRequest struct {- Workdir string- TaskID string- Video string- SourceURL string- MediaWorkdir string- ReportProgress func(string, int, string)- Audio string- Subtitle string- Horizontal bool- Dubbed bool- MajorTitle string- MinorTitle string- SubtitleStyle *subtitlestyle.StyleSet+ Workdir string+ TaskID string+ Video string+ SourceURL string+ MediaWorkdir string+ ReportProgress func(string, int, string)+ Audio string+ Subtitle string+ Horizontal bool+ Dubbed bool+ MajorTitle string+ MinorTitle string+ SubtitleStyle *subtitlestyle.StyleSet+ LineMode LineMode+ PreviewWidth int+ PreviewHeight int+ MixedTargetSubtitle string+ TargetOnly bool}func Render(ctx context.Context, svc StageService, req RenderRequest) (Response, error) {@@ -58,15 +63,35 @@ func Render(ctx context.Context, svc StageService, req RenderRequest) (Response,VerticalVideoMajorTitle: req.MajorTitle,VerticalVideoMinorTitle: req.MinorTitle,SubtitleStyle: req.SubtitleStyle,+ SubtitleResultType: types.SubtitleResultTypeBilingualTranslationOnTop,+ }+ if req.LineMode == LineModeBilingualTargetBottom {+ stepParam.SubtitleResultType = types.SubtitleResultTypeBilingualTranslationOnBottom+ }+ if req.MixedTargetSubtitle != "" {+ normalized := filepath.Join(req.Workdir, "render_subtitles.srt")+ err = normalizeMixedRenderSRT(subtitle, req.MixedTargetSubtitle, normalized, req.LineMode, req.TargetOnly)+ if err != nil {+ return failRenderStage(req, manifest, renderStage(req), "subtitle_roles_invalid", err)+ }+ subtitle = normalized+ } else if req.TargetOnly {+ normalized := filepath.Join(req.Workdir, "render_subtitles.srt")+ if err = ExtractTargetSRT(subtitle, normalized, req.LineMode); err != nil {+ return failRenderStage(req, manifest, renderStage(req), "subtitle_roles_invalid", err)+ }+ subtitle = normalized}rendered, err := svc.RenderVideo(ctx, service.RenderVideoRequest{- Workdir: req.Workdir,- InputVideo: inputVideo,- AudioFile: req.Audio,- SubtitleFile: subtitle,- OutputFile: output,- Horizontal: req.Horizontal,- StepParam: stepParam,+ Workdir: req.Workdir,+ InputVideo: inputVideo,+ AudioFile: req.Audio,+ SubtitleFile: subtitle,+ OutputFile: output,+ Horizontal: req.Horizontal,+ StepParam: stepParam,+ PreviewWidth: req.PreviewWidth,+ PreviewHeight: req.PreviewHeight,})if err != nil {return failRenderStage(req, manifest, renderStage(req), "render_video_failed", err)@@ -121,6 +146,9 @@ func renderSubtitle(req RenderRequest, manifest *Manifest) string {}func renderOutput(req RenderRequest) string {+ if req.PreviewWidth > 0 && req.PreviewHeight > 0 {+ return filepath.Join(req.Workdir, "subtitle-preview.png")+ }name := "vertical_bilingual.mp4"switch {case req.Horizontal && req.Dubbed:diff --git a/runtime/krillinai/internal/pipeline/render_test.go b/runtime/krillinai/internal/pipeline/render_test.goindex 3eaee9ea..51f3d4e1 100644--- a/runtime/krillinai/internal/pipeline/render_test.go+++ b/runtime/krillinai/internal/pipeline/render_test.go@@ -5,6 +5,7 @@ import ("encoding/json""krillin-ai/internal/service"subtitlestyle "krillin-ai/internal/subtitle_style"+ "krillin-ai/internal/types""os""path/filepath""strings"@@ -56,6 +57,7 @@ func TestRenderPassesSubtitleStyleToService(t *testing.T) {Subtitle: "bilingual_srt.srt",Horizontal: true,SubtitleStyle: style,+ LineMode: LineModeBilingualTargetBottom,}resp, err := Render(context.Background(), fake, req)@@ -68,6 +70,9 @@ func TestRenderPassesSubtitleStyleToService(t *testing.T) {if fake.lastRender.StepParam == nil || fake.lastRender.StepParam.SubtitleStyle != style {t.Fatalf("SubtitleStyle was not passed to service")}+ if fake.lastRender.StepParam.SubtitleResultType != types.SubtitleResultTypeBilingualTranslationOnBottom {+ t.Fatal("subtitle line ordering was not passed to service")+ }}func TestRenderVerticalBilingualOutputName(t *testing.T) {diff --git a/runtime/krillinai/internal/pipeline/srt.go b/runtime/krillinai/internal/pipeline/srt.goindex e9bf09e2..f22e0aeb 100644--- a/runtime/krillinai/internal/pipeline/srt.go+++ b/runtime/krillinai/internal/pipeline/srt.go@@ -102,6 +102,136 @@ func targetLine(lines []string, mode LineMode) (string, error) {}}+// Mixed vertical SRT writes each full target cue before its overlapping source cues.+func mixedSubtitleSourceCueIndexes(mixedPath, targetPath string) (map[int]bool, error) {+ blocks, err := readSRTBlocks(mixedPath)+ if err != nil {+ return nil, err+ }+ targets, err := readSRTBlocks(targetPath)+ if err != nil {+ return nil, err+ }+ timings := make(map[string]bool, len(targets))+ key := func(timestamp string) string {+ return strings.Join(pipelineSRTTimestampPattern.FindStringSubmatch(timestamp)[1:], ":")+ }+ for _, target := range targets {+ if !pipelineSRTTimestampPattern.MatchString(target.Timestamp) {+ return nil, fmt.Errorf("invalid target subtitle timestamp")+ }+ timings[key(target.Timestamp)] = true+ }+ matched := make(map[string]bool, len(targets))+ sourceIndexes := make(map[int]bool)+ for _, block := range blocks {+ if !pipelineSRTTimestampPattern.MatchString(block.Timestamp) {+ return nil, fmt.Errorf("invalid mixed subtitle timestamp")+ }+ index, err := strconv.Atoi(block.Index)+ if err != nil {+ return nil, err+ }+ // Imported bilingual cues already carry both roles in a single block.+ if len(block.Lines) > 1 {+ continue+ }+ timing := key(block.Timestamp)+ if timings[timing] && !matched[timing] {+ matched[timing] = true+ } else {+ sourceIndexes[index] = true+ }+ }+ return sourceIndexes, nil+}++// Pair legacy overlapping cues so preview and burn-in use the same bilingual layout.+func normalizeMixedRenderSRT(input, target, output string, mode LineMode, targetOnly bool) error {+ roles, err := mixedSubtitleSourceCueIndexes(input, target)+ if err != nil {+ return err+ }+ blocks, err := readSRTBlocks(input)+ if err != nil {+ return err+ }+ var b strings.Builder+ count := 0+ formatTime := func(ms int64) string {+ return fmt.Sprintf("%02d:%02d:%02d,%03d", ms/3600000, ms/60000%60, ms/1000%60, ms%1000)+ }+ write := func(start, end int64, lines []string) {+ if end <= start {+ return+ }+ count+++ fmt.Fprintf(&b, "%d\n%s --> %s\n%s\n\n", count, formatTime(start), formatTime(end), strings.Join(lines, "\n"))+ }+ for i := 0; i < len(blocks); {+ block := blocks[i]+ index, _ := strconv.Atoi(block.Index)+ if roles[index] {+ return fmt.Errorf("mixed subtitle cue %s has no matching translation timeline", block.Index)+ }+ start, end, err := parsePipelineSRTTimeline(block.Timestamp)+ if err != nil {+ return err+ }+ i+++ if len(block.Lines) > 1 {+ lines := block.Lines+ if targetOnly {+ text, err := targetLine(lines, mode)+ if err != nil {+ return err+ }+ lines = []string{text}+ }+ write(start, end, lines)+ continue+ }+ cursor := start+ for i < len(blocks) {+ source := blocks[i]+ sourceIndex, _ := strconv.Atoi(source.Index)+ if !roles[sourceIndex] {+ break+ }+ sourceStart, sourceEnd, err := parsePipelineSRTTimeline(source.Timestamp)+ if err != nil {+ return err+ }+ if sourceEnd <= start || sourceStart >= end {+ return fmt.Errorf("source cue %s does not overlap its translation", source.Index)+ }+ if !targetOnly {+ if sourceStart < cursor {+ sourceStart = cursor+ }+ if sourceEnd > end {+ sourceEnd = end+ }+ write(cursor, sourceStart, block.Lines)+ lines := []string{block.Lines[0], strings.Join(source.Lines, " ")}+ if mode == LineModeBilingualTargetBottom {+ lines[0], lines[1] = lines[1], lines[0]+ }+ write(sourceStart, sourceEnd, lines)+ if sourceEnd > cursor {+ cursor = sourceEnd+ }+ }+ i+++ }+ write(cursor, end, block.Lines)+ }+ if count == 0 {+ return fmt.Errorf("empty render subtitle")+ }+ return os.WriteFile(output, []byte(b.String()), 0644)+}+func validateExistingSubtitleOutputs(outputs Outputs) error {candidates := []struct {path stringdiff --git a/runtime/krillinai/internal/pipeline/srt_test.go b/runtime/krillinai/internal/pipeline/srt_test.goindex d804c56b..64cd315e 100644--- a/runtime/krillinai/internal/pipeline/srt_test.go+++ b/runtime/krillinai/internal/pipeline/srt_test.go@@ -87,3 +87,69 @@ func TestValidateSRTTimelineAllowsOverlapOnlyWhenRequested(t *testing.T) {t.Fatalf("validateSRTTimelineFile(allow overlap) error = %v", err)}}+func TestMixedVerticalSubtitleRolesUseTimelineInsteadOfLanguage(t *testing.T) {+ dir := t.TempDir()+ target := filepath.Join(dir, "target.srt")+ mixed := filepath.Join(dir, "mixed.srt")+ if err := os.WriteFile(target, []byte("1\n00:00:00,000 --> 00:00:02,000\nEnglish translation\n\n2\n00:00:02,000 --> 00:00:04,000\nSecond translation\n\n"), 0644); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(mixed, []byte("1\n00:00:00,000 --> 00:00:02,000\nEdited English translation\n\n2\n00:00:00,000 --> 00:00:02,000\n原文\n\n3\n00:00:02,000 --> 00:00:04,000\nSecond translation\n\n4\n00:00:02,000 --> 00:00:03,000\nEnglish original\n\n"), 0644); err != nil {+ t.Fatal(err)+ }+ roles, err := mixedSubtitleSourceCueIndexes(mixed, target)+ if err != nil {+ t.Fatal(err)+ }+ if len(roles) != 2 || !roles[2] || !roles[4] || roles[1] || roles[3] {+ t.Fatalf("incorrect subtitle roles: %#v", roles)+ }+}++func TestMixedRenderPairsEditedSubcuesAndPreservesGaps(t *testing.T) {+ for _, mode := range []LineMode{LineModeBilingualTargetTop, LineModeBilingualTargetBottom} {+ for _, targetOnly := range []bool{true, false} {+ dir := t.TempDir()+ target, mixed, out := filepath.Join(dir, "target.srt"), filepath.Join(dir, "mixed.srt"), filepath.Join(dir, "render.srt")+ os.WriteFile(target, []byte("1\n00:00:00,000 --> 00:00:03,000\nTranslation\n\n"), 0644)+ os.WriteFile(mixed, []byte("1\n00:00:00,000 --> 00:00:03,000\nEdited translation\n\n2\n00:00:00,000 --> 00:00:01,000\nEdited original\n\n3\n00:00:02,000 --> 00:00:03,000\nSecond original\n\n"), 0644)+ if err := normalizeMixedRenderSRT(mixed, target, out, mode, targetOnly); err != nil {+ t.Fatal(err)+ }+ blocks, err := readSRTBlocks(out)+ if err != nil {+ t.Fatal(err)+ }+ if targetOnly {+ if len(blocks) != 1 || blocks[0].Lines[0] != "Edited translation" {+ t.Fatalf("lost edited vertical translation: %#v", blocks)+ }+ continue+ }+ if len(blocks) != 3 || len(blocks[1].Lines) != 1 || blocks[1].Timestamp != "00:00:01,000 --> 00:00:02,000" {+ t.Fatalf("lost translation during source gap: %#v", blocks)+ }+ translationIndex := 0+ if mode == LineModeBilingualTargetBottom {+ translationIndex = 1+ }+ if blocks[0].Lines[translationIndex] != "Edited translation" || blocks[0].Lines[1-translationIndex] != "Edited original" {+ t.Fatalf("lost roles or edited text: %#v", blocks)+ }+ }+ }+}++func TestMixedRenderKeepsImportedBilingualCues(t *testing.T) {+ dir := t.TempDir()+ target, mixed, out := filepath.Join(dir, "target.srt"), filepath.Join(dir, "mixed.srt"), filepath.Join(dir, "render.srt")+ os.WriteFile(target, []byte("1\n00:00:00,000 --> 00:00:01,000\nTranslation\n\n"), 0644)+ os.WriteFile(mixed, []byte("1\n00:00:00,000 --> 00:00:01,000\nOriginal\nEdited translation\n\n"), 0644)+ if err := normalizeMixedRenderSRT(mixed, target, out, LineModeBilingualTargetBottom, true); err != nil {+ t.Fatal(err)+ }+ blocks, _ := readSRTBlocks(out)+ if len(blocks) != 1 || len(blocks[0].Lines) != 1 || blocks[0].Lines[0] != "Edited translation" {+ t.Fatalf("incorrect target-only import: %#v", blocks)+ }+}diff --git a/runtime/krillinai/internal/service/render_stage.go b/runtime/krillinai/internal/service/render_stage.goindex c120d243..66d7fe44 100644--- a/runtime/krillinai/internal/service/render_stage.go+++ b/runtime/krillinai/internal/service/render_stage.go@@ -5,6 +5,9 @@ import ("crypto/sha256""encoding/json""fmt"+ "image"+ "image/color"+ "image/png""krillin-ai/internal/storage"subtitlestyle "krillin-ai/internal/subtitle_style""krillin-ai/internal/types"@@ -16,13 +19,15 @@ import ()type RenderVideoRequest struct {- Workdir string- InputVideo string- AudioFile string- SubtitleFile string- OutputFile string- Horizontal bool- StepParam *types.SubtitleTaskStepParam+ Workdir string+ InputVideo string+ AudioFile string+ SubtitleFile string+ OutputFile string+ Horizontal bool+ StepParam *types.SubtitleTaskStepParam+ PreviewWidth int+ PreviewHeight int}type resolutionProbe func(inputVideo string) (int, int, error)@@ -55,6 +60,15 @@ func escapeAssFilterPath(path string) string {func buildEmbedSubtitleArgs(req RenderVideoRequest) ([]string, string) {assPath := renderAssPath(req)filter := buildAssFilterExpression(assPath, packagedSubtitleFontsDir())+ if req.PreviewWidth > 0 && req.PreviewHeight > 0 {+ // Recover straight alpha from opaque renders: libass alpha=1 differs at blended edges.+ previewFilter := fmt.Sprintf("[0:v]split[black][white];[black]%s[b];[white]lutrgb=r=255:g=255:b=255,%s[w];[b][w]vstack", filter, filter)+ return []string{+ "-y", "-f", "lavfi", "-i",+ fmt.Sprintf("color=c=black:s=%dx%d:d=1,format=rgb24", req.PreviewWidth, req.PreviewHeight),+ "-filter_complex", previewFilter, "-frames:v", "1", "-update", "1", req.OutputFile,+ }, assPath+ }args := []string{"-y","-i", req.InputVideo,@@ -104,11 +118,16 @@ func renderSubtitleFile(ctx context.Context, req RenderVideoRequest) (string, erstepParam = &types.SubtitleTaskStepParam{TaskBasePath: req.Workdir}req.StepParam = stepParam}- preparedReq, err := prepareSubtitleRenderLayout(req, getResolution, convertToVertical)- if err != nil {- return "", fmt.Errorf("renderSubtitleFile prepare subtitle layout error: %w", err)+ if req.PreviewWidth > 0 && req.PreviewHeight > 0 {+ req.StepParam.RenderWidth = req.PreviewWidth+ req.StepParam.RenderHeight = req.PreviewHeight+ } else {+ preparedReq, err := prepareSubtitleRenderLayout(req, getResolution, convertToVertical)+ if err != nil {+ return "", fmt.Errorf("renderSubtitleFile prepare subtitle layout error: %w", err)+ }+ req = preparedReq}- req = preparedReqif err := validatePackagedSubtitleFonts(req.StepParam.SubtitleStyle); err != nil {return "", fmt.Errorf("renderSubtitleFile subtitle fonts error: %w", err)}@@ -121,9 +140,61 @@ func renderSubtitleFile(ctx context.Context, req RenderVideoRequest) (string, erif err != nil {return "", fmt.Errorf("renderSubtitleFile ffmpeg error: %w, output: %s", err, string(output))}+ if req.PreviewWidth > 0 && req.PreviewHeight > 0 {+ if err := transparentSubtitlePreview(req.OutputFile, req.PreviewWidth, req.PreviewHeight); err != nil {+ return "", fmt.Errorf("renderSubtitleFile preview alpha error: %w", err)+ }+ }return req.OutputFile, nil}+func transparentSubtitlePreview(filename string, width, height int) error {+ f, err := os.Open(filename)+ if err != nil {+ return err+ }+ stacked, err := png.Decode(f)+ f.Close()+ if err != nil {+ return err+ }+ if stacked.Bounds().Dx() != width || stacked.Bounds().Dy() != height*2 {+ return fmt.Errorf("unexpected preview dimensions: %v", stacked.Bounds())+ }+ preview := image.NewNRGBA(image.Rect(0, 0, width, height))+ for y := 0; y < height; y++ {+ for x := 0; x < width; x++ {+ black := color.NRGBAModel.Convert(stacked.At(x, y)).(color.NRGBA)+ white := color.NRGBAModel.Convert(stacked.At(x, y+height)).(color.NRGBA)+ alpha := 255 - (int(white.R)-int(black.R)+int(white.G)-int(black.G)+int(white.B)-int(black.B)+1)/3+ if alpha <= 0 {+ continue+ }+ if alpha > 255 {+ alpha = 255+ }+ straight := func(value uint8) uint8 {+ v := (int(value)*255 + alpha/2) / alpha+ if v > 255 {+ v = 255+ }+ return uint8(v)+ }+ preview.SetNRGBA(x, y, color.NRGBA{R: straight(black.R), G: straight(black.G), B: straight(black.B), A: uint8(alpha)})+ }+ }+ f, err = os.Create(filename)+ if err != nil {+ return err+ }+ err = png.Encode(f, preview)+ closeErr := f.Close()+ if err != nil {+ return err+ }+ return closeErr+}+type subtitleFontManifest struct {Version int `json:"version"`Fonts []struct {diff --git a/runtime/krillinai/internal/service/srt_embed.go b/runtime/krillinai/internal/service/srt_embed.goindex 5bed51c9..48654bc9 100644--- a/runtime/krillinai/internal/service/srt_embed.go+++ b/runtime/krillinai/internal/service/srt_embed.go@@ -705,10 +705,14 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.Suif isHorizontal {screenStyle = styleSet.Horizontal}- majorTags := subtitlestyle.DialogueTags(screenStyle.Major)- minorTags := subtitlestyle.DialogueTags(screenStyle.Minor)- majorAlignment := subtitlestyle.Alignment(screenStyle.Major)- minorAlignment := subtitlestyle.Alignment(screenStyle.Minor)+ majorPrefix := fmt.Sprintf("{\\rMajor}{\\an%d}%s", subtitlestyle.Alignment(screenStyle.Major), subtitlestyle.DialogueTags(screenStyle.Major))+ minorPrefix := fmt.Sprintf("{\\rMinor}{\\an%d}%s", subtitlestyle.Alignment(screenStyle.Minor), subtitlestyle.DialogueTags(screenStyle.Minor))+ firstStyle, secondStyle := screenStyle.Major, screenStyle.Minor+ firstPrefix, secondPrefix := majorPrefix, minorPrefix+ if stepParam != nil && stepParam.SubtitleResultType == types.SubtitleResultTypeBilingualTranslationOnBottom {+ firstStyle, secondStyle = screenStyle.Minor, screenStyle.Major+ firstPrefix, secondPrefix = minorPrefix, majorPrefix+ }if isHorizontal {_, _ = assFile.WriteString(subtitlestyle.BuildAssHeader(styleSet, isHorizontal))@@ -766,15 +770,13 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.SuendFormatted := formatTimestamp(endTime)if len(subtitleLines) == 1 {majorText := joinASSLines(wrapSubtitleForASS(subtitleLines[0], screenStyle.Major, stepParam))- combinedText := fmt.Sprintf("%s{\\an%d}{\\rMajor}%s", majorTags, majorAlignment, majorText)+ combinedText := majorPrefix + majorText_, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))continue}- majorText := joinASSLines(wrapSubtitleForASS(subtitleLines[0], screenStyle.Major, stepParam))- minorText := joinASSLines(wrapSubtitleForASS(subtitleLines[1], screenStyle.Minor, stepParam))- combinedText := fmt.Sprintf("%s{\\an%d}{\\rMajor}%s\\N%s{\\an%d}{\\rMinor}%s",- majorTags, majorAlignment, majorText,- minorTags, minorAlignment, minorText)+ majorText := joinASSLines(wrapSubtitleForASS(subtitleLines[0], firstStyle, stepParam))+ minorText := joinASSLines(wrapSubtitleForASS(subtitleLines[1], secondStyle, stepParam))+ combinedText := firstPrefix + majorText + `\N` + secondPrefix + minorText_, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))}} else {@@ -819,11 +821,9 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.Suif len(subtitleLines) > 1 {startFormatted := formatTimestamp(startTime)endFormatted := formatTimestamp(endTime)- majorText := joinASSLines(wrapSubtitleForASS(subtitleLines[0], screenStyle.Major, stepParam))- minorText := joinASSLines(wrapSubtitleForASS(subtitleLines[1], screenStyle.Minor, stepParam))- combinedText := fmt.Sprintf("%s{\\an%d}{\\rMajor}%s\\N%s{\\an%d}{\\rMinor}%s",- majorTags, majorAlignment, majorText,- minorTags, minorAlignment, minorText)+ majorText := joinASSLines(wrapSubtitleForASS(subtitleLines[0], firstStyle, stepParam))+ minorText := joinASSLines(wrapSubtitleForASS(subtitleLines[1], secondStyle, stepParam))+ combinedText := firstPrefix + majorText + `\N` + secondPrefix + minorText_, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))continue}@@ -845,22 +845,16 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.Su}startFormatted := formatTimestamp(iStart)endFormatted := formatTimestamp(iEnd)- combinedText := fmt.Sprintf("%s{\\an%d}{\\rMajor}%s",- majorTags,- majorAlignment,- line)+ combinedText := majorPrefix + line_, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))}} else {// 处理英文字幕startFormatted := formatTimestamp(startTime)endFormatted := formatTimestamp(endTime)- cleanedText := joinASSLines(wrapSubtitleForASS(content, screenStyle.Minor, stepParam))- combinedText := fmt.Sprintf("%s{\\an%d}{\\rMinor}%s",- minorTags,- minorAlignment,- cleanedText)- _, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Minor,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))+ cleanedText := joinASSLines(wrapSubtitleForASS(content, screenStyle.Major, stepParam))+ combinedText := majorPrefix + cleanedText+ _, _ = assFile.WriteString(fmt.Sprintf("Dialogue: 0,%s,%s,Major,,0,0,0,,%s\n", startFormatted, endFormatted, combinedText))}}}diff --git a/runtime/krillinai/internal/service/srt_embed_test.go b/runtime/krillinai/internal/service/srt_embed_test.goindex 58e5aacc..4f80d5b0 100644--- a/runtime/krillinai/internal/service/srt_embed_test.go+++ b/runtime/krillinai/internal/service/srt_embed_test.go@@ -1,6 +1,7 @@package serviceimport (+ "fmt""math""os""path/filepath"@@ -90,10 +91,10 @@ func TestVerticalAssKeepsBothLinesFromBilingualSRT(t *testing.T) {t.Fatal(err)}ass := string(data)- if !strings.Contains(ass, "{\\rMajor}这是中文字幕") {+ if !strings.Contains(ass, "{\\rMajor}{\\an2}这是中文字幕") {t.Fatalf("vertical bilingual SRT should keep first line as Major: %s", ass)}- if !strings.Contains(ass, "{\\rMinor}This is the English subtitle") {+ if !strings.Contains(ass, "{\\rMinor}{\\an2}This is the English subtitle") {t.Fatalf("vertical bilingual SRT should keep second line as Minor: %s", ass)}}@@ -119,7 +120,7 @@ func TestHorizontalAssKeepsSingleLineSubtitle(t *testing.T) {if count := strings.Count(ass, "Dialogue:"); count != 1 {t.Fatalf("Dialogue count = %d, want 1; ass = %s", count, ass)}- if !strings.Contains(ass, "{\\an2}{\\rMajor}我认为学习速记是一项技能") {+ if !strings.Contains(ass, "{\\rMajor}{\\an2}我认为学习速记是一项技能") {t.Fatalf("single-line subtitle was not written as Major dialogue: %s", ass)}if strings.Contains(ass, "{\\rMinor}") {@@ -168,7 +169,7 @@ func TestHorizontalAssUsesCustomSubtitleStyle(t *testing.T) {if !strings.Contains(ass, ",4,1.5,2,10,10,44,1") {t.Fatalf("custom outline/margin missing:\n%s", ass)}- if !strings.Contains(ass, `{\fad(120,180)\blur1}{\an2}{\rMajor}主字幕`) {+ if !strings.Contains(ass, `{\rMajor}{\an2}{\fad(120,180)\blur1}主字幕`) {t.Fatalf("custom dialogue tags missing:\n%s", ass)}}@@ -333,7 +334,7 @@ func TestHorizontalAssWrapsUsingNarrowRenderWidth(t *testing.T) {}}-func TestVerticalAssUsesCustomMinorStyle(t *testing.T) {+func TestVerticalEnglishTranslationUsesMajorStyle(t *testing.T) {dir := t.TempDir()in := filepath.Join(dir, "subtitle.srt")out := filepath.Join(dir, "subtitle.ass")@@ -344,8 +345,8 @@ func TestVerticalAssUsesCustomMinorStyle(t *testing.T) {fontSize := 11style := subtitlestyle.DefaultStyleSet()- style.Vertical.Minor.FontSize = &fontSize- style.Vertical.Minor.PrimaryColor = "#00FF00"+ style.Vertical.Major.FontSize = &fontSize+ style.Vertical.Major.PrimaryColor = "#00FF00"err := srtToAss(in, out, false, &types.SubtitleTaskStepParam{TaskBasePath: dir,@@ -359,8 +360,33 @@ func TestVerticalAssUsesCustomMinorStyle(t *testing.T) {t.Fatal(err)}ass := string(data)- if !strings.Contains(ass, "Style: Minor,Arial,11,&H0000FF00") {- t.Fatalf("custom vertical Minor style missing:\n%s", ass)+ if !strings.Contains(ass, "Style: Major,Arial,11,&H0000FF00") || !strings.Contains(ass, `{\rMajor}{\an2}English subtitle`) {+ t.Fatalf("English translation did not use the primary style:\n%s", ass)+ }+}++func TestBilingualTranslationOnBottomKeepsSemanticStylesAndShadow(t *testing.T) {+ for _, horizontal := range []bool{true, false} {+ t.Run(fmt.Sprint(horizontal), func(t *testing.T) {+ dir := t.TempDir()+ in, out := filepath.Join(dir, "subtitle.srt"), filepath.Join(dir, "subtitle.ass")+ if err := os.WriteFile(in, []byte("1\n00:00:00,000 --> 00:00:01,000\nSource\nTranslation\n\n"), 0644); err != nil {+ t.Fatal(err)+ }+ style := subtitlestyle.DefaultStyleSet()+ style.Horizontal.Major.OverrideTags = `\xshad-4\yshad6\blur2.5`+ style.Vertical.Major.OverrideTags = style.Horizontal.Major.OverrideTags+ if err := srtToAss(in, out, horizontal, &types.SubtitleTaskStepParam{SubtitleStyle: style, SubtitleResultType: types.SubtitleResultTypeBilingualTranslationOnBottom}); err != nil {+ t.Fatal(err)+ }+ data, err := os.ReadFile(out)+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(data), `{\rMinor}{\an2}Source\N{\rMajor}{\an2}{\xshad-4\yshad6\blur2.5}Translation`) {+ t.Fatalf("subtitle roles or shadow reset order are wrong: %s", data)+ }+ })}}diff --git a/runtime/krillinai/internal/subtitle_style/style.go b/runtime/krillinai/internal/subtitle_style/style.goindex 2d50c83d..374f2d63 100644--- a/runtime/krillinai/internal/subtitle_style/style.go+++ b/runtime/krillinai/internal/subtitle_style/style.go@@ -137,6 +137,7 @@ func BuildAssHeader(set *StyleSet, horizontal bool) string {builder.WriteString("Title: Example\n")builder.WriteString("Original Script: \n")builder.WriteString("ScriptType: v4.00+\n")+ builder.WriteString("PlayResX: 384\nPlayResY: 288\nScaledBorderAndShadow: yes\n")builder.WriteString("PlayDepth: 0\n\n")builder.WriteString("[V4+ Styles]\n")builder.WriteString("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n")