From 15787104fd4ba59c5e5f334945e6ec0ca390056f Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 16 Jul 2026 17:51:37 -0600 Subject: [PATCH] =?UTF-8?q?videos:=20address=20review=20=E2=80=94=20tighte?= =?UTF-8?q?n=20auto-caption=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Require `line === 'auto'` in the auto-caption signature so a cue that is deliberately placed (e.g. `line:0` for top-edge) is left alone even if it is also `align:start position:0%`. YouTube auto-captions never set `line`. - For lazily-loaded language tracks, only detach the cuechange listener after the cue list is actually populated, in case a cuechange fires before cues load. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/src/components/VideoPlayer.js | 17 ++++++++++------- app/src/components/VideoPlayer.test.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/components/VideoPlayer.js b/app/src/components/VideoPlayer.js index be602e7b..c0604528 100644 --- a/app/src/components/VideoPlayer.js +++ b/app/src/components/VideoPlayer.js @@ -37,17 +37,18 @@ const MEDIA_PATH = '/media'; // which pins the text to the bottom-left of the player and makes it hard to read. Detect that signature and // reset those cues to the browser's centered default. This mutates only the in-memory VTTCue objects the // browser parsed; the .vtt file on disk is never modified. Cues that were deliberately positioned (anything -// other than the auto-caption signature) are left untouched. +// other than the auto-caption signature) are left untouched. YouTube auto-captions never set `line`, so we +// require `line === 'auto'` too; that leaves a deliberately placed cue (e.g. `line:0` for top-edge) alone even +// if it happens to also be `align:start position:0%`. export function centerAutoCaptions(track) { if (!track || !track.cues) { return 0; } let changed = 0; for (const cue of track.cues) { - if (cue.align === 'start' && cue.position === 0) { + if (cue.align === 'start' && cue.position === 0 && cue.line === 'auto') { cue.align = 'center'; cue.position = 'auto'; - cue.line = 'auto'; changed++; } } @@ -63,11 +64,13 @@ function handleCaptionTrackLoad(event) { centerAutoCaptions(track); } else { // A non-default track has mode 'disabled' at load time, so its cues are not parsed yet. Normalize - // them the first time the browser populates them (when the user selects the track and playback - // advances into a cue). + // them once the browser populates the cue list (when the user selects the track and playback advances + // into a cue). Only detach after cues actually exist, in case a cuechange fires before they load. const onCueChange = () => { - centerAutoCaptions(track); - track.removeEventListener('cuechange', onCueChange); + if (track.cues && track.cues.length > 0) { + centerAutoCaptions(track); + track.removeEventListener('cuechange', onCueChange); + } }; track.addEventListener('cuechange', onCueChange); } diff --git a/app/src/components/VideoPlayer.test.js b/app/src/components/VideoPlayer.test.js index 1aa42453..63981141 100644 --- a/app/src/components/VideoPlayer.test.js +++ b/app/src/components/VideoPlayer.test.js @@ -37,6 +37,19 @@ describe('centerAutoCaptions', () => { expect(track.cues[1].line).toBe(0); }); + test('does not touch a left/bottom cue that was deliberately given a line', () => { + // align:start + position:0 but an explicit line -> intentional placement, not the auto-caption + // signature (YouTube never sets line). Must be left alone. + const track = makeTrack([ + {align: 'start', position: 0, line: 0}, + ]); + + const changed = centerAutoCaptions(track); + + expect(changed).toBe(0); + expect(track.cues[0]).toEqual({align: 'start', position: 0, line: 0}); + }); + test('handles a track with no cues yet (mode disabled)', () => { expect(centerAutoCaptions({cues: null})).toBe(0); expect(centerAutoCaptions(null)).toBe(0);