Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: a subtitle line with & or < reads as written in WebVTT (#72)

HEOJUNFOc97b30d553d2
diff --git a/.changeset/vtt-escape-text.md b/.changeset/vtt-escape-text.md
new file mode 100644
index 0000000..4010311
--- /dev/null
+++ b/.changeset/vtt-escape-text.md
@@ -0,0 +1,5 @@
+---
+'openfilm': patch
+---
+
+A subtitle line with `&`, `<` or `>` reads as written in an exported WebVTT file. A line like `Tom & <Jerry>` lost `<Jerry>`, which players read as a tag, and a speaker's `&` broke the voice tag; they are now written as WebVTT's `&amp;`, `&lt;` and `&gt;`, as the voice-over captions already were. `-->` in a line stays `-->` instead of turning into `→`.
diff --git a/packages/openfilm/studio/server/film-subtitle.mjs b/packages/openfilm/studio/server/film-subtitle.mjs
index 6c4801c..b960fdc 100644
--- a/packages/openfilm/studio/server/film-subtitle.mjs
+++ b/packages/openfilm/studio/server/film-subtitle.mjs
@@ -721,11 +721,14 @@ export function filmSubtitleSrt(cues) {
return blocks.length ? `${blocks.join('\n\n')}\n` : '';
}
+/** `&`, `<` and `>` as VTT's escapes: a raw `<` starts a tag, `&` an escape, and `-->` would end the cue's timing. */
+const escapeVtt = (/** @type {string} */ s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
/** A WebVTT file; the speaker as `<v name>`, VTT's own voice tag. @param {readonly FilmSubtitleCue[]} cues */
export function filmSubtitleVtt(cues) {
const blocks = sequencedCues(cues).map(({ cue, end }) => {
- const text = cue.text.trim().replace(/-->/g, '→');
- const line = cue.speaker ? `<v ${cue.speaker.replace(/[<>]/g, '')}>${text}` : text;
+ const text = escapeVtt(cue.text.trim());
+ const line = cue.speaker ? `<v ${escapeVtt(cue.speaker)}>${text}` : text;
return `${srtStamp(cue.startMs).replace(',', '.')} --> ${srtStamp(end).replace(',', '.')}\n${line}`;
});
return blocks.length ? `WEBVTT\n\n${blocks.join('\n\n')}\n` : '';
diff --git a/packages/openfilm/studio/server/film-subtitle.test.mjs b/packages/openfilm/studio/server/film-subtitle.test.mjs
index c980a90..d4da702 100644
--- a/packages/openfilm/studio/server/film-subtitle.test.mjs
+++ b/packages/openfilm/studio/server/film-subtitle.test.mjs
@@ -80,6 +80,12 @@ test('files: overlapping lines give way, speakers stay apart from the text', ()
assert.equal(filmSubtitleSrt([]), '');
});
+test('files: a VTT line with & or < reads as written, not as a tag', () => {
+ const cues = [{ startMs: 0, durMs: 1000, text: 'Tom & <Jerry> --> out', speaker: 'Q&A <host>' }];
+ assert.equal(filmSubtitleVtt(cues), 'WEBVTT\n\n00:00:00.000 --> 00:00:01.000\n<v Q&amp;A &lt;host&gt;>Tom &amp; &lt;Jerry&gt; --&gt; out\n');
+ assert.equal(filmSubtitleSrt(cues), '1\n00:00:00,000 --> 00:00:01,000\nQ&A <host>:Tom & <Jerry> --> out\n');
+});
+
test('a burned-in frame: the line in the frame\'s pixels, escaped, nothing when off or silent', () => {
const cues = [{ startMs: 0, durMs: 1000, text: 'Tom & <Jerry>' }];
const html = filmSubtitleFrameHtml(cues, D, { w: 1920, h: 1080 }, 500);