From 21ed7633875b71881ad2b8b403291c3ee3ea2644 Mon Sep 17 00:00:00 2001 From: Aleksandr Hovhannisyan Date: Sat, 7 Sep 2024 12:07:33 -0500 Subject: [PATCH] Add support for WebVTT (#658) --- core.d.ts | 2 ++ core.js | 12 ++++++++++++ fixture/fixture-vtt-eof.vtt | 1 + fixture/fixture-vtt-linebreak.vtt | 4 ++++ fixture/fixture-vtt-space.vtt | 2 ++ fixture/fixture-vtt-tab.vtt | 2 ++ package.json | 3 ++- readme.md | 1 + supported.js | 2 ++ test.js | 6 ++++++ 10 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 fixture/fixture-vtt-eof.vtt create mode 100644 fixture/fixture-vtt-linebreak.vtt create mode 100644 fixture/fixture-vtt-space.vtt create mode 100644 fixture/fixture-vtt-tab.vtt diff --git a/core.d.ts b/core.d.ts index 16179ec9..1f1ad94f 100644 --- a/core.d.ts +++ b/core.d.ts @@ -165,6 +165,7 @@ export type FileExtension = | 'icc' | 'fbx' | 'vsdx' + | 'vtt' ; // eslint-disable-line semi-style export type MimeType = @@ -270,6 +271,7 @@ export type MimeType = | 'audio/x-musepack' | 'text/calendar' | 'text/vcard' + | 'text/vtt' | 'model/gltf-binary' | 'application/vnd.tcpdump.pcap' | 'audio/x-dsf' // Non-standard diff --git a/core.js b/core.js index 9444b66a..81787c3d 100644 --- a/core.js +++ b/core.js @@ -1139,6 +1139,18 @@ export class FileTypeParser { } } + if ( + this.checkString('WEBVTT') + && ( + // One of LF, CR, tab, space, or end of file must follow "WEBVTT" per the spec (see `fixture/fixture-vtt-*.vtt` for examples). Note that `\0` is technically the null character (there is no such thing as an EOF character). However, checking for `\0` gives us the same result as checking for the end of the stream. + (['\n', '\r', '\t', ' ', '\0'].some(char7 => this.checkString(char7, {offset: 6})))) + ) { + return { + ext: 'vtt', + mime: 'text/vtt', + }; + } + // -- 8-byte signatures -- if (this.check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) { diff --git a/fixture/fixture-vtt-eof.vtt b/fixture/fixture-vtt-eof.vtt new file mode 100644 index 00000000..af1827dd --- /dev/null +++ b/fixture/fixture-vtt-eof.vtt @@ -0,0 +1 @@ +WEBVTT \ No newline at end of file diff --git a/fixture/fixture-vtt-linebreak.vtt b/fixture/fixture-vtt-linebreak.vtt new file mode 100644 index 00000000..ed33e96d --- /dev/null +++ b/fixture/fixture-vtt-linebreak.vtt @@ -0,0 +1,4 @@ +WEBVTT + +00:11.000 --> 00:13.000 +Test WEBVTT prefix followed by a line break \ No newline at end of file diff --git a/fixture/fixture-vtt-space.vtt b/fixture/fixture-vtt-space.vtt new file mode 100644 index 00000000..e3f02d79 --- /dev/null +++ b/fixture/fixture-vtt-space.vtt @@ -0,0 +1,2 @@ +WEBVTT 00:11.000 --> 00:13.000 +Test WEBVTT prefix followed by a space \ No newline at end of file diff --git a/fixture/fixture-vtt-tab.vtt b/fixture/fixture-vtt-tab.vtt new file mode 100644 index 00000000..5349f53d --- /dev/null +++ b/fixture/fixture-vtt-tab.vtt @@ -0,0 +1,2 @@ +WEBVTT 00:11.000 --> 00:13.000 +Test WEBVTT prefix followed by a tab \ No newline at end of file diff --git a/package.json b/package.json index 08f1ca3b..f86f5232 100644 --- a/package.json +++ b/package.json @@ -215,7 +215,8 @@ "avro", "icc", "fbx", - "vsdx" + "vsdx", + "vtt" ], "dependencies": { "get-stream": "^9.0.1", diff --git a/readme.md b/readme.md index 21e540ae..5ef754cf 100644 --- a/readme.md +++ b/readme.md @@ -507,6 +507,7 @@ console.log(fileType); - [`vcf`](https://en.wikipedia.org/wiki/VCard) - vCard - [`voc`](https://wiki.multimedia.cx/index.php/Creative_Voice) - Creative Voice File - [`vsdx`](https://en.wikipedia.org/wiki/Microsoft_Visio) - Microsoft Visio File +- [`vtt`](https://en.wikipedia.org/wiki/WebVTT) - WebVTT File (for video captions) - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - WebAssembly intermediate compiled format - [`wav`](https://en.wikipedia.org/wiki/WAV) - Waveform Audio file - [`webm`](https://en.wikipedia.org/wiki/WebM) - Web video file diff --git a/supported.js b/supported.js index 24feb8fe..b64f5b59 100644 --- a/supported.js +++ b/supported.js @@ -152,6 +152,7 @@ export const extensions = [ 'icc', 'fbx', 'vsdx', + 'vtt', ]; export const mimeTypes = [ @@ -257,6 +258,7 @@ export const mimeTypes = [ 'audio/x-musepack', 'text/calendar', 'text/vcard', + 'text/vtt', 'model/gltf-binary', 'application/vnd.tcpdump.pcap', 'audio/x-dsf', // Non-standard diff --git a/test.js b/test.js index 3d4761a9..3b937533 100644 --- a/test.js +++ b/test.js @@ -261,6 +261,12 @@ const names = { 'fixture-vsdx', 'fixture-vstx', ], + vtt: [ + 'fixture-vtt-linebreak', + 'fixture-vtt-space', + 'fixture-vtt-tab', + 'fixture-vtt-eof', + ], }; // Define an entry here only if the file type has potential