Skip to content

Commit

Permalink
Support aac & mpeg-2 (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit authored and sindresorhus committed Dec 22, 2019
1 parent 0277f45 commit 321a50f
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 79 deletions.
7 changes: 5 additions & 2 deletions core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ declare namespace core {
| 'spx'
| 'ogx'
| 'arrow'
| 'shp';
| 'shp'
| 'aac'
| 'mp1';

type MimeType =
| 'image/jpeg'
Expand Down Expand Up @@ -233,7 +235,8 @@ declare namespace core {
| 'image/x-fujifilm-raf'
| 'video/x-m4v'
| 'video/3gpp2'
| 'application/x-esri-shape';
| 'application/x-esri-shape'
| 'audio/aac'

interface FileTypeResult {
/**
Expand Down
108 changes: 39 additions & 69 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,35 +985,6 @@ async function fromTokenizer(tokenizer) {
};
}

// Check for MPEG header
if (
check([0x49, 0x44, 0x33]) || // ID3 header
check([0xFF, 0xE2], {mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 3 header
) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}

if (
check([0xFF, 0xE4], {mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 2 header
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}

if (
check([0xFF, 0xF8], {mask: [0xFF, 0xFC]}) // MPEG 2 layer 0 using ADTS
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}

if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
// JPEG-2000 family

Expand Down Expand Up @@ -1045,15 +1016,6 @@ async function fromTokenizer(tokenizer) {
}
}

if (
check([0xFF, 0xF0], {mask: [0xFF, 0xFC]}) // MPEG 4 layer 0 using ADTS
) {
return {
ext: 'mp4',
mime: 'audio/mpeg'
};
}

// -- Unsafe signatures --

if (
Expand Down Expand Up @@ -1193,40 +1155,48 @@ async function fromTokenizer(tokenizer) {

// Check for MPEG header at different starting offsets
for (let start = 0; start < 2 && start < (buffer.length - 16); start++) {
if (
check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 3 header
) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}
// Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
if (buffer.length >= start + 2 && check([0xFF, 0xE0], {offset: start, mask: [0xFF, 0xE0]})) {
if (check([0x10], {offset: start + 1, mask: [0x16]})) {
// Check for (ADTS) MPEG-2
if (check([0x08], {offset: start + 1, mask: [0x08]})) {
return {
ext: 'aac',
mime: 'audio/aac'
};
}

if (
check([0xFF, 0xE4], {offset: start, mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 2 header
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
// Must be (ADTS) MPEG-4
return {
ext: 'aac',
mime: 'audio/aac'
};
}

if (
check([0xFF, 0xF8], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 2 layer 0 using ADTS
) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
// MPEG 1 or 2 Layer 3 header
// Check for MPEG layer 3
if (check([0x02], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}

if (
check([0xFF, 0xF0], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 4 layer 0 using ADTS
) {
return {
ext: 'mp4',
mime: 'audio/mpeg'
};
// Check for MPEG layer 2
if (check([0x04], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}

// Check for MPEG layer 1
if (check([0x06], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp1',
mime: 'audio/mpeg'
};
}
}
}
}
Expand Down
File renamed without changes.
Binary file added fixture/fixture-adts-mpeg4-2.aac
Binary file not shown.
File renamed without changes.
Binary file removed fixture/fixture-offset1.mp3
Binary file not shown.
Binary file added fixture/fixture.mp1
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@
"dcm",
"mpc",
"arrow",
"shp"
"shp",
"aac",
"mp1"
],
"devDependencies": {
"@types/node": "^12.7.2",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Returns a set of supported MIME types.
- [`mov`](https://en.wikipedia.org/wiki/QuickTime_File_Format)
- [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave)
- [`mpg`](https://en.wikipedia.org/wiki/MPEG-1)
- [`mp1`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_I) - MPEG-1 Audio Layer I
- [`mp2`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_II)
- [`mp3`](https://en.wikipedia.org/wiki/MP3)
- [`ogg`](https://en.wikipedia.org/wiki/Ogg)
Expand Down Expand Up @@ -271,6 +272,7 @@ Returns a set of supported MIME types.
- [`mie`](https://en.wikipedia.org/wiki/Sidecar_file) - Dedicated meta information format which supports storage of binary as well as textual meta information
- [`shp`](https://en.wikipedia.org/wiki/Shapefile) - Geospatial vector data format
- [`arrow`](https://arrow.apache.org) - Columnar format for tables of data
- [`aac`](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) - Advanced Audio Coding

*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*

Expand Down
7 changes: 5 additions & 2 deletions supported.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ module.exports = {
'ogx',
'mpc',
'arrow',
'shp'
'shp',
'aac',
'mp1'
],
mimeTypes: [
'image/jpeg',
Expand Down Expand Up @@ -232,6 +234,7 @@ module.exports = {
'image/x-fujifilm-raf',
'video/x-m4v',
'video/3gpp2',
'application/x-esri-shape'
'application/x-esri-shape',
'audio/aac'
]
};
12 changes: 7 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const types = supported.extensions.filter(ext => !missingTests.includes(ext));
// Define an entry here only if the fixture has a different
// name than `fixture` or if you want multiple fixtures
const names = {
aac: [
'fixture-adts-mpeg2',
'fixture-adts-mpeg4',
'fixture-adts-mpeg4-2'
],
arw: [
'fixture',
'fixture2',
Expand Down Expand Up @@ -59,12 +64,10 @@ const names = {
],
mp2: [
'fixture',
'fixture-mpa',
'fixture-faac-adts'
'fixture-mpa'
],
mp3: [
'fixture',
'fixture-offset1',
'fixture-mp2l3',
'fixture-ffe3'
],
Expand All @@ -73,8 +76,7 @@ const names = {
'fixture-isom',
'fixture-isomv2',
'fixture-mp4v2',
'fixture-dash',
'fixture-aac-adts'
'fixture-dash'
],
tif: [
'fixture-big-endian',
Expand Down

0 comments on commit 321a50f

Please sign in to comment.