BlueGit

timfa filemanager

sanitize {notes} in VTT

timfa Tim Falken · pushed by timfa

Authored 2026-06-13 · 5 months ago

Hash
e3629e40d175c8db43ba5910c47f331bda227e2f
Parent
ccb65d4a
2 files changed +102 −3
server/video-stream.js +50 −0
@@ -728,6 +728,44 @@ function findMatchingSidecarSrtPath (sourcePath)
728 728 return null;
729 729 }
730 730
731 function stripSubtitleEditorNotes (text)
732 {
733 return String(text || '')
734 .replace(/\{[^{}]*\}/g, '')
735 .replace(/[ \t]{2,}/g, ' ')
736 .trim();
737 }
738
739 function isWebVttStructuralLine (line)
740 {
741 const trimmed = String(line || '').trim();
742 if (!trimmed) return true;
743 if (/^WEBVTT\b/i.test(trimmed)) return true;
744 if (trimmed.includes('-->')) return true;
745 if (/^(NOTE|STYLE|REGION)\b/i.test(trimmed)) return true;
746 if (/^\d+$/.test(trimmed)) return true;
747 return false;
748 }
749
750 function sanitizeWebVttContent (vttContent)
751 {
752 return String(vttContent || '').split(/\r?\n/).map((line) =>
753 {
754 if (isWebVttStructuralLine(line)) return line;
755 return stripSubtitleEditorNotes(line);
756 }).join('\n');
757 }
758
759 async function sanitizeWebVttFile (filePath)
760 {
761 const raw = await fs.promises.readFile(filePath, 'utf8');
762 const sanitized = sanitizeWebVttContent(raw);
763 if (sanitized !== raw)
764 {
765 await fs.promises.writeFile(filePath, sanitized, 'utf8');
766 }
767 }
768
731 769 async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
732 770 {
733 771 const extractedTracks = [];
@@ -772,6 +810,15 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
772 810 continue;
773 811 }
774 812
813 try
814 {
815 await sanitizeWebVttFile(targetPath);
816 } catch
817 {
818 skippedCount += 1;
819 continue;
820 }
821
775 822 extractedTracks.push({
776 823 fileName,
777 824 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
@@ -798,6 +845,7 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
798 845 '-c:s', 'webvtt',
799 846 targetPath,
800 847 ]);
848 await sanitizeWebVttFile(targetPath);
801 849
802 850 extractedTracks.push({
803 851 fileName,
@@ -1331,4 +1379,6 @@ module.exports = {
1331 1379 getActiveStreamCount,
1332 1380 getVideoStreamDebugSnapshot,
1333 1381 cleanupIdleStreamArtifacts,
1382 stripSubtitleEditorNotes,
1383 sanitizeWebVttContent,
1334 1384 };
@@ -728,6 +728,44 @@ function findMatchingSidecarSrtPath (sourcePath)
728 return null;
728 return null;
729}
729}
730
730
731function stripSubtitleEditorNotes (text)
732{
733 return String(text || '')
734 .replace(/\{[^{}]*\}/g, '')
735 .replace(/[ \t]{2,}/g, ' ')
736 .trim();
737}
738
739function isWebVttStructuralLine (line)
740{
741 const trimmed = String(line || '').trim();
742 if (!trimmed) return true;
743 if (/^WEBVTT\b/i.test(trimmed)) return true;
744 if (trimmed.includes('-->')) return true;
745 if (/^(NOTE|STYLE|REGION)\b/i.test(trimmed)) return true;
746 if (/^\d+$/.test(trimmed)) return true;
747 return false;
748}
749
750function sanitizeWebVttContent (vttContent)
751{
752 return String(vttContent || '').split(/\r?\n/).map((line) =>
753 {
754 if (isWebVttStructuralLine(line)) return line;
755 return stripSubtitleEditorNotes(line);
756 }).join('\n');
757}
758
759async function sanitizeWebVttFile (filePath)
760{
761 const raw = await fs.promises.readFile(filePath, 'utf8');
762 const sanitized = sanitizeWebVttContent(raw);
763 if (sanitized !== raw)
764 {
765 await fs.promises.writeFile(filePath, sanitized, 'utf8');
766 }
767}
768
731async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
769async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
732{
770{
733 const extractedTracks = [];
771 const extractedTracks = [];
@@ -772,6 +810,15 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
772 continue;
810 continue;
773 }
811 }
774
812
813 try
814 {
815 await sanitizeWebVttFile(targetPath);
816 } catch
817 {
818 skippedCount += 1;
819 continue;
820 }
821
775 extractedTracks.push({
822 extractedTracks.push({
776 fileName,
823 fileName,
777 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
824 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
@@ -798,6 +845,7 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
798 '-c:s', 'webvtt',
845 '-c:s', 'webvtt',
799 targetPath,
846 targetPath,
800 ]);
847 ]);
848 await sanitizeWebVttFile(targetPath);
801
849
802 extractedTracks.push({
850 extractedTracks.push({
803 fileName,
851 fileName,
@@ -1331,4 +1379,6 @@ module.exports = {
1331 getActiveStreamCount,
1379 getActiveStreamCount,
1332 getVideoStreamDebugSnapshot,
1380 getVideoStreamDebugSnapshot,
1333 cleanupIdleStreamArtifacts,
1381 cleanupIdleStreamArtifacts,
1382 stripSubtitleEditorNotes,
1383 sanitizeWebVttContent,
1334};
1384};
tests/video-stream.test.js +52 −3
@@ -334,7 +334,13 @@ describe('video stream bootstrap', () =>
334 334 process.nextTick(() =>
335 335 {
336 336 fs.mkdirSync(path.dirname(subtitleTarget), { recursive: true });
337 fs.writeFileSync(subtitleTarget, 'WEBVTT\n\n');
337 fs.writeFileSync(subtitleTarget, [
338 'WEBVTT',
339 '',
340 '00:00:01.000 --> 00:00:02.000',
341 'Hello {editor note} world',
342 '',
343 ].join('\n'));
338 344 child.emit('close', 0);
339 345 });
340 346 return child;
@@ -351,8 +357,9 @@ describe('video stream bootstrap', () =>
351 357 return child;
352 358 });
353 359
354 const { createVideoStreamSession } = require('../server/video-stream');
355 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
360 const { createVideoStreamSession, STREAM_ROOT_DIR } = require('../server/video-stream');
361 const session = await createVideoStreamSession(sourceFile);
362 expect(session).toEqual({
356 363 sessionId: expect.any(String),
357 364 durationSeconds: null,
358 365 videoWidth: 854,
@@ -372,6 +379,15 @@ describe('video stream bootstrap', () =>
372 379 skipped: 0,
373 380 },
374 381 });
382
383 const vttPath = path.join(
384 STREAM_ROOT_DIR,
385 fs.readdirSync(STREAM_ROOT_DIR)[0],
386 'subtitle-0.vtt',
387 );
388 const vttText = fs.readFileSync(vttPath, 'utf8');
389 expect(vttText).toContain('Hello world');
390 expect(vttText).not.toContain('{editor note}');
375 391 });
376 392
377 393 test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () =>
@@ -963,3 +979,36 @@ describe('video stream bootstrap', () =>
963 979 await expect(assetPromise).resolves.toBeUndefined();
964 980 });
965 981 });
982
983 describe('text subtitle editor-note sanitization', () =>
984 {
985 const { stripSubtitleEditorNotes, sanitizeWebVttContent } = require('../server/video-stream');
986
987 test('stripSubtitleEditorNotes removes brace-wrapped notes from cue text', () =>
988 {
989 const input = "He grabs the handle{Maybe rephrase, don't make it this short} and jumps";
990 expect(stripSubtitleEditorNotes(input)).toBe('He grabs the handle and jumps');
991 });
992
993 test('stripSubtitleEditorNotes removes multiple notes in one line', () =>
994 {
995 expect(stripSubtitleEditorNotes('Hello {note one} there {note two} friend'))
996 .toBe('Hello there friend');
997 });
998
999 test('sanitizeWebVttContent preserves timestamps and strips cue text notes', () =>
1000 {
1001 const vtt = [
1002 'WEBVTT',
1003 '',
1004 '00:00:01.000 --> 00:00:04.000',
1005 "He grabs the handle{Maybe rephrase, don't make it this short} and jumps",
1006 '',
1007 ].join('\n');
1008
1009 const sanitized = sanitizeWebVttContent(vtt);
1010 expect(sanitized).toContain('00:00:01.000 --> 00:00:04.000');
1011 expect(sanitized).toContain('He grabs the handle and jumps');
1012 expect(sanitized).not.toContain('{Maybe rephrase');
1013 });
1014 });
@@ -334,7 +334,13 @@ describe('video stream bootstrap', () =>
334 process.nextTick(() =>
334 process.nextTick(() =>
335 {
335 {
336 fs.mkdirSync(path.dirname(subtitleTarget), { recursive: true });
336 fs.mkdirSync(path.dirname(subtitleTarget), { recursive: true });
337 fs.writeFileSync(subtitleTarget, 'WEBVTT\n\n');
337 fs.writeFileSync(subtitleTarget, [
338 'WEBVTT',
339 '',
340 '00:00:01.000 --> 00:00:02.000',
341 'Hello {editor note} world',
342 '',
343 ].join('\n'));
338 child.emit('close', 0);
344 child.emit('close', 0);
339 });
345 });
340 return child;
346 return child;
@@ -351,8 +357,9 @@ describe('video stream bootstrap', () =>
351 return child;
357 return child;
352 });
358 });
353
359
354 const { createVideoStreamSession } = require('../server/video-stream');
360 const { createVideoStreamSession, STREAM_ROOT_DIR } = require('../server/video-stream');
355 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
361 const session = await createVideoStreamSession(sourceFile);
362 expect(session).toEqual({
356 sessionId: expect.any(String),
363 sessionId: expect.any(String),
357 durationSeconds: null,
364 durationSeconds: null,
358 videoWidth: 854,
365 videoWidth: 854,
@@ -372,6 +379,15 @@ describe('video stream bootstrap', () =>
372 skipped: 0,
379 skipped: 0,
373 },
380 },
374 });
381 });
382
383 const vttPath = path.join(
384 STREAM_ROOT_DIR,
385 fs.readdirSync(STREAM_ROOT_DIR)[0],
386 'subtitle-0.vtt',
387 );
388 const vttText = fs.readFileSync(vttPath, 'utf8');
389 expect(vttText).toContain('Hello world');
390 expect(vttText).not.toContain('{editor note}');
375 });
391 });
376
392
377 test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () =>
393 test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () =>
@@ -963,3 +979,36 @@ describe('video stream bootstrap', () =>
963 await expect(assetPromise).resolves.toBeUndefined();
979 await expect(assetPromise).resolves.toBeUndefined();
964 });
980 });
965});
981});
982
983describe('text subtitle editor-note sanitization', () =>
984{
985 const { stripSubtitleEditorNotes, sanitizeWebVttContent } = require('../server/video-stream');
986
987 test('stripSubtitleEditorNotes removes brace-wrapped notes from cue text', () =>
988 {
989 const input = "He grabs the handle{Maybe rephrase, don't make it this short} and jumps";
990 expect(stripSubtitleEditorNotes(input)).toBe('He grabs the handle and jumps');
991 });
992
993 test('stripSubtitleEditorNotes removes multiple notes in one line', () =>
994 {
995 expect(stripSubtitleEditorNotes('Hello {note one} there {note two} friend'))
996 .toBe('Hello there friend');
997 });
998
999 test('sanitizeWebVttContent preserves timestamps and strips cue text notes', () =>
1000 {
1001 const vtt = [
1002 'WEBVTT',
1003 '',
1004 '00:00:01.000 --> 00:00:04.000',
1005 "He grabs the handle{Maybe rephrase, don't make it this short} and jumps",
1006 '',
1007 ].join('\n');
1008
1009 const sanitized = sanitizeWebVttContent(vtt);
1010 expect(sanitized).toContain('00:00:01.000 --> 00:00:04.000');
1011 expect(sanitized).toContain('He grabs the handle and jumps');
1012 expect(sanitized).not.toContain('{Maybe rephrase');
1013 });
1014});