Skip to content

Commit

Permalink
Fix for m4a files as audio/mp4. Up-rev to 1.1.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Sep 30, 2023
1 parent a7d3294 commit e072b35
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [1.1.2] - 2023-09-30

- codecs: Handle m4a files as audio/mp4.

## [1.1.1] - 2023-06-21

- Fix missing RarVM import in unrar.js.
Expand Down
5 changes: 5 additions & 0 deletions codecs/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export function getShortMIMEString(info) {
return 'audio/flac';
}

// M4A files are specifically audio/mp4.
if (info?.format?.filename?.toLowerCase().endsWith('.m4a')) {
return 'audio/mp4';
}

// Otherwise, any file with at least 1 video stream is considered video/.
// Otherwise, any file with at least 1 audio stream is considered audio/.
const type = info.streams.some(s => s.codec_type === 'video') ?
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codedread/bitjs",
"version": "1.1.1",
"version": "1.1.2",
"description": "Binary Tools for JavaScript",
"homepage": "https://github.com/codedread/bitjs",
"author": "Jeff Schiller",
Expand Down
41 changes: 28 additions & 13 deletions tests/codecs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ describe('codecs test suite', () => {
})).equals('audio/mpeg');
});

it('detects M4A audio', () => {
expect(getShortMIMEString({
format: { filename: 'sample.m4a', format_name: 'mov,mp4,m4a,3gp,3g2,mj2' },
streams: [ { codec_type: 'video', }, { codec_type: 'audio' } ],
})).equals('audio/mp4');
});

it('detects MP4 video', () => {
expect(getShortMIMEString({
format: { format_name: 'mov,mp4,m4a,3gp,3g2,mj2' },
Expand Down Expand Up @@ -306,24 +313,21 @@ describe('codecs test suite', () => {

describe('MP4A / AAC', () => {
/** @type {ProbeInfo} */
let info;

beforeEach(() => {
info = {
format: { format_name: 'mov,mp4,m4a,3gp,3g2,mj2' },
streams: [{
codec_type: 'audio',
codec_tag_string: 'mp4a',
}],
};
});
let baseInfo = {
format: { format_name: 'mov,mp4,m4a,3gp,3g2,mj2' },
streams: [{
codec_type: 'audio',
codec_tag_string: 'mp4a',
}],
};

it('throws when unknown', () => {
expect(() => getFullMIMEString(info)).to.throw();
expect(() => getFullMIMEString(baseInfo)).to.throw();
});

describe('Profile tests', () => {
it('recognizes AAC-LC', () => {
const info = structuredClone(baseInfo);
info.streams[0].profile = 'LC';
expect(getFullMIMEString(info))
.to.be.a('string')
Expand All @@ -332,13 +336,24 @@ describe('codecs test suite', () => {
});

it('handles codec_name=aac but no codec_tag_string', () => {
const info = structuredClone(baseInfo);
info.streams[0].profile = 'LC';
info.streams[0].codec_tag_string = '[0][0][0][0]';
info.streams[0].codec_name = 'aac';
expect(getFullMIMEString(info))
.to.be.a('string')
.and.equals('audio/mp4; codecs="mp4a.40.2"');
});

it('handles m4a file with MJPEG stream', () => {
const info = structuredClone(baseInfo);
info.format.filename = 'sample.m4a';
info.streams[0].profile = 'LC';
info.streams.push({codec_name: 'mjpeg', codec_type: 'video', profile: 'Baseline'});
expect(getFullMIMEString(info))
.to.be.a('string')
.and.equals('audio/mp4; codecs="mp4a.40.2"');
})
});

describe('MP4 / FLAC', () => {
Expand Down Expand Up @@ -374,7 +389,7 @@ describe('codecs test suite', () => {
expect(getFullMIMEString(vInfo))
.to.be.a('string')
.and.equals('video/mp4; codecs="flac"');

});
});

Expand Down

0 comments on commit e072b35

Please sign in to comment.