Skip to content

Commit

Permalink
Add proper support for AVI and WAV files. Up-rev to 1.1.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Oct 16, 2023
1 parent 97cfb3b commit 5a00a3f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,13 @@

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

## [1.1.3] - 2023-10-15

### Changed

- codecs: Add support for WAV files to getShortMIMEString() and getFullMIMEString().
- codecs: Fix support for AVI files in getFullMIMEString().

## [1.1.2] - 2023-09-30

### Changed
Expand Down
30 changes: 17 additions & 13 deletions codecs/codecs.js
Expand Up @@ -42,6 +42,17 @@
* @property {ProbeFormat} format
*/

/**
* Maps the ffprobe format.format_name string to a short MIME type.
* @type {Object<string, string>}
*/
const FORMAT_NAME_TO_SHORT_TYPE = {
'avi': 'video/x-msvideo',
'flac': 'audio/flac',
'mp3': 'audio/mpeg',
'wav': 'audio/wav',
}

/**
* TODO: Reconcile this with file/sniffer.js findMimeType() which does signature matching.
* @param {ProbeInfo} info
Expand All @@ -51,11 +62,9 @@ export function getShortMIMEString(info) {
if (!info) throw `Invalid ProbeInfo`;
if (!info.streams || info.streams.length === 0) throw `No streams in ProbeInfo`;

// 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';
const formatName = info?.format?.format_name;
if (formatName && Object.keys(FORMAT_NAME_TO_SHORT_TYPE).includes(formatName)) {
return FORMAT_NAME_TO_SHORT_TYPE[formatName];
}

// M4A files are specifically audio/mp4.
Expand All @@ -75,10 +84,7 @@ export function getShortMIMEString(info) {

/** @type {string} */
let subType;
switch (info.format.format_name) {
case 'avi':
subType = 'x-msvideo';
break;
switch (formatName) {
case 'mpeg':
subType = 'mpeg';
break;
Expand All @@ -93,7 +99,7 @@ export function getShortMIMEString(info) {
subType = 'webm';
break;
default:
throw `Cannot handle format ${info.format.format_name} yet. ` +
throw `Cannot handle format ${formatName} yet. ` +
`Please file a bug https://github.com/codedread/bitjs/issues/new`;
}

Expand All @@ -113,9 +119,7 @@ export function getShortMIMEString(info) {
export function getFullMIMEString(info) {
/** A string like 'video/mp4' */
let contentType = `${getShortMIMEString(info)}`;
// If MP3/FLAC, just send back the type.
if (contentType === 'audio/mpeg'
|| contentType === 'audio/flac') {
if (Object.values(FORMAT_NAME_TO_SHORT_TYPE).includes(contentType)) {
return contentType;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@codedread/bitjs",
"version": "1.1.2",
"version": "1.1.3",
"description": "Binary Tools for JavaScript",
"homepage": "https://github.com/codedread/bitjs",
"author": "Jeff Schiller",
Expand Down
36 changes: 36 additions & 0 deletions tests/codecs.spec.js
Expand Up @@ -455,4 +455,40 @@ describe('codecs test suite', () => {
.and.equals('audio/webm; codecs="ac-3"');
});
});

describe('AVI', () => {
it('detects AVI', () => {
/** @type {ProbeInfo} */
let info = {
format: { format_name: 'avi' },
streams: [
{ codec_type: 'video', codec_name: 'mpeg4', codec_tag_string: 'XVID' },
{ codec_type: 'audio', codec_name: 'mp3' },
{ codec_type: 'audio', codec_name: 'mp3' },
],
};
expect(getShortMIMEString(info))
.to.be.a('string')
.and.equals('video/x-msvideo');
expect(getFullMIMEString(info))
.to.be.a('string')
.and.equals('video/x-msvideo');
});
});

describe('WAV', () => {
it('detects WAV', () => {
/** @type {ProbeInfo} */
let info = {
format: { format_name: 'wav' },
streams: [ { codec_type: 'audio', codec_name: 'pcm_s16le' } ],
};
expect(getShortMIMEString(info))
.to.be.a('string')
.and.equals('audio/wav');
expect(getFullMIMEString(info))
.to.be.a('string')
.and.equals('audio/wav');
});
});
});

0 comments on commit 5a00a3f

Please sign in to comment.