1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 17:49:16 +02:00

Add support for FLAC in sniffer and codecs package.

This commit is contained in:
Jeff Schiller 2023-02-05 19:39:04 -08:00
parent 44beb992bf
commit 303c635b90
5 changed files with 17 additions and 4 deletions

View file

@ -51,9 +51,11 @@ export function getShortMIMEString(info) {
if (!info) throw `Invalid ProbeInfo`; if (!info) throw `Invalid ProbeInfo`;
if (!info.streams || info.streams.length === 0) throw `No streams in 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') { if (info.format.format_name === 'mp3') {
return 'audio/mpeg'; 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/. // Otherwise, any file with at least 1 video stream is considered video/.
@ -106,8 +108,9 @@ export function getShortMIMEString(info) {
export function getFullMIMEString(info) { export function getFullMIMEString(info) {
/** A string like 'video/mp4' */ /** A string like 'video/mp4' */
let contentType = `${getShortMIMEString(info)}`; let contentType = `${getShortMIMEString(info)}`;
// If MP3, just send back the type. // If MP3/FLAC, just send back the type.
if (contentType === 'audio/mpeg') { if (contentType === 'audio/mpeg'
|| contentType === 'audio/flac') {
return contentType; return contentType;
} }
@ -124,6 +127,7 @@ export function getFullMIMEString(info) {
case 'opus': codecFrags.add('opus'); break; case 'opus': codecFrags.add('opus'); break;
// I'm going off of what Chromium calls this one, with the dash. // I'm going off of what Chromium calls this one, with the dash.
case 'ac3': codecFrags.add('ac-3'); break; case 'ac3': codecFrags.add('ac-3'); break;
case 'flac': codecFrags.add('flac'); break;
default: default:
throw `Could not handle codec_name ${stream.codec_name}, ` + throw `Could not handle codec_name ${stream.codec_name}, ` +
`codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` +

View file

@ -31,6 +31,7 @@ const fileSignatures = {
'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]], 'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]],
// Audio/Video formats. // Audio/Video formats.
'application/ogg': [[0x4F, 0x67, 0x67, 0x53]], 'application/ogg': [[0x4F, 0x67, 0x67, 0x53]],
'audio/flac': [[0x66, 0x4C, 0x61, 0x43]],
'audio/mpeg': [[0xFF, 0xFB], [0xFF, 0xF3], [0xFF, 0xF2], [0x49, 0x44, 0x33]], 'audio/mpeg': [[0xFF, 0xFB], [0xFF, 0xF3], [0xFF, 0xF2], [0x49, 0x44, 0x33]],
}; };

View file

@ -1,6 +1,6 @@
{ {
"name": "@codedread/bitjs", "name": "@codedread/bitjs",
"version": "1.0.8", "version": "1.0.9",
"description": "Binary Tools for JavaScript", "description": "Binary Tools for JavaScript",
"homepage": "https://github.com/codedread/bitjs", "homepage": "https://github.com/codedread/bitjs",
"author": "Jeff Schiller", "author": "Jeff Schiller",

View file

@ -47,6 +47,13 @@ describe('codecs test suite', () => {
})).equals('audio/mpeg'); })).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', () => { it('detects AVI video', () => {
expect(getShortMIMEString({ expect(getShortMIMEString({
format: { format_name: 'avi' }, format: { format_name: 'avi' },

View file

@ -36,4 +36,5 @@ describe('bitjs.file.sniffer', () => {
it('OGG', () => { sniffTest('application/ogg', [0x4F, 0x67, 0x67, 0x53]); }); 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_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('TAR_2', () => { sniffTest('application/x-tar', [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00]); });
it('FLAC', () => { sniffTest('audio/flac', [0x66, 0x4C, 0x61, 0x43]); });
}); });