mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 17:49:16 +02:00
Add proper support for AVI and WAV files. Up-rev to 1.1.3.
This commit is contained in:
parent
97cfb3b388
commit
5a00a3fdd0
4 changed files with 61 additions and 14 deletions
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
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
|
## [1.1.2] - 2023-09-30
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
@ -42,6 +42,17 @@
|
||||||
* @property {ProbeFormat} format
|
* @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.
|
* TODO: Reconcile this with file/sniffer.js findMimeType() which does signature matching.
|
||||||
* @param {ProbeInfo} info
|
* @param {ProbeInfo} info
|
||||||
|
@ -51,11 +62,9 @@ export function getShortMIMEString(info) {
|
||||||
if (!info) throw `Invalid ProbeInfo`;
|
if (!info) throw `Invalid ProbeInfo`;
|
||||||
if (!info.streams || info.streams.length === 0) throw `No streams in 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).
|
const formatName = info?.format?.format_name;
|
||||||
if (info.format.format_name === 'mp3') {
|
if (formatName && Object.keys(FORMAT_NAME_TO_SHORT_TYPE).includes(formatName)) {
|
||||||
return 'audio/mpeg';
|
return FORMAT_NAME_TO_SHORT_TYPE[formatName];
|
||||||
} else if (info.format.format_name === 'flac') {
|
|
||||||
return 'audio/flac';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// M4A files are specifically audio/mp4.
|
// M4A files are specifically audio/mp4.
|
||||||
|
@ -75,10 +84,7 @@ export function getShortMIMEString(info) {
|
||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
let subType;
|
let subType;
|
||||||
switch (info.format.format_name) {
|
switch (formatName) {
|
||||||
case 'avi':
|
|
||||||
subType = 'x-msvideo';
|
|
||||||
break;
|
|
||||||
case 'mpeg':
|
case 'mpeg':
|
||||||
subType = 'mpeg';
|
subType = 'mpeg';
|
||||||
break;
|
break;
|
||||||
|
@ -93,7 +99,7 @@ export function getShortMIMEString(info) {
|
||||||
subType = 'webm';
|
subType = 'webm';
|
||||||
break;
|
break;
|
||||||
default:
|
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`;
|
`Please file a bug https://github.com/codedread/bitjs/issues/new`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,9 +119,7 @@ export function getShortMIMEString(info) {
|
||||||
export function getFullMIMEString(info) {
|
export function getFullMIMEString(info) {
|
||||||
/** A string like 'video/mp4' */
|
/** A string like 'video/mp4' */
|
||||||
let contentType = `${getShortMIMEString(info)}`;
|
let contentType = `${getShortMIMEString(info)}`;
|
||||||
// If MP3/FLAC, just send back the type.
|
if (Object.values(FORMAT_NAME_TO_SHORT_TYPE).includes(contentType)) {
|
||||||
if (contentType === 'audio/mpeg'
|
|
||||||
|| contentType === 'audio/flac') {
|
|
||||||
return contentType;
|
return contentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@codedread/bitjs",
|
"name": "@codedread/bitjs",
|
||||||
"version": "1.1.2",
|
"version": "1.1.3",
|
||||||
"description": "Binary Tools for JavaScript",
|
"description": "Binary Tools for JavaScript",
|
||||||
"homepage": "https://github.com/codedread/bitjs",
|
"homepage": "https://github.com/codedread/bitjs",
|
||||||
"author": "Jeff Schiller",
|
"author": "Jeff Schiller",
|
||||||
|
|
|
@ -455,4 +455,40 @@ describe('codecs test suite', () => {
|
||||||
.and.equals('audio/webm; codecs="ac-3"');
|
.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');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue