1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 17:49:16 +02:00

Release v1.07: Add MPEG2 video codec support.

This commit is contained in:
Jeff Schiller 2023-02-01 21:47:01 -08:00
parent 726feee48f
commit a24524e772
4 changed files with 53 additions and 17 deletions

View file

@ -23,11 +23,30 @@ or
$ yarn add @codedread/bitjs $ yarn add @codedread/bitjs
``` ```
## bitjs.archive ### Using in Node
This module is an ES Module, which should work as expected in other projects using ES Modules. However,
if you are using a project that uses CommonJs modules, it's a little tricker to use. One valid example
of this is if a TypeScript project compiles to CommonJS, it will try to turn imports into require()
statements, which will break. The fix for this (unfortunately) is to update your tsconfig.json:
```json
"moduleResolution": "Node16",
```
and use a Dynamic Import:
```javascript
const { getFullMIMEString } = await import('@codedread/bitjs');
```
## Packages
### bitjs.archive
This package includes objects for unarchiving binary data in popular archive formats (zip, rar, tar) providing unzip, unrar and untar capabilities via JavaScript in the browser. A prototype version of a compressor that creates Zip files is also present. The decompression/compression actually happens inside a Web Worker. This package includes objects for unarchiving binary data in popular archive formats (zip, rar, tar) providing unzip, unrar and untar capabilities via JavaScript in the browser. A prototype version of a compressor that creates Zip files is also present. The decompression/compression actually happens inside a Web Worker.
### Decompressing #### Decompressing
```javascript ```javascript
import { Unzipper } from './bitjs/archive/decompress.js'; import { Unzipper } from './bitjs/archive/decompress.js';
@ -69,7 +88,7 @@ unzipper.update(anArrayBufferWithMoreBytes);
unzipper.update(anArrayBufferWithYetMoreBytes); unzipper.update(anArrayBufferWithYetMoreBytes);
``` ```
### Compressing #### Compressing
The Zipper only supports creating zip files without compression (story only) for now. The interface The Zipper only supports creating zip files without compression (story only) for now. The interface
is pretty straightforward and there is no event-based / streaming API. is pretty straightforward and there is no event-based / streaming API.
@ -95,7 +114,7 @@ const zippedArrayBuffer = await zipper.start(
true /* isLastFile */); true /* isLastFile */);
``` ```
## bitjs.codecs ### bitjs.codecs
This package includes code for dealing with media files (audio/video). It is useful for deriving This package includes code for dealing with media files (audio/video). It is useful for deriving
ISO RFC6381 MIME type strings, including the codec information. Currently supports a limited subset ISO RFC6381 MIME type strings, including the codec information. Currently supports a limited subset
@ -121,7 +140,7 @@ exec(cmd, (error, stdout) => {
}); });
``` ```
## bitjs.file ### bitjs.file
This package includes code for dealing with files. It includes a sniffer which detects the type of file, given an ArrayBuffer. This package includes code for dealing with files. It includes a sniffer which detects the type of file, given an ArrayBuffer.
@ -130,7 +149,7 @@ import { findMimeType } from './bitjs/file/sniffer.js';
const mimeType = findMimeType(someArrayBuffer); const mimeType = findMimeType(someArrayBuffer);
``` ```
## bitjs.image ### bitjs.image
This package includes code for dealing with binary images. It includes a module for converting WebP images into alternative raster graphics formats (PNG/JPG). This package includes code for dealing with binary images. It includes a module for converting WebP images into alternative raster graphics formats (PNG/JPG).
@ -145,7 +164,7 @@ convertWebPtoPNG(webpBuffer).then(pngBuf => {
}); });
``` ```
## bitjs.io ### bitjs.io
This package includes stream objects for reading and writing binary data at the bit and byte level: BitStream, ByteStream. This package includes stream objects for reading and writing binary data at the bit and byte level: BitStream, ByteStream.
@ -156,12 +175,6 @@ const crc = bstream.readBits(12); // read in 12 bits as CRC, advancing the point
const flagbits = bstream.peekBits(6); // look ahead at next 6 bits, but do not advance the pointer const flagbits = bstream.peekBits(6); // look ahead at next 6 bits, but do not advance the pointer
``` ```
# Other Tests
Those that haven't been ported to mocha/chai/nodejs.
* [bitjs.archive tests](https://codedread.github.io/bitjs/tests/archive-test.html)
## Reference ## Reference
* [UnRar](http://codedread.github.io/bitjs/docs/unrar.html): A work-in-progress description of the RAR file format. * [UnRar](http://codedread.github.io/bitjs/docs/unrar.html): A work-in-progress description of the RAR file format.

View file

@ -27,7 +27,8 @@
*/ */
/** /**
* @typedef ProbeFormat ffprobe -show_format -print_format json. Only the fields we care about. * @typedef ProbeFormat Only the fields we care about from the following command:
* ffprobe -show_format -show_streams -v quiet -print_format json -i file.mp4
* @property {string} filename * @property {string} filename
* @property {string} format_name * @property {string} format_name
* @property {string} duration Number of seconds, as a string like "473.506367". * @property {string} duration Number of seconds, as a string like "473.506367".
@ -123,7 +124,7 @@ export function getFullMIMEString(info) {
case 'opus': codecFrags.add('opus'); break; case 'opus': codecFrags.add('opus'); break;
default: default:
throw `Could not handle codec_name ${stream.codec_name}, ` + throw `Could not handle codec_name ${stream.codec_name}, ` +
`codec_tag_string ${stream.codec_tag_string} for file ${info.filename} yet. ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` +
`Please file a bug https://github.com/codedread/bitjs/issues/new`; `Please file a bug https://github.com/codedread/bitjs/issues/new`;
} }
break; break;
@ -141,9 +142,10 @@ export function getFullMIMEString(info) {
switch (stream.codec_name) { switch (stream.codec_name) {
case 'h264': codecFrags.add(getAVC1CodecString(stream)); break; case 'h264': codecFrags.add(getAVC1CodecString(stream)); break;
case 'vp9': codecFrags.add(getVP09CodecString(stream)); break; case 'vp9': codecFrags.add(getVP09CodecString(stream)); break;
case 'mpeg2video': codecFrags.add('mpeg2video'); break;
default: default:
throw `Could not handle codec_name ${stream.codec_name}, ` + throw `Could not handle codec_name ${stream.codec_name}, ` +
`codec_tag_string ${stream.codec_tag_string} for file ${info.filename} yet. ` + `codec_tag_string ${stream.codec_tag_string} for file ${info.format.filename} yet. ` +
`Please file a bug https://github.com/codedread/bitjs/issues/new`; `Please file a bug https://github.com/codedread/bitjs/issues/new`;
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "@codedread/bitjs", "name": "@codedread/bitjs",
"version": "1.0.6", "version": "1.0.7",
"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",

View file

@ -354,4 +354,25 @@ describe('codecs test suite', () => {
.and.equals('audio/webm; codecs="opus"'); .and.equals('audio/webm; codecs="opus"');
}); });
}); });
describe('MPEG2', () => {
/** @type {ProbeInfo} */
let info;
beforeEach(() => {
info = {
format: { format_name: 'matroska,webm' },
streams: [{
codec_type: 'video',
codec_name: 'mpeg2video',
}],
};
});
it('detects mpeg2video', () => {
expect(getFullMIMEString(info))
.to.be.a('string')
.and.equals('video/webm; codecs="mpeg2video"');
});
});
}); });