diff --git a/changelog.d/4554.fixed.md b/changelog.d/4554.fixed.md new file mode 100644 index 0000000000..f113f2905a --- /dev/null +++ b/changelog.d/4554.fixed.md @@ -0,0 +1 @@ +Stories import no longer drops a caption cue whose identifier starts with WEBVTT (#4554) — thanks @felixpy! diff --git a/electron/src/shared/utils/importStory.js b/electron/src/shared/utils/importStory.js index 13b4899b92..def704bb75 100644 --- a/electron/src/shared/utils/importStory.js +++ b/electron/src/shared/utils/importStory.js @@ -124,11 +124,14 @@ function captionBlocks(text, webvtt) { .filter(Boolean); if (!lines.length) return false; const first = lines[0]; - if (/^WEBVTT(?:[ \t]|$)/.test(first)) return false; + const nextIsTiming = lines.length > 1 && isTimingLine(lines[1]); + // The file header starts with WEBVTT and has no timing line next. A cue + // identifier may start with the same word when the next line is the timing line. + if (/^WEBVTT(?:[ \t]|$)/.test(first) && !nextIsTiming) return false; if (/^NOTE(?:[ \t]|$)/.test(first)) return false; // A cue starts with a timing line or an identifier immediately followed // by one. This also permits STYLE and REGION as cue identifiers. - return isTimingLine(first) || (lines.length > 1 && isTimingLine(lines[1])); + return isTimingLine(first) || nextIsTiming; }); } diff --git a/electron/src/shared/utils/importStory.test.js b/electron/src/shared/utils/importStory.test.js index eaf2a8f368..cb490e5414 100644 --- a/electron/src/shared/utils/importStory.test.js +++ b/electron/src/shared/utils/importStory.test.js @@ -141,6 +141,24 @@ describe('parseSrt', () => { expect(parseSrt(vtt)).toBe('Hello world.\nStill here.'); } }); + it('keeps a cue whose identifier starts with WEBVTT', () => { + for (const identifier of ['WEBVTT', 'WEBVTT intro', 'WEBVTT\tname']) { + const vtt = + `WEBVTT\n\n${identifier}\n00:00:01.000 --> 00:00:02.000\nFirst cue\n\n` + + 'second\n00:00:02.000 --> 00:00:03.000\nSecond cue\n'; + expect(parseSrt(vtt)).toBe('First cue\nSecond cue'); + } + expect(parseSrt('WEBVTT\n00:00:01.000 --> 00:00:02.000\nHello\n')).toBe('Hello'); + }); + it('still drops a WebVTT header that carries metadata and no cue timing', () => { + for (const header of ['WEBVTT', 'WEBVTT Kind: captions', 'WEBVTT\tKind: captions']) { + const vtt = `${header}\nLanguage: en\n\n00:00:01.000 --> 00:00:02.000\nHello\n`; + expect(parseSrt(vtt)).toBe('Hello'); + } + expect(parseSrt('WEBVTT Kind: captions\n\n00:00:01.000 --> 00:00:02.000\nHello\n')).toBe( + 'Hello', + ); + }); it('keeps NOTE when it is the spoken dialogue', () => { expect( parseSrt('WEBVTT\n\n00:01.000 --> 00:02.000\nNOTE this is spoken\nSTYLE\nREGION\n'), @@ -184,4 +202,11 @@ describe('importToText', () => { expect(importToText('captions.vtt', vtt)).toBe('hey everyone'); expect(importToText('captions.VTT', vtt)).toBe('hey everyone'); }); + it('keeps a WEBVTT cue identifier for .vtt and .srt filenames', () => { + const vtt = + 'WEBVTT\n\nWEBVTT intro\n00:00:01.000 --> 00:00:02.000\nFirst cue\n\n' + + 'second\n00:00:02.000 --> 00:00:03.000\nSecond cue\n'; + expect(importToText('captions.vtt', vtt)).toBe('First cue\nSecond cue'); + expect(importToText('captions.srt', vtt)).toBe('First cue\nSecond cue'); + }); });