Skip to content

Commit 5a00a3f

Browse files
committed
Add proper support for AVI and WAV files. Up-rev to 1.1.3.
1 parent 97cfb3b commit 5a00a3f

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

Diff for: CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

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

5+
## [1.1.3] - 2023-10-15
6+
7+
### Changed
8+
9+
- codecs: Add support for WAV files to getShortMIMEString() and getFullMIMEString().
10+
- codecs: Fix support for AVI files in getFullMIMEString().
11+
512
## [1.1.2] - 2023-09-30
613

714
### Changed

Diff for: codecs/codecs.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
* @property {ProbeFormat} format
4343
*/
4444

45+
/**
46+
* Maps the ffprobe format.format_name string to a short MIME type.
47+
* @type {Object<string, string>}
48+
*/
49+
const FORMAT_NAME_TO_SHORT_TYPE = {
50+
'avi': 'video/x-msvideo',
51+
'flac': 'audio/flac',
52+
'mp3': 'audio/mpeg',
53+
'wav': 'audio/wav',
54+
}
55+
4556
/**
4657
* TODO: Reconcile this with file/sniffer.js findMimeType() which does signature matching.
4758
* @param {ProbeInfo} info
@@ -51,11 +62,9 @@ export function getShortMIMEString(info) {
5162
if (!info) throw `Invalid ProbeInfo`;
5263
if (!info.streams || info.streams.length === 0) throw `No streams in ProbeInfo`;
5364

54-
// mp3/flac files are always considered audio/ (even with mjpeg video streams).
55-
if (info.format.format_name === 'mp3') {
56-
return 'audio/mpeg';
57-
} else if (info.format.format_name === 'flac') {
58-
return 'audio/flac';
65+
const formatName = info?.format?.format_name;
66+
if (formatName && Object.keys(FORMAT_NAME_TO_SHORT_TYPE).includes(formatName)) {
67+
return FORMAT_NAME_TO_SHORT_TYPE[formatName];
5968
}
6069

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

7685
/** @type {string} */
7786
let subType;
78-
switch (info.format.format_name) {
79-
case 'avi':
80-
subType = 'x-msvideo';
81-
break;
87+
switch (formatName) {
8288
case 'mpeg':
8389
subType = 'mpeg';
8490
break;
@@ -93,7 +99,7 @@ export function getShortMIMEString(info) {
9399
subType = 'webm';
94100
break;
95101
default:
96-
throw `Cannot handle format ${info.format.format_name} yet. ` +
102+
throw `Cannot handle format ${formatName} yet. ` +
97103
`Please file a bug https://github.com/codedread/bitjs/issues/new`;
98104
}
99105

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

Diff for: package.json

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

Diff for: tests/codecs.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,40 @@ describe('codecs test suite', () => {
455455
.and.equals('audio/webm; codecs="ac-3"');
456456
});
457457
});
458+
459+
describe('AVI', () => {
460+
it('detects AVI', () => {
461+
/** @type {ProbeInfo} */
462+
let info = {
463+
format: { format_name: 'avi' },
464+
streams: [
465+
{ codec_type: 'video', codec_name: 'mpeg4', codec_tag_string: 'XVID' },
466+
{ codec_type: 'audio', codec_name: 'mp3' },
467+
{ codec_type: 'audio', codec_name: 'mp3' },
468+
],
469+
};
470+
expect(getShortMIMEString(info))
471+
.to.be.a('string')
472+
.and.equals('video/x-msvideo');
473+
expect(getFullMIMEString(info))
474+
.to.be.a('string')
475+
.and.equals('video/x-msvideo');
476+
});
477+
});
478+
479+
describe('WAV', () => {
480+
it('detects WAV', () => {
481+
/** @type {ProbeInfo} */
482+
let info = {
483+
format: { format_name: 'wav' },
484+
streams: [ { codec_type: 'audio', codec_name: 'pcm_s16le' } ],
485+
};
486+
expect(getShortMIMEString(info))
487+
.to.be.a('string')
488+
.and.equals('audio/wav');
489+
expect(getFullMIMEString(info))
490+
.to.be.a('string')
491+
.and.equals('audio/wav');
492+
});
493+
});
458494
});

0 commit comments

Comments
 (0)