Skip to content

Commit 303c635

Browse files
committed
Add support for FLAC in sniffer and codecs package.
1 parent 44beb99 commit 303c635

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

codecs/codecs.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ export function getShortMIMEString(info) {
5151
if (!info) throw `Invalid ProbeInfo`;
5252
if (!info.streams || info.streams.length === 0) throw `No streams in ProbeInfo`;
5353

54-
// mp3 files are always considered audio/.
54+
// mp3/flac files are always considered audio/ (even with mjpeg video streams).
5555
if (info.format.format_name === 'mp3') {
5656
return 'audio/mpeg';
57+
} else if (info.format.format_name === 'flac') {
58+
return 'audio/flac';
5759
}
5860

5961
// Otherwise, any file with at least 1 video stream is considered video/.
@@ -106,8 +108,9 @@ export function getShortMIMEString(info) {
106108
export function getFullMIMEString(info) {
107109
/** A string like 'video/mp4' */
108110
let contentType = `${getShortMIMEString(info)}`;
109-
// If MP3, just send back the type.
110-
if (contentType === 'audio/mpeg') {
111+
// If MP3/FLAC, just send back the type.
112+
if (contentType === 'audio/mpeg'
113+
|| contentType === 'audio/flac') {
111114
return contentType;
112115
}
113116

@@ -124,6 +127,7 @@ export function getFullMIMEString(info) {
124127
case 'opus': codecFrags.add('opus'); break;
125128
// I'm going off of what Chromium calls this one, with the dash.
126129
case 'ac3': codecFrags.add('ac-3'); break;
130+
case 'flac': codecFrags.add('flac'); break;
127131
default:
128132
throw `Could not handle codec_name ${stream.codec_name}, ` +
129133
`codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` +

file/sniffer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const fileSignatures = {
3131
'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]],
3232
// Audio/Video formats.
3333
'application/ogg': [[0x4F, 0x67, 0x67, 0x53]],
34+
'audio/flac': [[0x66, 0x4C, 0x61, 0x43]],
3435
'audio/mpeg': [[0xFF, 0xFB], [0xFF, 0xF3], [0xFF, 0xF2], [0x49, 0x44, 0x33]],
3536
};
3637

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codedread/bitjs",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Binary Tools for JavaScript",
55
"homepage": "https://github.com/codedread/bitjs",
66
"author": "Jeff Schiller",

tests/codecs.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ describe('codecs test suite', () => {
4747
})).equals('audio/mpeg');
4848
});
4949

50+
it('detects FLAC', () => {
51+
expect(getShortMIMEString({
52+
format: { format_name: 'flac' },
53+
streams: [ { codec_type: 'audio'}, { codec_type: 'video' } ],
54+
})).equals('audio/flac');
55+
});
56+
5057
it('detects AVI video', () => {
5158
expect(getShortMIMEString({
5259
format: { format_name: 'avi' },

tests/file-sniffer.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ describe('bitjs.file.sniffer', () => {
3636
it('OGG', () => { sniffTest('application/ogg', [0x4F, 0x67, 0x67, 0x53]); });
3737
it('TAR_1', () => { sniffTest('application/x-tar', [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30]); });
3838
it('TAR_2', () => { sniffTest('application/x-tar', [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00]); });
39+
it('FLAC', () => { sniffTest('audio/flac', [0x66, 0x4C, 0x61, 0x43]); });
3940
});

0 commit comments

Comments
 (0)