From 24312405b503314bf79066f95c437b152b28824e Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Sun, 5 Feb 2023 21:44:00 -0800 Subject: [PATCH] Improve handling of fLaC --- codecs/codecs.js | 15 ++++++++------- tests/codecs.spec.js | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/codecs/codecs.js b/codecs/codecs.js index 997f468..7354497 100644 --- a/codecs/codecs.js +++ b/codecs/codecs.js @@ -127,9 +127,10 @@ 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; + // It seems to be "fLaC". + case 'flac': codecFrags.add(stream.codec_tag_string); break; default: - throw `Could not handle codec_name ${stream.codec_name}, ` + + throw `Could not handle audio codec_name ${stream.codec_name}, ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` + `Please file a bug https://github.com/codedread/bitjs/issues/new`; } @@ -141,16 +142,16 @@ export function getFullMIMEString(info) { case 'avc1': codecFrags.add(getAVC1CodecString(stream)); break; case 'vp09': codecFrags.add(getVP09CodecString(stream)); break; // We don't handle these as video streams with codecs, so skip them. - case 'png': - case 'mjpeg': - continue; + case 'png': continue; default: switch (stream.codec_name) { case 'h264': codecFrags.add(getAVC1CodecString(stream)); break; - case 'vp9': codecFrags.add(getVP09CodecString(stream)); break; case 'mpeg2video': codecFrags.add('mpeg2video'); break; + // Skip mjpeg as a video stream for the codecs string. + case 'mjpeg': break; + case 'vp9': codecFrags.add(getVP09CodecString(stream)); break; default: - throw `Could not handle codec_name ${stream.codec_name}, ` + + throw `Could not handle video codec_name ${stream.codec_name}, ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` + `Please file a bug https://github.com/codedread/bitjs/issues/new`; diff --git a/tests/codecs.spec.js b/tests/codecs.spec.js index 0e76354..efe9fb8 100644 --- a/tests/codecs.spec.js +++ b/tests/codecs.spec.js @@ -49,7 +49,7 @@ describe('codecs test suite', () => { it('detects FLAC', () => { expect(getShortMIMEString({ - format: { format_name: 'flac' }, + format: { format_name: 'flac', format_tag_string: 'fLaC' }, streams: [ { codec_type: 'audio'}, { codec_type: 'video' } ], })).equals('audio/flac'); }); @@ -341,6 +341,43 @@ describe('codecs test suite', () => { }); }); + describe('MP4 / FLAC', () => { + /** @type {ProbeInfo} */ + let info; + + beforeEach(() => { + info = { + format: { format_name: 'mov,mp4,m4a,3gp,3g2,mj2' }, + streams: [{ + codec_type: 'audio', + codec_tag_string: 'mp4a', + }], + }; + }); + + it('audio/mp4 handles fLaC', () => { + info.streams[0].codec_tag_string = 'fLaC'; + info.streams[0].codec_name = 'flac'; + expect(getFullMIMEString(info)) + .to.be.a('string') + .and.equals('audio/mp4; codecs="fLaC"'); + }); + + it('video/mp4 handles fLaC', () => { + const vInfo = structuredClone(info); + vInfo.streams[0].codec_tag_string = 'fLaC'; + vInfo.streams[0].codec_name = 'flac'; + vInfo.streams.push({ + codec_type: 'video', + codec_name: 'mjpeg', + }); + expect(getFullMIMEString(vInfo)) + .to.be.a('string') + .and.equals('video/mp4; codecs="fLaC"'); + + }); + }); + describe('Vorbis', () => { /** @type {ProbeInfo} */ let info;