Skip to content

Commit

Permalink
Add support for WebVTT (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrHovhannisyan authored Sep 7, 2024
1 parent 988bf4b commit 21ed763
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export type FileExtension =
| 'icc'
| 'fbx'
| 'vsdx'
| 'vtt'
; // eslint-disable-line semi-style

export type MimeType =
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
1 change: 1 addition & 0 deletions fixture/fixture-vtt-eof.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WEBVTT
4 changes: 4 additions & 0 deletions fixture/fixture-vtt-linebreak.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
WEBVTT
00:11.000 --> 00:13.000
<v Speaker Name>Test WEBVTT prefix followed by a line break
2 changes: 2 additions & 0 deletions fixture/fixture-vtt-space.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WEBVTT 00:11.000 --> 00:13.000
<v Speaker Name>Test WEBVTT prefix followed by a space
2 changes: 2 additions & 0 deletions fixture/fixture-vtt-tab.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WEBVTT 00:11.000 --> 00:13.000
<v Speaker Name>Test WEBVTT prefix followed by a tab
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@
"avro",
"icc",
"fbx",
"vsdx"
"vsdx",
"vtt"
],
"dependencies": {
"get-stream": "^9.0.1",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions supported.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const extensions = [
'icc',
'fbx',
'vsdx',
'vtt',
];

export const mimeTypes = [
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21ed763

Please sign in to comment.