diff --git a/codecs/codecs.js b/codecs/codecs.js index 66d9107..997f468 100644 --- a/codecs/codecs.js +++ b/codecs/codecs.js @@ -51,9 +51,11 @@ export function getShortMIMEString(info) { if (!info) throw `Invalid ProbeInfo`; if (!info.streams || info.streams.length === 0) throw `No streams in ProbeInfo`; - // mp3 files are always considered audio/. + // mp3/flac files are always considered audio/ (even with mjpeg video streams). if (info.format.format_name === 'mp3') { return 'audio/mpeg'; + } else if (info.format.format_name === 'flac') { + return 'audio/flac'; } // Otherwise, any file with at least 1 video stream is considered video/. @@ -106,8 +108,9 @@ export function getShortMIMEString(info) { export function getFullMIMEString(info) { /** A string like 'video/mp4' */ let contentType = `${getShortMIMEString(info)}`; - // If MP3, just send back the type. - if (contentType === 'audio/mpeg') { + // If MP3/FLAC, just send back the type. + if (contentType === 'audio/mpeg' + || contentType === 'audio/flac') { return contentType; } @@ -124,6 +127,7 @@ export function getFullMIMEString(info) { case 'opus': codecFrags.add('opus'); break; // I'm going off of what Chromium calls this one, with the dash. case 'ac3': codecFrags.add('ac-3'); break; + case 'flac': codecFrags.add('flac'); break; default: throw `Could not handle codec_name ${stream.codec_name}, ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` + diff --git a/file/sniffer.js b/file/sniffer.js index 8506a51..85ff1db 100644 --- a/file/sniffer.js +++ b/file/sniffer.js @@ -31,6 +31,7 @@ const fileSignatures = { 'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]], // Audio/Video formats. 'application/ogg': [[0x4F, 0x67, 0x67, 0x53]], + 'audio/flac': [[0x66, 0x4C, 0x61, 0x43]], 'audio/mpeg': [[0xFF, 0xFB], [0xFF, 0xF3], [0xFF, 0xF2], [0x49, 0x44, 0x33]], }; diff --git a/package.json b/package.json index 2b8483b..d2d2b33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@codedread/bitjs", - "version": "1.0.8", + "version": "1.0.9", "description": "Binary Tools for JavaScript", "homepage": "https://github.com/codedread/bitjs", "author": "Jeff Schiller", diff --git a/tests/codecs.spec.js b/tests/codecs.spec.js index 8082556..0e76354 100644 --- a/tests/codecs.spec.js +++ b/tests/codecs.spec.js @@ -47,6 +47,13 @@ describe('codecs test suite', () => { })).equals('audio/mpeg'); }); + it('detects FLAC', () => { + expect(getShortMIMEString({ + format: { format_name: 'flac' }, + streams: [ { codec_type: 'audio'}, { codec_type: 'video' } ], + })).equals('audio/flac'); + }); + it('detects AVI video', () => { expect(getShortMIMEString({ format: { format_name: 'avi' }, diff --git a/tests/file-sniffer.spec.js b/tests/file-sniffer.spec.js index e8fc118..00224c9 100644 --- a/tests/file-sniffer.spec.js +++ b/tests/file-sniffer.spec.js @@ -36,4 +36,5 @@ describe('bitjs.file.sniffer', () => { it('OGG', () => { sniffTest('application/ogg', [0x4F, 0x67, 0x67, 0x53]); }); it('TAR_1', () => { sniffTest('application/x-tar', [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30]); }); it('TAR_2', () => { sniffTest('application/x-tar', [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00]); }); + it('FLAC', () => { sniffTest('audio/flac', [0x66, 0x4C, 0x61, 0x43]); }); });