The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Exports: a 剪映 or CapCut draft's subtitles are the app's own subtitles, not text clips (#254)
riverisbuildinge923057dc414
diff --git a/.changeset/draft-subtitles-as-subtitles.md b/.changeset/draft-subtitles-as-subtitles.mdnew file mode 100644index 0000000..2d3764b--- /dev/null+++ b/.changeset/draft-subtitles-as-subtitles.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Drafts for 剪映 and CapCut: the film's subtitles now arrive as the app's own subtitles, on a subtitle track in their language, like the subtitles it recognizes itself. You can find, restyle and edit them together, instead of as separate text clips.diff --git a/packages/openfilm/studio/server/exports.mjs b/packages/openfilm/studio/server/exports.mjsindex 34a4336..18603f1 100644--- a/packages/openfilm/studio/server/exports.mjs+++ b/packages/openfilm/studio/server/exports.mjs@@ -1146,9 +1146,9 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoupdate(job, { encoder: prores && !app ? (await hardwareEncoder('prores', have)) ?? 'prores_ks' : 'png' });/* a source out of the project fails the export before anything is drawn */for (const track of doc.tracks) for (const row of track.clips) clipPath(root, row.src);- const { cues } = ask.subtitles === false ? { cues: [] } : await subtitlesOf(root).catch((error) => {+ const { cues, style: shown } = ask.subtitles === false ? { cues: [], style: null } : await subtitlesOf(root).catch((error) => {if (fcp) throw error;- return { cues: [] };+ return { cues: [], style: null };});const scratch = join(root, '.film', 'cache', 'export', `nle-${process.pid}-${randomBytes(4).toString('hex')}`);const final = freeFile(dir, baseName(job.name, ask), '');@@ -1391,7 +1391,8 @@ export function createExports({ onChange = () => {}, captions = subtitlesForExpoupdate(job, { phase: 'finishing', progress: 0.98 });if (app) {/* the app on Windows reads draft_content.json, on a Mac draft_info.json: the same draft in both */- const draft = jianyingDraft(timeline, { app, name: basename(final), folder: final, root: dir, cues });+ /* the subtitles' language: a translation shown alone is in its own; else told from the words (jianying.mjs) */+ const draft = jianyingDraft(timeline, { app, name: basename(final), folder: final, root: dir, cues, language: shown?.bilingual ? null : shown?.language });const info = JSON.stringify(draft.info);await writeFile(join(staging, 'draft_info.json'), info, 'utf8');await writeFile(join(staging, 'draft_content.json'), info, 'utf8');diff --git a/packages/openfilm/studio/server/jianying.mjs b/packages/openfilm/studio/server/jianying.mjsindex 3d17b02..dc67297 100644--- a/packages/openfilm/studio/server/jianying.mjs+++ b/packages/openfilm/studio/server/jianying.mjs@@ -19,7 +19,9 @@* · Footage keeps its own sound: the sound clip linked to it in the plan becomes the video clip's level, so nothing* plays twice. Other sounds are audio clips, their fades 剪映's own fades.* · A hidden track's clips come hidden, a muted track's sounds muted, one click from back on.- * · Subtitles are text clips on a track of their own, white with a dark edge, low in the picture.+ * · Subtitles are the app's own subtitles, not text clips beside them: a subtitle track (`flag` 1), each line a+ * `subtitle` material of one subtitle task (`config.subtitle_taskinfo`), as its recognized subtitles are, so the+ * app lists, restyles and edits them together. White with a dark edge, low in the picture.* · Each file is in the draft's media library (draft_meta_info.json `draft_materials`, group 0, what the app lists* under its media panel), and each material names its entry there (`local_material_id`): without it the app* lists none of them and finds a video by nothing but its path.@@ -27,6 +29,7 @@import { randomUUID } from 'node:crypto';import { readFileSync } from 'node:fs';import { join } from 'node:path';+import { filmSubtitleLanguageOf } from './film-subtitle.mjs';/*** @typedef {import('./nle.mjs').NleTimeline} NleTimeline@@ -104,13 +107,26 @@ const clipSettings = (/** @type {number} */ scale, /** @type {number} */ x, /**alpha: 1, flip: { horizontal: false, vertical: false }, rotation: 0, scale: { x: scale, y: scale }, transform: { x, y },});+/**+ * A language as the app names its subtitles' (`zh-CN`, `en-US`): a region-less code takes its usual region; one the+ * script does not tell (Latin) is English.+ * @param {string | null | undefined} code+ */+function appLanguage(code) {+ const [base, region] = String(code || 'en').split(/[-_]/);+ if (region) return `${base.toLowerCase()}-${/^hant$/i.test(region) ? 'TW' : /^hans$/i.test(region) ? 'CN' : region.toUpperCase()}`;+ const usual = /** @type {Record<string, string>} */ ({ zh: 'CN', ja: 'JP', ko: 'KR', en: 'US', es: 'ES', fr: 'FR', de: 'DE', pt: 'BR', ru: 'RU', th: 'TH', ar: 'SA', hi: 'IN', vi: 'VN', id: 'ID', it: 'IT' });+ const lower = base.toLowerCase();+ return `${lower}-${usual[lower] ?? lower.toUpperCase()}`;+}+const round6 = (/** @type {number} */ n) => { const r = Math.round(n * 1e6) / 1e6; return Object.is(r, -0) ? 0 : r; };/*** The draft: `info` (draft_info.json) and `meta` (draft_meta_info.json), for the folder `folder` in the app's drafts* folder `root`.* @param {NleTimeline} timeline- * @param {{ app?: DraftApp, name: string, folder: string, root: string, cues?: readonly JianyingCue[], now?: number }} opts+ * @param {{ app?: DraftApp, name: string, folder: string, root: string, cues?: readonly JianyingCue[], language?: string | null, now?: number }} opts*/export function jianyingDraft(timeline, opts) {const platform = PLATFORM[opts.app ?? 'jianying'];@@ -247,18 +263,26 @@ export function jianyingDraft(timeline, opts) {});}- /* subtitles: a text track over everything */+ /* subtitles: the app's own, a subtitle track over everything, in the cues' language (as the app names it) */const cues = (opts.cues ?? []).filter((c) => c.text.trim() && c.durMs > 0);+ /** @type {Record<string, unknown>[]} */+ const subtitleTasks = [];if (cues.length) {+ const language = appLanguage(opts.language || filmSubtitleLanguageOf(cues.map((c) => c.text).join(' ')));+ const task = `${hex().slice(0, 24)}_0_0`;+ const group = `${language}_${opts.now ?? Date.now()}`;+ subtitleTasks.push({ id: task, type: 10, language, content: '', remove_invalid_task_id: '', ai_accurate_recognize_enable: false, supplies_commit_id: '', is_local_asr: false });const index = renderIndex++;const frame = (/** @type {number} */ ms) => Math.round((ms / 1000) * fps);tracks.push({- attribute: 0, flag: 0, id: hex(), is_default_name: false, name: 'Subtitles', type: 'text',+ attribute: 0, flag: 1, id: hex(), is_default_name: false, name: 'Subtitles', type: 'text',segments: cues.map((cue) => {const id = hex();const text = cue.text.trim();materials.texts.push({- id, type: 'text', typesetting: 0, alignment: 1, letter_spacing: 0, line_spacing: 0.02, line_feed: 1,+ id, type: 'subtitle', add_type: 1, recognize_task_id: task, recognize_text: text, language, group_id: group,+ words: { start_time: [], end_time: [], text: [] }, current_words: { start_time: [], end_time: [], text: [] },+ subtitle_keywords: { range: [] }, typesetting: 0, alignment: 1, letter_spacing: 0, line_spacing: 0.02, line_feed: 1,line_max_width: 0.82, force_apply_line_max_width: false, check_flag: 15, global_alpha: 1,content: JSON.stringify({styles: [{@@ -289,7 +313,7 @@ export function jianyingDraft(timeline, opts) {lyrics_recognition_id: '', lyrics_sync: true, lyrics_taskinfo: [], maintrack_adsorb: false, material_save_mode: 0,multi_language_current: 'none', multi_language_list: [], multi_language_main: 'none', multi_language_mode: 'none',original_sound_last_index: 1, record_audio_last_index: 1, sticker_max_index: 1, subtitle_keywords_config: null,- subtitle_recognition_id: '', subtitle_sync: true, subtitle_taskinfo: [], system_font_list: [], video_mute: false,+ subtitle_recognition_id: '', subtitle_sync: true, subtitle_taskinfo: subtitleTasks, system_font_list: [], video_mute: false,zoom_info_params: null,},cover: null, create_time: 0, duration, extra_info: null, fps, free_render_index_mode_on: false, group_container: null,diff --git a/packages/openfilm/studio/server/jianying.test.mjs b/packages/openfilm/studio/server/jianying.test.mjsindex e2813a4..b2e1f48 100644--- a/packages/openfilm/studio/server/jianying.test.mjs+++ b/packages/openfilm/studio/server/jianying.test.mjs@@ -75,10 +75,17 @@ test('footage plays its own sound at its level; other sounds are audio clips witassert.equal(silent.info.tracks.find((t) => t.type === 'audio').segments[0].volume, 0, 'a sound not heard comes silent');});-test('subtitles: a text track over all, one clip a cue, white with a dark edge, low in the picture', () => {- const { info } = jianyingDraft(plan(), { name: 'Film', folder: '/d/Film', root: '/d', cues: [{ text: '你好 world', startMs: 500, durMs: 1500 }, { text: ' ', startMs: 2000, durMs: 500 }] });+test('subtitles: the app\'s own subtitle track over all, one subtitle a cue, white with a dark edge, low in the picture', () => {+ const { info } = jianyingDraft(plan(), { name: 'Film', folder: '/d/Film', root: '/d', now: 1, cues: [{ text: '你好 world', startMs: 500, durMs: 1500 }, { text: ' ', startMs: 2000, durMs: 500 }] });const text = info.tracks.at(-1);assert.equal(text.type, 'text');+ /* as the app keeps the subtitles it recognizes: a subtitle track, its lines of one subtitle task, in their language */+ assert.equal(text.flag, 1);+ const [line] = info.materials.texts;+ assert.equal(line.type, 'subtitle');+ assert.equal(line.recognize_text, '你好 world');+ assert.equal(line.language, 'zh-CN');+ assert.deepEqual(info.config.subtitle_taskinfo.map((t) => [t.id, t.type, t.language]), [[line.recognize_task_id, 10, 'zh-CN']]);assert.equal(text.segments.length, 1, 'an empty cue is left out');assert.deepEqual(text.segments[0].target_timerange, { start: 500_000, duration: 1_500_000 });assert.equal(text.segments[0].clip.transform.y, -0.8);@@ -170,3 +177,12 @@ test('each file is in the draft\'s media library, once, and each material namesconst shot = library.find((e) => e.file_Path.endsWith('a.mp4'));assert.deepEqual([shot.width, shot.height, shot.duration], [960, 540, 10_000_000]);});++test('subtitles in the language shown: a translation picked is its own, Latin words without one are English, none no task', () => {+ const cue = [{ text: 'Hola a todos', startMs: 0, durMs: 1000 }];+ const lang = (/** @type {any} */ o) => jianyingDraft(plan(), { name: 'Film', folder: '/d/Film', root: '/d', cues: cue, ...o }).info.config.subtitle_taskinfo[0]?.language;+ assert.equal(lang({ language: 'es' }), 'es-ES');+ assert.equal(lang({ language: 'zh-Hant' }), 'zh-TW');+ assert.equal(lang({}), 'en-US');+ assert.deepEqual(jianyingDraft(plan(), { name: 'Film', folder: '/d/Film', root: '/d' }).info.config.subtitle_taskinfo, []);+});