Compare commits

..
47 Commits
Author SHA1 Message Date
Vanilagy 472dc113e3 Uhm 2026-03-05 19:58:10 +01:00
Vanilagy 193dcf0f14 Bump minor 2026-03-05 19:57:45 +01:00
Vanilagy f79834c3c1 Add frame rate-based sampling for MediaStreamVideoTrackSource, reducing the probability of files with wildly irregular FPS; add unthrottled timer primitives 2026-03-05 19:57:32 +01:00
Vanilagy 66185fed15 Clean up & clarify 2026-03-05 11:49:27 +01:00
Vanilagy 4172f81aa0 Upload FLAC artifacts in release CI 2026-03-04 21:27:21 +01:00
Vanilagy f37dd21947 Bump minor 2026-03-04 21:23:02 +01:00
Vanilagy a15283eaa3 Add @mediabunny/flac-encoder extension, clean up some other code, fix incorrect top-level tsconfig 2026-03-04 21:21:52 +01:00
Vanilagy e18da824ce Make test more resilient 2026-03-04 17:12:40 +01:00
Vanilagy 506ad0c798 Bump minor 2026-03-04 17:08:02 +01:00
Vanilagy c4398fb88c Add @mediabunny/aac-encoder extension package 2026-03-04 17:05:34 +01:00
Vanilagy 76e0703b13 Bump patch 2026-02-26 21:48:36 +01:00
152002bd5d fix(isobmff): write QuickTime-compliant MOV AAC sample entry (#311)
* fix(isobmff): write QuickTime-compliant MOV AAC sample entry

* Clean up logic, only version 1 sample description box was needed, remove bloaty tests

---------

Co-authored-by: Vanilagy <[email protected]>
2026-02-26 21:48:11 +01:00
Vanilagy adc2f125bf Bump minor 2026-02-25 17:20:06 +01:00
Vanilagy bb4749cd8d Add support for reading/writing non-square pixel video frames (fixes #309) 2026-02-25 17:16:33 +01:00
Vanilagy a7dc2c198b Bump patch 2026-02-24 13:13:34 +01:00
Vanilagy 89afa1e2a6 Make worker_threads dynamic imports direct and keep them in the bundle (externalized), & mark them as ignored for browser builds (fixes #306) 2026-02-24 13:11:16 +01:00
31147d6e96 fix: surface StreamTarget write errors instead of swallowing them (#305)
* fix: await StreamTarget writes to prevent overlapping OPFS operations

* no need for flush

* preserve fire and forget writes

* Remove pendingWrites, add missing mutex acquire to ADTS muxer finalize method

---------

Co-authored-by: Vanilagy <[email protected]>
2026-02-24 11:21:13 +00:00
Vanilagy 48f9bda91a Add Tella gold sponsor 2026-02-24 11:07:05 +01:00
Vanilagy 6a4e86698e Bump patch 2026-02-17 12:35:49 +01:00
Vanilagy 317193452c Fix Webpack erroring with worker_threads import, unref Worker so process can terminate 2026-02-17 12:34:45 +01:00
Vanilagy 4928779ba1 Add Screen Studio gold sponsor 2026-02-16 21:04:41 +01:00
Vanilagy 703858fc4e Bump patch, fix broken remote resource link 2026-02-16 20:59:33 +01:00
Vanilagy df5efa0442 Fix test 2026-02-16 15:34:42 +01:00
Vanilagy ddc826e6e3 MPEG-TS muxer: Write out DTS for video tracks (fixes buggy QuickTime behavior) 2026-02-16 15:30:23 +01:00
2876197631 Fix WaveDemuxer reading too far in corrupted ID3 chunk (#301)
* Update read-mp4.test.ts

* Fix the bug

* Delete read-wav.test.ts

* Submit test file

* Small adjustments

---------

Co-authored-by: Vanilagy <[email protected]>
2026-02-16 10:48:07 +00:00
Vanilagy e3906a3e36 Bump patch AGAIN 2026-02-12 18:54:42 +01:00
Vanilagy 46bd70b880 Bump patch, add bunny 2026-02-12 18:50:00 +01:00
Vanilagy 31b278fbd4 Fix linting errors, fix test not creating bundle 2026-02-12 18:37:55 +01:00
Vanilagy d3b737b183 Bump minor 2026-02-12 18:32:32 +01:00
Vanilagy a97200f3f0 Add @mediabunny/ac3 extension package 2026-02-12 18:31:59 +01:00
Vanilagy e3aa7108f5 Make test 150% faster 2026-02-12 12:22:25 +01:00
Vanilagy b717cbf68c Bump minor 2026-02-12 12:06:25 +01:00
Vanilagy 7cc26edacb Add Mediabunny Codec Registry 2026-02-12 11:56:37 +01:00
Vanilagy de9a4bbc23 Update codec soup 2026-02-11 16:03:57 +01:00
Vanilagy 4fae45ec40 Allow negative trim ranges for conversion, add OutputFormat.supportsTimestampedMediaData 2026-02-11 15:56:51 +01:00
417c85d8ed feat(codec): add detection support for AC-3, E-AC-3 audio codecs (#275)
* ac3/eac3 detection

* fix tests

* review pass + follow codebase comments style

* Add ac3/eac3 to docs

* WIP mpeg-2

* Set registration_descriptor

* add support for ac3 system b in mpeg-ts

* Update docs with ac3 in .ts support

* fix formatting

* Remove default to fix linter

* Add eac3 to mpeg-ts

* fix formatting

* Fix failing test

* Refactor AC3/EAC3 code a little

---------

Co-authored-by: Vanilagy <[email protected]>
2026-02-11 15:23:12 +01:00
Vanilagy 612642c5ad Remove sponsor 2026-02-11 08:38:49 +01:00
Vanilagy 2298f4a8b3 Add jepcd sponsor 2026-02-10 17:07:57 +01:00
David P.andGitHub a613a870c8 Merge pull request #296 from yonathan06/patch-1
Update gling.svg
2026-02-10 16:24:26 +01:00
YonatanandGitHub b61b2b326f Update gling.svg 2026-02-10 16:45:44 +02:00
Vanilagy cacbc2a399 Fix assertion failure 2026-02-07 10:35:40 +01:00
Vanilagy ab0fd6a8d8 Fix WAVE demuxer out of bounds reads 2026-02-07 10:28:14 +01:00
Vanilagy 6a099b4259 Bump minor 2026-02-04 18:28:34 +01:00
Vanilagy 172b4a1fd2 Add two new sponsors 2026-02-04 16:54:45 +01:00
David P.andGitHub 43c1ea7efd Merge pull request #292 from fredrikj/parallelism
Add UrlSourceOptions.parallelism
2026-02-03 16:03:55 +01:00
Vanilagy cab92c55ec Fix a few things 2026-02-03 16:01:46 +01:00
Fredrik Johansson 3209363470 Add UrlSourceOptions.parallelism 2026-01-29 09:31:51 +01:00
156 changed files with 9026 additions and 586 deletions
+27 -1
View File
@@ -59,7 +59,33 @@ jobs:
- name: Upload build artifacts
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ github.event.release.tag_name }} dist/bundles/mediabunny.cjs dist/bundles/mediabunny.min.cjs dist/bundles/mediabunny.mjs dist/bundles/mediabunny.min.mjs dist/mediabunny.d.ts packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.js packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.min.js packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.mjs packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.min.mjs packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
run: >
gh release upload ${{ github.event.release.tag_name }}
dist/bundles/mediabunny.cjs
dist/bundles/mediabunny.min.cjs
dist/bundles/mediabunny.mjs
dist/bundles/mediabunny.min.mjs
dist/mediabunny.d.ts
packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.js
packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.min.js
packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.mjs
packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.min.mjs
packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
packages/ac3/dist/bundles/mediabunny-ac3.js
packages/ac3/dist/bundles/mediabunny-ac3.min.js
packages/ac3/dist/bundles/mediabunny-ac3.mjs
packages/ac3/dist/bundles/mediabunny-ac3.min.mjs
packages/ac3/dist/mediabunny-ac3.d.ts
packages/aac-encoder/dist/bundles/mediabunny-aac-encoder.js
packages/aac-encoder/dist/bundles/mediabunny-aac-encoder.min.js
packages/aac-encoder/dist/bundles/mediabunny-aac-encoder.mjs
packages/aac-encoder/dist/bundles/mediabunny-aac-encoder.min.mjs
packages/aac-encoder/dist/mediabunny-aac-encoder.d.ts
packages/flac-encoder/dist/bundles/mediabunny-flac-encoder.js
packages/flac-encoder/dist/bundles/mediabunny-flac-encoder.min.js
packages/flac-encoder/dist/bundles/mediabunny-flac-encoder.mjs
packages/flac-encoder/dist/bundles/mediabunny-flac-encoder.min.mjs
packages/flac-encoder/dist/mediabunny-flac-encoder.d.ts
- name: Publish Mediabunny to npm
run: npm publish --access public
+4 -1
View File
@@ -6,4 +6,7 @@ node_modules
/docs/api
*.tsbuildinfo
packages/mp3-encoder/dist
packages/mp3-encoder/dist
packages/ac3/dist
packages/aac-encoder/dist
packages/flac-encoder/dist
+8 -4
View File
@@ -34,15 +34,19 @@ Mediabunny is a JavaScript library for reading, writing, and converting media fi
<a href="https://kino.ai/" target="_blank" rel="sponsored">
<img src="./docs/public/sponsors/kino.jpg" width="60" height="60" alt="Kino">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://screen.studio/" target="_blank" rel="sponsored">
<img src="./docs/public/sponsors/screen-studio.webp" width="60" height="60" alt="Screen Studio">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://www.tella.com/" target="_blank" rel="sponsored">
<img src="./docs/public/sponsors/tella.svg" width="60" height="60" alt="Tella">
</a>
</div>
### Silver sponsors
<div align="center">
<a href="https://pqina.nl/pintura/" target="_blank" rel="sponsored">
<img src="./docs/public/sponsors/pintura-labs.png" width="50" height="50" alt="Pintura Labs">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://ponder.ai/" target="_blank" rel="sponsored">
<img src="./docs/public/sponsors/ponder.png" width="50" height="50" alt="Ponder">
</a>
+12 -5
View File
@@ -2,9 +2,11 @@
<script src="../dist/bundles/mediabunny.cjs"></script>
<script src="../packages/mp3-encoder/dist/bundles/mediabunny-mp3-encoder.js"></script>
<script src="../packages/ac3/dist/bundles/mediabunny-ac3.js"></script>
<script type="module">
//MediabunnyMp3Encoder.registerMp3Encoder();
MediabunnyAc3.registerAc3Encoder();
const fileInput = document.createElement('input');
fileInput.type = 'file';
@@ -56,7 +58,9 @@
}),
output,
audio: (_, n) => ({
discard: n > 1,
//codec: 'eac3',
//discard: true,
//discard: n > 1,
//codec: 'opus',
//codec: 'opus',
/*
@@ -100,8 +104,11 @@
},
*/
video: () => ({
forceTranscode: true,
allowRotationMetadata: false,
//forceTranscode: true,
//forceTranscode: true,
//width: 1280,
//forceTranscode: true,
//allowRotationMetadata: false,
//width: 720,
//frameRate: 30,
//bitrate: Mediabunny.QUALITY_VERY_LOW,
@@ -181,8 +188,8 @@
}
},
trim: {
start: 0,
//end: 4
//start: 0,
//end: 10
},
});
console.log(conversion);
+7 -5
View File
@@ -1,8 +1,11 @@
<!DOCTYPE html>
<script src="../dist/bundles/mediabunny.cjs"></script>
<script src="../packages/ac3/dist/bundles/mediabunny-ac3.js"></script>
<script type="module">
MediabunnyAc3.registerAc3Decoder();
const fileInput = document.createElement('input');
fileInput.type = 'file';
document.body.append(fileInput);
@@ -14,12 +17,11 @@
source: new Mediabunny.BlobSource(file),
});
const track = await input.getPrimaryVideoTrack();
const sink = new Mediabunny.EncodedPacketSink(track);
const videoTrack = await input.getPrimaryVideoTrack();
const sink = new Mediabunny.VideoSampleSink(videoTrack);
for await (const packet of sink.packets()) {
console.log(packet);
}
const sample = await sink.getSample(await videoTrack.getFirstTimestamp());
console.log(sample);
/*
+6 -4
View File
@@ -17,9 +17,9 @@
const button = document.querySelector('button');
button.addEventListener('click', async () => {
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
const stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false });
const videoTrack = stream.getVideoTracks()[0];
const audioTrack = stream.getAudioTracks()[0];
const audioTrack = null;//stream.getAudioTracks()[0];
const output = new Mediabunny.Output({
target: new Mediabunny.BufferTarget(),
@@ -30,7 +30,8 @@
if (videoTrack) {
videoSource = new Mediabunny.MediaStreamVideoTrackSource(videoTrack, {
codec: 'avc',
bitrate: Mediabunny.QUALITY_MEDIUM
bitrate: Mediabunny.QUALITY_MEDIUM,
sizeChangeBehavior: 'passThrough',
});
videoSource.errorPromise.catch((d) => console.log("Hello?????", d));
@@ -45,12 +46,13 @@
audioSource.errorPromise.catch((d) => console.log("Hello!!???", d));
output.addAudioTrack(audioSource);
//output.addAudioTrack(audioSource);
}
await output.start();
setTimeout(() => {
return;
videoSource?.pause();
audioSource?.pause();
+43
View File
@@ -42,6 +42,12 @@ export default withMermaid({
{ text: 'Examples', link: '/examples', activeMatch: '/examples' },
{ text: 'Sponsors', link: '/#sponsors', activeMatch: '/#sponsors' },
{ text: 'License', link: 'https://github.com/Vanilagy/mediabunny#license' },
{
text: 'More',
items: [
{ text: 'Codec Registry', link: '/codec-registry/overview' },
],
},
],
sidebar: {
@@ -87,12 +93,49 @@ export default withMermaid({
text: 'Extensions',
items: [
{ text: 'mp3-encoder', link: '/guide/extensions/mp3-encoder' },
{ text: 'aac-encoder', link: '/guide/extensions/aac-encoder' },
{ text: 'ac3', link: '/guide/extensions/ac3' },
{ text: 'flac-encoder', link: '/guide/extensions/flac-encoder' },
],
},
],
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
'/api': apiRoutes as any,
'/codec-registry': [
{
text: 'Codec registry',
items: [
{ text: 'Overview', link: '/codec-registry/overview' },
],
},
{
text: 'Video',
items: [
{ text: 'AVC (H.264)', link: '/codec-registry/avc' },
{ text: 'HEVC (H.265)', link: '/codec-registry/hevc' },
{ text: 'VP8', link: '/codec-registry/vp8' },
{ text: 'VP9', link: '/codec-registry/vp9' },
{ text: 'AV1', link: '/codec-registry/av1' },
],
},
{
text: 'Audio',
items: [
{ text: 'AAC', link: '/codec-registry/aac' },
{ text: 'Opus', link: '/codec-registry/opus' },
{ text: 'MP3', link: '/codec-registry/mp3' },
{ text: 'Vorbis', link: '/codec-registry/vorbis' },
{ text: 'FLAC', link: '/codec-registry/flac' },
{ text: 'AC-3', link: '/codec-registry/ac3' },
{ text: 'E-AC-3', link: '/codec-registry/eac3' },
{ text: 'Linear PCM', link: '/codec-registry/pcm' },
{ text: 'μ-law PCM', link: '/codec-registry/ulaw' },
{ text: 'A-law PCM', link: '/codec-registry/alaw' },
],
},
],
},
socialLinks: [
+4 -1
View File
@@ -19,5 +19,8 @@
"Custom coders": "API for adding custom encoders and decoders.",
"Miscellaneous": "Whatever's left.",
"@mediabunny/mp3-encoder": "Adds MP3 encoder support to Mediabunny."
"@mediabunny/mp3-encoder": "Adds MP3 encoder support to Mediabunny.",
"@mediabunny/ac3": "Adds AC-3/E-AC-3 decoder and encoder support to Mediabunny.",
"@mediabunny/aac-encoder": "Polyfills AAC encoder support to Mediabunny.",
"@mediabunny/flac-encoder": "Adds FLAC encoder support to Mediabunny."
}
File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 141 KiB

+50
View File
@@ -0,0 +1,50 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# AAC codec registration
## Description
The Advanced Audio Coding (AAC) audio codec, specified in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html).
An AAC bitstream can have either of two formats:
- _AAC_ (raw), where packets contain raw AAC frames (syntax element `raw_data_block()`) as defined in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html) Section 4.4.2.1. Here, codec metadata is provided out-of-band.
- _ADTS_, where packets contain ADTS frames as defined in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html) Section 1.A.3.2. Here, codec metadata is provided in-band in each ADTS frame header.
All packets within the bitstream must have the same format.
## Codec ID
```ts
'aac'
```
## `EncodedPacket` data
If the bitstream is in the _AAC_ (raw) format, the packet's data must be a raw AAC frame (syntax element `raw_data_block()`) as defined in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html) Section 4.4.2.1.
If the bitstream is in the _ADTS_ format, the packet's data must be an ADTS frame as defined in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html) Section 1.A.3.2.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
The following fully qualified codec strings are recognized:
- `'mp4a.40.2'` — MPEG-4 AAC-LC
- `'mp4a.40.02'` — MPEG-4 AAC-LC (leading zero for Aud-OTI compatibility)
- `'mp4a.40.5'` — MPEG-4 HE-AAC v1 (AAC-LC + SBR)
- `'mp4a.40.05'` — MPEG-4 HE-AAC v1 (leading zero for Aud-OTI compatibility)
- `'mp4a.40.29'` — MPEG-4 HE-AAC v2 (AAC-LC + SBR + PS)
- `'mp4a.67'` — MPEG-2 AAC-LC
## `AudioDecoderConfig` description
If the bitstream is in the _AAC_ (raw) format, `description` must be an `AudioSpecificConfig` as defined in [ISO/IEC 14496-3](https://www.iso.org/standard/76383.html) Section 1.6.2.1.
If the bitstream is in the _ADTS_ format, `description` must be undefined.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# AC-3 codec registration
## Description
The Dolby Digital (AC-3) audio codec, specified in [ETSI TS 102 366](https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf).
## Codec ID
```ts
'ac3'
```
## `EncodedPacket` data
The packet's data must be a sync frame (syntactic element `syncframe()`) as defined in Section 4.3 of [ETSI TS 102 366](https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf), beginning with the sync word `0x0B77`.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'ac-3'
```
## `AudioDecoderConfig` description
`description` is not used for this codec.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# A-law PCM codec registration
## Description
The A-law companded PCM audio codec, specified in [ITU-T G.711](https://www.itu.int/rec/T-REC-G.711) Tables 1a and 1b.
## Codec ID
```ts
'alaw'
```
## `EncodedPacket` data
The packet's data must be a sequence of bytes of arbitrary length (divisible by the channel count), where each byte is an A-law encoded PCM sample. For multichannel audio, samples from different channels are interleaved.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'alaw'
```
## `AudioDecoderConfig` description
`description` is not used for this codec.
+33
View File
@@ -0,0 +1,33 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Video codec" />
# AV1 codec registration
## Description
The AOMedia Video 1 (AV1) video codec, specified in the [AV1 Bitstream & Decoding Process Specification](https://aomediacodec.github.io/av1-spec/).
## Codec ID
```ts
'av1'
```
## `EncodedPacket` data
The packet's data must comply with the low-overhead bitstream format as defined in Section 5 of the [AV1 Bitstream & Decoding Process Specification](https://aomediacodec.github.io/av1-spec/).
## `EncodedPacket` type
If the packet's type is `'key'`, then the packet is expected to contain a frame with `frame_type` of `KEY_FRAME`, as defined in Section 6.8.2 of the [AV1 Bitstream & Decoding Process Specification](https://aomediacodec.github.io/av1-spec/).
## `VideoDecoderConfig` codec string
The full codec string begins with the prefix `'av01.'`, with a variable-length suffix as specified in Section 5 of the [AV1 Codec ISO Media File Format Binding](https://aomediacodec.github.io/av1-isobmff/).
## `VideoDecoderConfig` description
`description` is not used for this codec.
+41
View File
@@ -0,0 +1,41 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Video codec" />
# AVC (H.264) codec registration
## Description
The Advanced Video Coding (H.264) video codec, specified in [Rec. ITU-T H.264](https://www.itu.int/rec/T-REC-H.264) / [ISO/IEC 14496-10](https://www.iso.org/standard/87574.html).
An AVC bitstream can have either of two formats:
- _Canonical_ (length-prefixed), as defined in [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html) Section 5.3.2. Here, video parameter sets (SPS/PPS) are provided out-of-band.
- _Annex B_, as defined in [Rec. ITU-T H.264](https://www.itu.int/rec/T-REC-H.264) Annex B. Here, video parameter sets (SPS/PPS) must be provided in-band in the respective NALUs.
All packets within the bitstream must have the same format.
## Codec ID
```ts
'avc'
```
## `EncodedPacket` data
The packet's data must be an access unit as defined in [Rec. ITU-T H.264](https://www.itu.int/rec/T-REC-H.264) Section 7.4.1.2, in either _canonical_ or _Annex B_ format.
## `EncodedPacket` type
If the packet's type is `'key'`, then the packet is expected to contain a primary coded picture from which decoding can begin. Additionally, if the bitstream's format is _Annex B_, then this packet is also expected to contain the necessary video parameter sets to initialize the decoder.
## `VideoDecoderConfig` codec string
The full codec string begins with the prefix `'avc1.'` or `'avc3.'`, with a suffix of 6 characters as described respectively in Section 3.4 of [RFC 6381](https://www.rfc-editor.org/rfc/rfc6381) and Section 5.4.1 of [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html).
## `VideoDecoderConfig` description
If the bitstream is in the _canonical_ (length-prefixed) format, `description` must be an `AVCDecoderConfigurationRecord` as defined in [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html) Section 5.3.3.1.
If the bitstream is in the _Annex B_ format, `description` must be undefined.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# E-AC-3 codec registration
## Description
The Dolby Digital Plus (E-AC-3) audio codec, specified in [ETSI TS 102 366](https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf).
## Codec ID
```ts
'eac3'
```
## `EncodedPacket` data
The packet's data must be a sync frame as defined in Section E.1.2.0 of [ETSI TS 102 366](https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf), beginning with the sync word `0x0B77`.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'ec-3'
```
## `AudioDecoderConfig` description
`description` is not used for this codec.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# FLAC codec registration
## Description
The Free Lossless Audio Codec (FLAC), specified in the [FLAC Format Specification](https://xiph.org/flac/format.html).
## Codec ID
```ts
'flac'
```
## `EncodedPacket` data
The packet's data must be a FLAC frame as described in the [FLAC Format Specification](https://xiph.org/flac/format.html).
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'flac'
```
## `AudioDecoderConfig` description
`description` must contain the bytes `0x66 0x4C 0x61 0x43` (the ASCII string `'fLaC'`), followed by a `STREAMINFO` metadata block as defined in Section 7 of the [FLAC Format Specification](https://xiph.org/flac/format.html).
+41
View File
@@ -0,0 +1,41 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Video codec" />
# HEVC (H.265) codec registration
## Description
The High Efficiency Video Coding (H.265) video codec, specified in [Rec. ITU-T H.265](https://www.itu.int/rec/T-REC-H.265) / [ISO/IEC 23008-2](https://www.iso.org/standard/75484.html).
An HEVC bitstream can have either of two formats:
- _Canonical_ (length-prefixed), as defined in [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html) Section 8.3.2. Here, video parameter sets (VPS/SPS/PPS) are provided out-of-band.
- _Annex B_, as defined in [Rec. ITU-T H.265](https://www.itu.int/rec/T-REC-H.265) Annex B. Here, video parameter sets (VPS/SPS/PPS) must be provided in-band in the respective NALUs.
All packets within the bitstream must have the same format.
## Codec ID
```ts
'hevc'
```
## `EncodedPacket` data
The packet's data must be an access unit as defined in [Rec. ITU-T H.265](https://www.itu.int/rec/T-REC-H.265) Section 7.4.2.4, containing exactly one base layer coded picture, in either _canonical_ or _Annex B_ format.
## `EncodedPacket` type
If the packet's type is `'key'`, then the packet is expected to contain an IDR, CRA, or BLA picture. Additionally, if the bitstream's format is _Annex B_, then this packet is also expected to contain the necessary video parameter sets to initialize the decoder.
## `VideoDecoderConfig` codec string
The full codec string begins with the prefix `'hev1.'` or `'hvc1.'`, with a variable-length suffix of four dot-separated fields as specified in Section E.3 of [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html).
## `VideoDecoderConfig` description
If the bitstream is in the _canonical_ (length-prefixed) format, `description` must be an `HEVCDecoderConfigurationRecord` as defined in [ISO/IEC 14496-15](https://www.iso.org/standard/89118.html) Section 8.3.3.1.
If the bitstream is in the _Annex B_ format, `description` must be undefined.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# MP3 codec registration
## Description
The MP3 audio codec (MPEG-1/2 Audio Layer III), specified in [ISO/IEC 13818-3](https://www.iso.org/standard/26797.html).
## Codec ID
```ts
'mp3'
```
## `EncodedPacket` data
The packet's data must be an MP3 frame as described in Section 2.4.2.2 of [ISO/IEC 13818-3](https://www.iso.org/standard/26797.html).
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'mp3'
```
## `AudioDecoderConfig` description
`description` is not used for this codec.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# Opus codec registration
## Description
The Opus audio codec, specified in [RFC 6716](https://www.rfc-editor.org/rfc/rfc6716).
## Codec ID
```ts
'opus'
```
## `EncodedPacket` data
The packet's data must be an Opus packet as described in Section 3 of [RFC 6716](https://www.rfc-editor.org/rfc/rfc6716).
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'opus'
```
## `AudioDecoderConfig` description
If present, `description` must be an Identification Header as defined in Section 5.1 of [RFC 7845](https://www.rfc-editor.org/rfc/rfc7845).
+26
View File
@@ -0,0 +1,26 @@
# Mediabunny Codec Registry
The Mediabunny Codec Registry formalizes the precise definitions of all video and audio codecs supported by Mediabunny. More specifically, for any given codec, it describes the format that `EncodedPacket`, `VideoDecoderConfig` and `AudioDecoderConfig` must adhere to. All packets coming out of or going into Mediabunny are expected to adhere to this registry.
The registry is an extension of the [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/). Mediabunny's registry matches that of WebCodecs for all codecs supported by both.
## Video codecs
- [AVC (H.264)](./avc)
- [HEVC (H.265)](./hevc)
- [VP8](./vp8)
- [VP9](./vp9)
- [AV1](./av1)
## Audio codecs
- [AAC](./aac)
- [Opus](./opus)
- [MP3](./mp3)
- [Vorbis](./vorbis)
- [FLAC](./flac)
- [AC-3](./ac3)
- [E-AC-3](./eac3)
- [Linear PCM](./pcm)
- [μ-law PCM](./ulaw)
- [A-law PCM](./alaw)
+44
View File
@@ -0,0 +1,44 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# Linear PCM codecs registration
## Description
A family of linear pulse-code modulation (PCM) audio codecs of various bit depths and byte orders.
## Codec IDs
| Codec ID | Description |
| --- | --- |
| `'pcm-u8'` | Unsigned 8-bit integer |
| `'pcm-s8'` | Signed 8-bit integer |
| `'pcm-s16'` | Signed 16-bit integer, little-endian |
| `'pcm-s16be'` | Signed 16-bit integer, big-endian |
| `'pcm-s24'` | Signed 24-bit integer, little-endian |
| `'pcm-s24be'` | Signed 24-bit integer, big-endian |
| `'pcm-s32'` | Signed 32-bit integer, little-endian |
| `'pcm-s32be'` | Signed 32-bit integer, big-endian |
| `'pcm-f32'` | 32-bit float, little-endian |
| `'pcm-f32be'` | 32-bit float, big-endian |
| `'pcm-f64'` | 64-bit float, little-endian |
| `'pcm-f64be'` | 64-bit float, big-endian |
## `EncodedPacket` data
The packet's data must be a sequence of bytes of arbitrary length (divisible by the size of one frame), with each sample occupying the number of bits defined by the codec ID. For multichannel audio, samples from different channels are interleaved.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
The codec string is the same as the codec ID (e.g. `'pcm-s16'`, `'pcm-f32'`).
## `AudioDecoderConfig` description
`description` is not used for this codec.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# μ-law PCM codec registration
## Description
The μ-law companded PCM audio codec, specified in [ITU-T G.711](https://www.itu.int/rec/T-REC-G.711) Tables 2a and 2b.
## Codec ID
```ts
'ulaw'
```
## `EncodedPacket` data
The packet's data must be a sequence of bytes of arbitrary length (divisible by the channel count), where each byte is a μ-law encoded PCM sample. For multichannel audio, samples from different channels are interleaved.
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'ulaw'
```
## `AudioDecoderConfig` description
`description` is not used for this codec.
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Audio codec" />
# Vorbis codec registration
## Description
The Vorbis audio codec, specified in the [Vorbis I Specification](https://xiph.org/vorbis/doc/Vorbis_I_spec.html).
## Codec ID
```ts
'vorbis'
```
## `EncodedPacket` data
The packet's data must be an audio packet as described in Section 4.3 of the [Vorbis I Specification](https://xiph.org/vorbis/doc/Vorbis_I_spec.html).
## `EncodedPacket` type
The packet's type is always `'key'`.
## `AudioDecoderConfig` codec string
```ts
'vorbis'
```
## `AudioDecoderConfig` description
`description` must contain Vorbis codec setup data in Xiph extradata format: the `page_segments` field, followed by the `segment_table` field, followed by the three Vorbis header packets (identification header, comments header, and setup header) as defined in Section 4.2 of the [Vorbis I Specification](https://xiph.org/vorbis/doc/Vorbis_I_spec.html).
+35
View File
@@ -0,0 +1,35 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Video codec" />
# VP8 codec registration
## Description
The VP8 video codec, specified in [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386).
## Codec ID
```ts
'vp8'
```
## `EncodedPacket` data
The packet's data must be a frame as described in Section 4 and Annex A of [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386).
## `EncodedPacket` type
If the packet's type is `'key'`, then the packet is expected to contain a frame where `key_frame` is `true`, as defined in Section 19.1 of [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386).
## `VideoDecoderConfig` codec string
```ts
'vp8'
```
## `VideoDecoderConfig` description
`description` is not used for this codec.
+33
View File
@@ -0,0 +1,33 @@
<script setup>
import { VPBadge } from 'vitepress/theme'
</script>
<VPBadge type="info" text="Video codec" />
# VP9 codec registration
## Description
The VP9 video codec, specified in the [VP9 Bitstream & Decoding Process Specification](https://storage.googleapis.com/downloads.webmproject.org/docs/vp9/vp9-bitstream-specification-v0.6-20160331-draft.pdf).
## Codec ID
```ts
'vp9'
```
## `EncodedPacket` data
The packet's data must be a frame as described in Section 6 of the [VP9 Bitstream & Decoding Process Specification](https://storage.googleapis.com/downloads.webmproject.org/docs/vp9/vp9-bitstream-specification-v0.6-20160331-draft.pdf).
## `EncodedPacket` type
If the packet's type is `'key'`, then the packet is expected to contain a frame with `frame_type` of `KEY_FRAME`, as defined in Section 7.2 of the [VP9 Bitstream & Decoding Process Specification](https://storage.googleapis.com/downloads.webmproject.org/docs/vp9/vp9-bitstream-specification-v0.6-20160331-draft.pdf).
## `VideoDecoderConfig` codec string
The full codec string begins with the prefix `'vp09.'`, with a variable-length suffix as specified in the [VP Codec ISO Media File Format Binding](https://www.webmproject.org/vp9/mp4/).
## `VideoDecoderConfig` description
`description` is not used for this codec.
+13
View File
@@ -367,6 +367,19 @@ If only `start` is set, the clip will run until the end of the input file. If on
Note that when using the trimming defaults, the resulting media file will always begin at timestamp 0. If your input file has a start time offset (like is common with MPEG-TS files) and you want to retain that, use `trim: { start: 0 }` to ensure timestamps don't get shifted.
---
You can even use negative trimming values to offset the start of the media:
```ts
const conversion = await Conversion.init({
// ...
trim: {
start: -2, // Two seconds of no media data (freeze frame / silence) at the start
},
// ...
});
```
## Metadata tags
By default, any [descriptive metadata tags](../api/MetadataTags.md) of the input will be copied to the output. If you want to further control the metadata tags written to the output, you can use the `tags` options:
+82
View File
@@ -0,0 +1,82 @@
# @mediabunny/aac-encoder
Some browsers lack support for AAC encoding in their WebCodecs implementations. This extension package provides a reliable AAC-LC encoder for use with Mediabunny. It is implemented using Mediabunny's [custom coder API](../supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [FFmpeg](https://ffmpeg.org/)'s AAC encoder under the hood.
<a class="!no-underline inline-flex items-center gap-1.5" :no-icon="true" href="https://github.com/Vanilagy/mediabunny/blob/main/packages/aac-encoder/README.md">
GitHub page
<span class="vpi-arrow-right" />
</a>
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/aac-encoder
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-aac-encoder.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyAacEncoder`. Use `mediabunny-aac-encoder.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerAacEncoder } from '@mediabunny/aac-encoder';
registerAacEncoder();
```
That's it - Mediabunny now uses the registered AAC encoder automatically.
If you want to be more correct, check for native browser support first:
```ts
import { canEncodeAudio } from 'mediabunny';
import { registerAacEncoder } from '@mediabunny/aac-encoder';
if (!(await canEncodeAudio('aac'))) {
registerAacEncoder();
}
```
## Example
Here, we convert an input file to an MP4 with AAC audio:
```ts
import {
Input,
ALL_FORMATS,
BlobSource,
Output,
BufferTarget,
Mp4OutputFormat,
canEncodeAudio,
Conversion,
} from 'mediabunny';
import { registerAacEncoder } from '@mediabunny/aac-encoder';
if (!(await canEncodeAudio('aac'))) {
// Only register the custom encoder if there's no native support
registerAacEncoder();
}
const input = new Input({
source: new BlobSource(file), // From a file picker, for example
formats: ALL_FORMATS,
});
const output = new Output({
format: new Mp4OutputFormat(),
target: new BufferTarget(),
});
const conversion = await Conversion.init({
input,
output,
});
await conversion.execute();
output.target.buffer; // => ArrayBuffer containing the MP4 file
```
+33
View File
@@ -0,0 +1,33 @@
# @mediabunny/ac3
Browsers have no support for AC-3 (Dolby Digital) or E-AC-3 (Dolby Digital Plus) in their WebCodecs implementations. This extension package provides both a decoder and encoder for use with Mediabunny, allowing you to decode and encode these codecs directly in the browser. It is implemented using Mediabunny's [custom coder API](../supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [FFmpeg](https://ffmpeg.org/)'s AC-3 and E-AC-3 coders under the hood.
<a class="!no-underline inline-flex items-center gap-1.5" :no-icon="true" href="https://github.com/Vanilagy/mediabunny/blob/main/packages/ac3/README.md">
GitHub page
<span class="vpi-arrow-right" />
</a>
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/ac3
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-ac3.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyAc3`. Use `mediabunny-ac3.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerAc3Decoder, registerAc3Encoder } from '@mediabunny/ac3';
registerAc3Decoder();
registerAc3Encoder();
```
That's it - Mediabunny now uses the registered AC-3/E-AC-3 decoder and encoder automatically.
+81
View File
@@ -0,0 +1,81 @@
# @mediabunny/flac-encoder
No browser currently supports FLAC encoding in their WebCodecs implementations. This extension package provides a reliable FLAC encoder for use with Mediabunny. It is implemented using Mediabunny's [custom coder API](../supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [libFLAC](https://github.com/xiph/flac) under the hood.
<a class="!no-underline inline-flex items-center gap-1.5" :no-icon="true" href="https://github.com/Vanilagy/mediabunny/blob/main/packages/flac-encoder/README.md">
GitHub page
<span class="vpi-arrow-right" />
</a>
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/flac-encoder
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-flac-encoder.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyFlacEncoder`. Use `mediabunny-flac-encoder.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
registerFlacEncoder();
```
That's it - Mediabunny now uses the registered FLAC encoder automatically.
If you want to be more correct, check for native browser support first:
```ts
import { canEncodeAudio } from 'mediabunny';
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
if (!(await canEncodeAudio('flac'))) {
registerFlacEncoder();
}
```
## Example
Here, we convert an input file to a FLAC file:
```ts
import {
Input,
ALL_FORMATS,
BlobSource,
Output,
BufferTarget,
FlacOutputFormat,
canEncodeAudio,
Conversion,
} from 'mediabunny';
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
if (!(await canEncodeAudio('flac'))) {
registerFlacEncoder();
}
const input = new Input({
source: new BlobSource(file), // From a file picker, for example
formats: ALL_FORMATS,
});
const output = new Output({
format: new FlacOutputFormat(),
target: new BufferTarget(),
});
const conversion = await Conversion.init({
input,
output,
});
await conversion.execute();
output.target.buffer; // => ArrayBuffer containing the FLAC file
```
+2 -2
View File
@@ -350,7 +350,7 @@ type CanvasSinkOptions = {
- `rotation`\
The clockwise rotation by which to rotate the raw video frame. Defaults to the rotation set in the file metadata. Rotation is applied before cropping and resizing.
- `crop`\
Specifies the rectangular region of the input video to crop to. The crop region will automatically be clamped to the dimensions of the input video track. Cropping is performed after rotation but before resizing.
Specifies the rectangular region of the input video to crop to. The crop region will automatically be clamped to the dimensions of the input video track. Cropping is performed after rotation but before resizing. The crop region is in the _display pixel space_ of the underlying video data.
- `poolSize`\
See [Canvas pool](#canvas-pool).
@@ -374,7 +374,7 @@ new CanvasSink(videoTrack, {
fit: 'cover',
});
// This sink yields canvases with the unaltered coded dimensions of the track,
// This sink yields canvases with the unrotated dimensions of the track,
// and without applying any rotation.
new CanvasSink(videoTrack, {
rotation: 0,
+4 -4
View File
@@ -74,7 +74,7 @@ type VideoEncodingConfig = {
- `bitrateMode`: Can be used to control constant vs. variable bitrate.
- `latencyMode`: The latency mode as specified by the WebCodecs API. Browsers default to `quality`. Media stream-driven video sources will automatically use the `realtime` setting.
- `keyFrameInterval`: The maximum interval in seconds between two adjacent key frames. Defaults to 5 seconds. More frequent key frames improve seeking behavior but increase file size. When using multiple video tracks, this value should be set to the same value for all tracks.
- `fullCodecString`: Allows you to optionally specify the full codec string used by the video encoder, as specified in the [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/). For example, you may set it to `'avc1.42001f'` when using AVC. Keep in mind that the codec string must still match the codec specified in `codec`. If you don't set this field, a codec string will be generated automatically.
- `fullCodecString`: Allows you to optionally specify the full codec string used by the video encoder, as specified in the [Mediabunny Codec Registry](/codec-registry/overview). For example, you may set it to `'avc1.42001f'` when using AVC. Keep in mind that the codec string must still match the codec specified in `codec`. If you don't set this field, a codec string will be generated automatically.
- `hardwareAcceleration`: A hint that configures the hardware acceleration method of this codec. This is best left on `'no-preference'`.
- `scalabilityMode`: An encoding scalability mode identifier as defined by [WebRTC-SVC](https://w3c.github.io/webrtc-svc/#scalabilitymodes*).
- `contentHint`: An encoding video content hint as defined by [mst-content-hint](https://w3c.github.io/mst-content-hint/#video-content-hints).
@@ -104,7 +104,7 @@ type AudioEncodingConfig = {
- `codec`: The [audio codec](./supported-formats-and-codecs#audio-codecs) used for encoding. Can be omitted for uncompressed PCM codecs.
- `bitrate`: The target number of bits per second. Alternatively, this can be a [subjective quality](#subjective-qualities).
- `bitrateMode`: Can be used to control constant vs. variable bitrate.
- `fullCodecString`: Allows you to optionally specify the full codec string used by the audio encoder, as specified in the [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/). For example, you may set it to `'mp4a.40.2'` when using AAC. Keep in mind that the codec string must still match the codec specified in `codec`. If you don't set this field, a codec string will be generated automatically.
- `fullCodecString`: Allows you to optionally specify the full codec string used by the audio encoder, as specified in the [Mediabunny Codec Registry](/codec-registry/overview). For example, you may set it to `'mp4a.40.2'` when using AAC. Keep in mind that the codec string must still match the codec specified in `codec`. If you don't set this field, a codec string will be generated automatically.
- `onEncodedPacket`: Called for each successfully encoded packet. Useful for determining encoding progress.
- `onEncoderConfig`: Called when the internal encoder config, as used by the WebCodecs API, is created. You can use this to introspect the full codec string.
@@ -240,7 +240,7 @@ await packetSource.add(firstPacket, {
});
```
`codec`, `codedWidth`, and `codedHeight` are required for all codecs, whereas `description` is required for some codecs. Additional fields, such as `colorSpace`, are optional. The [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/) specifies the formats of `codec` and `description` for each video codec, which you must adhere to.
`codec`, `codedWidth`, and `codedHeight` are required for all codecs, whereas `description` is required for some codecs. Additional fields, such as `colorSpace`, are optional. The [Mediabunny Codec Registry](/codec-registry/overview) specifies the formats of `codec` and `description` for each video codec, which you **must** adhere to.
#### B-frames
@@ -397,7 +397,7 @@ await packetSource.add(firstPacket, {
});
```
`codec`, `numberOfChannels`, and `sampleRate` are required for all codecs, whereas `description` is required for some codecs. The [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/) specifies the formats of `codec` and `description` for each audio codec, which you must adhere to.
`codec`, `numberOfChannels`, and `sampleRate` are required for all codecs, whereas `description` is required for some codecs. The [Mediabunny Codec Registry](/codec-registry/overview) specifies the formats of `codec` and `description` for each audio codec, which you must adhere to.
## Subtitle sources
+11
View File
@@ -94,6 +94,8 @@ constructor(
);
```
When creating a packet for a given codec, you *must* adhere to the data format specified in the [Mediabunny Codec Registry](/codec-registry/overview).
::: info
You probably won't ever need to set `sequenceNumber` or `byteLength` in the constructor.
:::
@@ -279,6 +281,10 @@ videoSample.format; // => VideoPixelFormat | null
videoSample.codedWidth; // => number
videoSample.codedHeight; // => number
// Pixel aspect ratio-corrected dimensions of the sample
videoSample.squarePixelWidth;
videoSample.squarePixelHeight;
// Transformed display dimensions of the sample (after rotation)
videoSample.displayWidth; // => number
videoSample.displayHeight; // => number
@@ -287,6 +293,9 @@ videoSample.displayHeight; // => number
// rotated by this amount when it is presented.
videoSample.rotation; // => 0 | 90 | 180 | 270
// The sample's pixel aspect ratio
videoSample.pixelAspectRatio; // => { num: number, den: number }
// Timing information
videoSample.timestamp; // => Presentation timestamp in seconds
videoSample.duration; // => Duration in seconds
@@ -295,6 +304,8 @@ videoSample.microsecondDuration; // => Duration in microseconds
// Color space of the sample
videoSample.colorSpace; // => VideoColorSpace
videoSample.visibleRect; // Rectangle
```
While all of these properties are read-only, you can use the `setTimestamp`, `setDuration` and `setRotation` methods to modify some of the metadata of the video sample.
+15 -3
View File
@@ -129,7 +129,7 @@ track.codec; // => MediaCodec | null
```
This field is `null` when the track's codec couldn't be recognized or is not supported by Mediabunny. See [Codecs](./supported-formats-and-codecs#codecs) for the full list of supported codecs. When Mediabunny doesn't recognize the format, you can still use the `internalCodecId` field to figure out the codec of the track, although its format depends on the container format used and is not homogenized by Mediabunny.
You can also extract the full codec parameter string from the track, as specified in the [WebCodecs Codec Registry](https://www.w3.org/TR/webcodecs-codec-registry/):
You can also extract the full codec parameter string from the track, as specified in the [Mediabunny Codec Registry](/codec-registry/overview):
```ts
await track.getCodecParameterString(); // => 'avc1.42001f'
```
@@ -216,17 +216,26 @@ This will only look at the first ~50 packets and then return the result. This is
In addition to the [common track metadata](#common-track-metadata), video tracks have additional metadata you can query:
```ts
// Get the raw pixel dimensions of the track's coded samples, before rotation:
// Get the raw pixel dimensions of the track's coded samples:
videoTrack.codedWidth; // => number
videoTrack.codedHeight; // => number
// Get the displayed pixel dimensions of the track's samples, after rotation:
// Get the pixel dimensions of the track after aspect ratio adjustments,
// but before rotation:
videoTrack.squarePixelWidth; // => number
videoTrack.squarePixelHeight; // => number
// Get the displayed pixel dimensions of the track's samples, after
// aspect ratio adjustments and rotation:
videoTrack.displayWidth; // => number
videoTrack.displayHeight; // => number
// Get the clockwise rotation in degrees by which the
// track's frames should be rotated:
videoTrack.rotation; // => 0 | 90 | 180 | 270
// Get the aspect ratio of the track's pixels (usually 1:1):
videoTrack.pixelAspectRatio; // => { num: number, den: number }
```
To compute a video track's average frame rate (FPS), use [`computePacketStats`](#packet-statistics):
@@ -506,6 +515,9 @@ type UrlSourceOptions = {
// in memory. Defaults to 8 MiB.
maxCacheSize?: number;
// The maximum number of parallel requests to use for fetching. Defaults to 2.
parallelism?: number;
// Used to provide a custom fetch function
fetchFn?: typeof fetch;
};
+17 -8
View File
@@ -21,6 +21,8 @@ Mediabunny supports a wide range of video, audio, and subtitle codecs. More spec
The availability of the codecs provided by the WebCodecs API depends on the browser and thus cannot be guaranteed by this library. Mediabunny provides [special utility functions](#querying-codec-encodability) to check which codecs are able to be encoded. You can also specify [custom coders](#custom-coders) to provide your own encoder/decoder implementation if the browser doesn't support the codec natively.
For precise definitions of each codec including the corresponding packet format, please refer to the [Mediabunny Codec Registry](/codec-registry/overview).
::: info
Mediabunny ships with built-in decoders and encoders for all audio PCM codecs, meaning they are always supported.
:::
@@ -35,11 +37,13 @@ Mediabunny ships with built-in decoders and encoders for all audio PCM codecs, m
### Audio codecs
- `'aac'` - Advanced Audio Coding (AAC)
- `'aac'` - Advanced Audio Coding (AAC) [^aac]
- `'opus'` - Opus
- `'mp3'` - MP3
- `'mp3'` - MP3 [^mp3]
- `'vorbis'` - Vorbis
- `'flac'` - Free Lossless Audio Codec (FLAC)
- `'flac'` - Free Lossless Audio Codec (FLAC) [^flac]
- `'ac3'` - Dolby Digital (AC-3) [^ac3]
- `'eac3'` - Dolby Digital Plus (E-AC-3) [^ac3]
- `'pcm-u8'` - 8-bit unsigned PCM
- `'pcm-s8'` - 8-bit signed PCM
- `'pcm-s16'` - 16-bit little-endian signed PCM
@@ -63,7 +67,7 @@ Mediabunny ships with built-in decoders and encoders for all audio PCM codecs, m
Not all codecs can be used with all containers. The following table specifies the supported codec-container combinations:
| | .mp4 | .mov | .mkv | .webm[^1] | .ogg | .mp3 | .wav | .aac | .flac | .ts |
| | .mp4 | .mov | .mkv | .webm[^webm] | .ogg | .mp3 | .wav | .aac | .flac | .ts |
|:--------------:|:--------:|:-----:|:-----:|:---------:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
| `'avc'` | ✓ | ✓ | ✓ | | | | | | | ✓ |
| `'hevc'` | ✓ | ✓ | ✓ | | | | | | | ✓ |
@@ -75,6 +79,8 @@ Not all codecs can be used with all containers. The following table specifies th
| `'mp3'` | ✓ | ✓ | ✓ | | | ✓ | | | | ✓ |
| `'vorbis'` | ✓ | ✓ | ✓ | ✓ | ✓ | | | | | |
| `'flac'` | ✓ | ✓ | ✓ | | | | | | ✓ | |
| `'ac3'` | ✓ | ✓ | ✓ | | | | | | | ✓ |
| `'eac3'` | ✓ | ✓ | ✓ | | | | | | | ✓ |
| `'pcm-u8'` | | ✓ | ✓ | | | | ✓ | | | |
| `'pcm-s8'` | | ✓ | | | | | | | | |
| `'pcm-s16'` | ✓ | ✓ | ✓ | | | | ✓ | | | |
@@ -89,11 +95,14 @@ Not all codecs can be used with all containers. The following table specifies th
| `'pcm-f64be'` | ✓ | ✓ | | | | | | | | |
| `'ulaw'` | | ✓ | | | | | ✓ | | | |
| `'alaw'` | | ✓ | | | | | ✓ | | | |
| `'webvtt'`[^2] | (✓) | | (✓) | (✓) | | | | | | |
| `'webvtt'`[^webvtt] | (✓) | | (✓) | (✓) | | | | | | |
[^1]: WebM only supports a small subset of the codecs supported by Matroska. However, this library can technically read all codecs from a WebM that are supported by Matroska.
[^2]: WebVTT can only be written, not read.
[^aac]: In some browsers, AAC encoding is not supported by WebCodecs. You can polyfill it with the [`@mediabunny/aac-encoder`](./extensions/aac-encoder) extension package.
[^mp3]: MP3 encoding is not supported by WebCodecs. You can polyfill it with the [`@mediabunny/mp3-encoder`](./extensions/mp3-encoder) extension package.
[^flac]: FLAC encoding is not supported by WebCodecs. You can polyfill it with the [`@mediabunny/flac-encoder`](./extensions/flac-encoder) extension package.
[^ac3]: AC-3 and E-AC-3 are not natively supported by WebCodecs. To encode or decode these codecs, you can use the [`@mediabunny/ac3`](./extensions/ac3) extension package.
[^webm]: WebM only supports a small subset of the codecs supported by Matroska. However, this library can technically read all codecs from a WebM that are supported by Matroska.
[^webvtt]: WebVTT can only be written, not read.
## Querying codec encodability
+6 -3
View File
@@ -95,9 +95,10 @@ const sponsors = {
{ image: '/sponsors/gling.svg', name: 'Gling AI', url: 'https://www.gling.ai/' },
{ image: '/sponsors/diffusionstudio.png', name: 'Diffusion Studio', url: 'https://diffusion.studio/' },
{ image: '/sponsors/kino.jpg', name: 'Kino', url: 'https://kino.ai/' },
{ image: '/sponsors/screen-studio.webp', name: 'Screen Studio', url: 'https://screen.studio/' },
{ image: '/sponsors/tella.svg', name: 'Tella', url: 'https://www.tella.com/' },
],
silver: [
{ image: '/sponsors/pintura-labs.png', name: 'Pintura Labs', url: 'https://pqina.nl/pintura/' },
{ image: '/sponsors/ponder.png', name: 'Ponder', url: 'https://ponder.ai/' },
],
bronze: [
@@ -107,12 +108,13 @@ const sponsors = {
{ image: '/sponsors/jellypod.png', name: 'Jellypod', url: 'https://jellypod.ai/' },
],
individual: [
{ image: '/sponsors/pintura-labs.png', name: 'Pintura Labs', url: 'https://pqina.nl/pintura/' },
{ image: 'https://avatars.githubusercontent.com/u/82552321', name: 'Polotno', url: 'https://github.com/polotno-project' },
{ image: 'https://avatars.githubusercontent.com/u/489051', name: 'Roman Rädle', url: 'https://github.com/raedle' },
{ image: 'https://avatars.githubusercontent.com/u/197597', name: 'Christopher Chedeau', url: 'https://github.com/vjeux' },
{ image: 'https://avatars.githubusercontent.com/u/84167135', name: 'Memenome', url: 'https://github.com/memenome' },
{ image: 'https://avatars.githubusercontent.com/u/5913254', name: 'Brandon McConnell', url: 'https://github.com/brandonmcconnell' },
{ image: 'https://avatars.githubusercontent.com/u/9549394', name: 'studnitz', url: 'https://github.com/studnitz' },
{ image: 'https://avatars.githubusercontent.com/u/4714175', name: 'Phoomparin Mano', url: 'https://github.com/heypoom' },
{ image: 'https://avatars.githubusercontent.com/u/504909', name: 'Hirbod', url: 'https://github.com/hirbod' },
{ image: 'https://avatars.githubusercontent.com/u/2698271', name: 'Matthew Gardner', url: 'https://github.com/spheric' },
{ image: 'https://avatars.githubusercontent.com/u/5475819', name: 'AJ Funk', url: 'https://github.com/AJFunk' },
@@ -122,6 +124,8 @@ const sponsors = {
{ image: 'https://avatars.githubusercontent.com/u/38181164', name: 'wcw', url: 'https://github.com/asd55667' },
{ image: 'https://avatars.githubusercontent.com/u/1836701', name: 'Bean Deng', url: 'https://github.com/HADB' },
{ image: 'https://avatars.githubusercontent.com/u/255616819', name: 'cronischarles-del', url: 'https://github.com/cronischarles-del' },
{ image: 'https://avatars.githubusercontent.com/u/37973863', name: 'Gomi', url: 'https://github.com/gxy5202' },
{ image: 'https://avatars.githubusercontent.com/u/36898190', name: 'jepcd', url: 'https://github.com/jepcd' },
{ image: 'https://avatars.githubusercontent.com/u/63088713', name: 'taf2000', url: 'https://github.com/taf2000' },
{ image: 'https://avatars.githubusercontent.com/u/58149663', name: 'H7GhosT', url: 'https://github.com/H7GhosT' },
{ image: 'https://avatars.githubusercontent.com/u/91711202', name: 'ihasq', url: 'https://github.com/ihasq' },
@@ -130,7 +134,6 @@ const sponsors = {
{ image: 'https://avatars.githubusercontent.com/u/3709646', name: 'Rodrigo Belfiore', url: 'https://github.com/roprgm' },
{ image: 'https://avatars.githubusercontent.com/u/31102694', name: 'Aiden Liu', url: 'https://github.com/aidenlx' },
{ image: 'https://avatars.githubusercontent.com/u/41021374', name: 'arthco', url: 'https://github.com/arthtyagi' },
{ image: 'https://avatars.githubusercontent.com/u/36898190', name: 'alakhpc', url: 'https://github.com/alakhpc' },
{ image: 'https://avatars.githubusercontent.com/u/5907357', name: 'Harvey Zhao', url: 'https://github.com/zhw2590582' },
],
};
+22 -3
View File
@@ -1,5 +1,24 @@
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="32" height="32" rx="5.33333" fill="#DAFF99"/>
<path d="M14.3273 9.29964C14.5206 8.97719 14.9878 8.97719 15.1811 9.29964L17.7213 13.5375C17.7634 13.6077 17.8221 13.6664 17.8923 13.7085L22.1301 16.2487C22.4526 16.4419 22.4526 16.9092 22.1301 17.1025L17.8923 19.6427C17.8221 19.6848 17.7634 19.7435 17.7213 19.8137L15.1811 24.0515C14.9878 24.374 14.5206 24.374 14.3273 24.0515L11.7871 19.8137C11.745 19.7435 11.6863 19.6848 11.6161 19.6427L7.37825 17.1025C7.0558 16.9092 7.0558 16.4419 7.37825 16.2487L11.6161 13.7085C11.6863 13.6664 11.745 13.6077 11.7871 13.5375L14.3273 9.29964Z" fill="#2D2D2D"/>
<path d="M22.7374 7.51156C22.7857 7.43094 22.9026 7.43094 22.9509 7.51156L23.7608 8.86276C23.7713 8.88032 23.786 8.895 23.8035 8.90552L25.1548 9.71544C25.2354 9.76376 25.2354 9.88058 25.1548 9.9289L23.8035 10.7388C23.786 10.7493 23.7713 10.764 23.7608 10.7816L22.9509 12.1328C22.9026 12.2134 22.7857 12.2134 22.7374 12.1328L21.9275 10.7816C21.917 10.764 21.9023 10.7493 21.8847 10.7388L20.5335 9.9289C20.4529 9.88058 20.4529 9.76376 20.5335 9.71544L21.8847 8.90552C21.9023 8.895 21.917 8.88032 21.9275 8.86276L22.7374 7.51156Z" fill="#2D2D2D"/>
<g clip-path="url(#clip0_3664_2482)">
<rect width="32" height="32" rx="4" fill="#DAFF99"/>
<path d="M15.736 9.07175C15.8492 8.8828 16.123 8.8828 16.2362 9.07175L18.9703 13.6355C18.9949 13.6766 19.0293 13.711 19.0705 13.7357L23.6322 16.4714C23.821 16.5846 23.821 16.8582 23.6322 16.9715L19.0705 19.7072C19.0293 19.7318 18.9949 19.7662 18.9703 19.8074L16.2362 24.3711C16.123 24.5601 15.8492 24.5601 15.736 24.3711L13.0019 19.8074C12.9772 19.7662 12.9428 19.7318 12.9017 19.7072L8.34001 16.9715C8.15121 16.8582 8.15121 16.5846 8.34001 16.4714L12.9017 13.7357C12.9428 13.711 12.9772 13.6766 13.0019 13.6355L15.736 9.07175Z" fill="#2D2D2D"/>
<g filter="url(#filter0_d_3664_2482)">
<path d="M23.8321 7.41198C23.8604 7.36474 23.9288 7.36474 23.9571 7.41198L24.815 8.84391C24.8212 8.85419 24.8298 8.86279 24.84 8.86896L26.2713 9.72731C26.3185 9.75561 26.3185 9.82401 26.2713 9.85232L24.84 10.7107C24.8298 10.7168 24.8212 10.7254 24.815 10.7357L23.9571 12.1676C23.9288 12.2149 23.8604 12.2149 23.8321 12.1676L22.9742 10.7357C22.9681 10.7254 22.9595 10.7168 22.9492 10.7107L21.5179 9.85232C21.4707 9.82401 21.4707 9.75561 21.5179 9.72731L22.9492 8.86896C22.9595 8.86279 22.9681 8.85419 22.9742 8.84391L23.8321 7.41198Z" fill="#2D2D2D"/>
</g>
</g>
<defs>
<filter id="filter0_d_3664_2482" x="-56.5175" y="-70.623" width="160.824" height="160.826" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="BackgroundImageFix"/>
<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/>
<feOffset/>
<feGaussianBlur stdDeviation="39"/>
<feComposite in2="hardAlpha" operator="out"/>
<feColorMatrix type="matrix" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/>
<feBlend mode="normal" in2="BackgroundImageFix" result="effect1_dropShadow_3664_2482"/>
<feBlend mode="normal" in="SourceGraphic" in2="effect1_dropShadow_3664_2482" result="shape"/>
</filter>
<clipPath id="clip0_3664_2482">
<rect width="32" height="32" rx="4" fill="white"/>
</clipPath>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

+18
View File
@@ -0,0 +1,18 @@
<svg width="46" height="46" viewBox="0 0 46 46" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1993_857)">
<path d="M0 6C0 2.5 2.5 0 6 0H40C43.5 0 46 2.5 46 6V40C46 43.5 43.5 46 40 46C36.5 46 6 46 6 46C2.5 46 0 43.5 0 40V6Z" fill="url(#paint0_radial_1993_857)"/>
<path d="M37.1921 24.4673C36.3173 24.4276 26.9327 24.4276 26.0579 24.4673C25.1831 24.5071 24.5071 25.1831 24.4673 26.0579C24.4276 26.9327 24.4276 36.3173 24.4673 37.1921C24.5071 38.0669 25.1831 38.7429 26.0579 38.7827C26.9327 38.8224 36.3173 38.8224 37.1921 38.7827C38.0669 38.7429 38.7429 38.0669 38.7827 37.1921C38.8224 36.3173 38.8224 26.9327 38.7827 26.0579C38.7429 25.1831 38.0669 24.5071 37.1921 24.4673Z" fill="#F9F9FF"/>
<path d="M7.1875 25.2058C7.18745 24.6153 7.91356 24.2462 8.49451 24.5414L21.1268 30.9599C21.7077 31.2551 21.7078 31.9931 21.1269 32.2884L8.4957 38.7085C7.91483 39.0039 7.18866 38.6348 7.18861 38.0445L7.1875 25.2058Z" fill="#F9F9FF"/>
<path d="M14.375 21.5625C18.3446 21.5625 21.5625 18.3446 21.5625 14.375C21.5625 10.4054 18.3446 7.1875 14.375 7.1875C10.4054 7.1875 7.1875 10.4054 7.1875 14.375C7.1875 18.3446 10.4054 21.5625 14.375 21.5625Z" fill="#F9F9FF"/>
<path d="M25.875 7.1875C25.875 7.1875 35.9375 7.1875 37.375 7.1875C38.8125 7.1875 38.8125 8.625 38.8125 8.625C38.8125 8.625 38.8125 8.625 38.8125 10.0625C38.8125 11.5 37.375 11.5 37.375 11.5L33.7813 11.5V20.125C33.7813 20.125 33.7813 21.5625 32.3438 21.5625C30.9062 21.5625 32.3438 21.5625 30.9062 21.5625C29.4688 21.5625 29.4688 20.125 29.4688 20.125V11.5L25.875 11.5C25.875 11.5 24.4375 11.5 24.4375 10.0625C24.4375 8.625 24.4375 10.0625 24.4375 8.625C24.4375 7.1875 25.875 7.1875 25.875 7.1875Z" fill="#F9F9FF"/>
</g>
<defs>
<radialGradient id="paint0_radial_1993_857" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(23) rotate(90) scale(46 46)">
<stop stop-color="#8278FA"/>
<stop offset="1" stop-color="#5E51F8"/>
</radialGradient>
<clipPath id="clip0_1993_857">
<rect width="46" height="46" fill="white"/>
</clipPath>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 2.0 KiB

+6
View File
@@ -38,6 +38,12 @@ export default tseslint.config(
'dist-docs',
'packages/mp3-encoder/dist',
'packages/mp3-encoder/build',
'packages/ac3/dist',
'packages/ac3/build',
'packages/aac-encoder/dist',
'packages/aac-encoder/build',
'packages/flac-encoder/dist',
'packages/flac-encoder/build',
'eslint.config.mjs',
'docs/.vitepress/cache',
'test/public',
@@ -143,7 +143,7 @@ loadUrlButton.addEventListener('click', () => {
const url = prompt(
'Please enter a URL of a media file. Note that it must be HTTPS and support cross-origin requests, so have the'
+ ' right CORS headers set.',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
'https://remotion.media/BigBuckBunny.mp4',
);
if (!url) {
return;
+1 -1
View File
@@ -680,7 +680,7 @@ loadUrlButton.addEventListener('click', () => {
const url = prompt(
'Please enter a URL of a media file. Note that it must be HTTPS and support cross-origin requests, so have the'
+ ' right CORS headers set.',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
'https://remotion.media/BigBuckBunny.mp4',
);
if (!url) {
return;
@@ -57,6 +57,9 @@ const extractMetadata = (resource: File | string) => {
'Coded width': `${track.codedWidth} pixels`,
'Coded height': `${track.codedHeight} pixels`,
'Rotation': `${track.rotation}° clockwise`,
'Pixel aspect ratio': `${track.pixelAspectRatio.num}:${track.pixelAspectRatio.den}`,
'Display width': `${track.displayWidth} pixels`,
'Display height': `${track.displayHeight} pixels`,
'Transparency': track.canBeTransparent(),
}
: track.isAudioTrack()
@@ -221,7 +224,7 @@ loadUrlButton.addEventListener('click', () => {
const url = prompt(
'Please enter a URL of a media file. Note that it must be HTTPS and support cross-origin requests, so have the'
+ ' right CORS headers set.',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
'https://remotion.media/BigBuckBunny.mp4',
);
if (!url) {
return;
@@ -137,7 +137,7 @@ loadUrlButton.addEventListener('click', () => {
const url = prompt(
'Please enter a URL of a media file. Note that it must be HTTPS and support cross-origin requests, so have the'
+ ' right CORS headers set.',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
'https://remotion.media/BigBuckBunny.mp4',
);
if (!url) {
return;
+63 -6
View File
@@ -1,12 +1,12 @@
{
"name": "mediabunny",
"version": "1.31.1",
"version": "1.38.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "mediabunny",
"version": "1.31.1",
"version": "1.38.0",
"license": "MPL-2.0",
"workspaces": [
"packages/*"
@@ -1413,6 +1413,18 @@
"@jridgewell/sourcemap-codec": "^1.4.14"
}
},
"node_modules/@mediabunny/aac-encoder": {
"resolved": "packages/aac-encoder",
"link": true
},
"node_modules/@mediabunny/ac3": {
"resolved": "packages/ac3",
"link": true
},
"node_modules/@mediabunny/flac-encoder": {
"resolved": "packages/flac-encoder",
"link": true
},
"node_modules/@mediabunny/mp3-encoder": {
"resolved": "packages/mp3-encoder",
"link": true
@@ -7739,9 +7751,9 @@
}
},
"node_modules/mediabunny": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.31.0.tgz",
"integrity": "sha512-nqM+6cOpNC/aDxCAZKnZe7oXnGaCn4rlgprzAmiH6C8GRdOHFnB6bZC0+WXGTT6mtAxXQd+BXuZ2q2zkma7dWg==",
"version": "1.37.0",
"resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.37.0.tgz",
"integrity": "sha512-eV7M9IJ29pr/8RNL1sYtIxNbdMfDMN1hMwMaOFfNLhwuKKGSC+eKwiJFpdVjEJ3zrMA4LGerF4Hps0SENFSAlg==",
"license": "MPL-2.0",
"peer": true,
"workspaces": [
@@ -12063,9 +12075,54 @@
"url": "https://github.com/sponsors/wooorm"
}
},
"packages/aac-encoder": {
"name": "@mediabunny/aac-encoder",
"version": "1.38.0",
"license": "MPL-2.0",
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
}
},
"packages/ac3": {
"name": "@mediabunny/ac3",
"version": "1.38.0",
"license": "MPL-2.0",
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
}
},
"packages/flac-encoder": {
"name": "@mediabunny/flac-encoder",
"version": "1.38.0",
"license": "MPL-2.0",
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
}
},
"packages/mp3-encoder": {
"name": "@mediabunny/mp3-encoder",
"version": "1.31.1",
"version": "1.38.0",
"license": "MPL-2.0",
"devDependencies": {
"@types/emscripten": "^1.40.1"
+8 -7
View File
@@ -1,7 +1,7 @@
{
"name": "mediabunny",
"author": "Vanilagy",
"version": "1.31.1",
"version": "1.38.0",
"description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.",
"type": "module",
"workspaces": [
@@ -29,18 +29,19 @@
},
"sideEffects": false,
"scripts": {
"build": "./build.sh",
"build": "./scripts/build.sh",
"watch": "tsx scripts/bundle.ts --watch",
"lint": "eslint .",
"test": "npx vitest --run",
"test-node": "npm run test node/",
"test-browser": "npm run test browser/",
"check": "rm -rf dist/modules && tsc -p src --stripInternal false && tsc -p packages/mp3-encoder/src --noEmit && tsc -p tsconfig.vitest.json --noEmit && tsc -p scripts --noEmit && tsc -p tsconfig.vite.json --noEmit",
"test": "npm run pre-test && npx vitest --run",
"test-node": "npm run pre-test && npm run test node/",
"test-browser": "npm run pre-test && npm run test browser/",
"pre-test": "tsx scripts/bundle.ts",
"check": "./scripts/check.sh",
"check-docblocks": "tsx scripts/check-docblocks.ts dist/mediabunny.d.ts",
"docs:dev": "vitepress dev docs",
"docs:build": "npm run build && npm run docs:generate && vitepress build docs && npm run examples:build && cp dist/mediabunny.d.ts dist-docs/",
"docs:preview": "vitepress preview docs",
"docs:generate": "tsx scripts/generate-api-docs.ts src/index.ts packages/mp3-encoder/src/index.ts docs/api-config.json",
"docs:generate": "tsx scripts/generate-api-docs.ts src/index.ts packages/mp3-encoder/src/index.ts packages/ac3/src/index.ts packages/aac-encoder/src/index.ts packages/flac-encoder/src/index.ts docs/api-config.json",
"dev": "vite",
"examples:build": "vite build",
"fix-build-import-paths": "tsx scripts/add-import-extensions.ts",
+1
View File
@@ -0,0 +1 @@
build/* linguist-generated
+373
View File
@@ -0,0 +1,373 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.
+156
View File
@@ -0,0 +1,156 @@
# @mediabunny/aac-encoder
[![](https://img.shields.io/npm/v/@mediabunny/aac-encoder)](https://www.npmjs.com/package/@mediabunny/aac-encoder)
[![](https://img.shields.io/bundlephobia/minzip/@mediabunny/aac-encoder)](https://bundlephobia.com/package/@mediabunny/aac-encoder)
[![](https://img.shields.io/npm/dm/@mediabunny/aac-encoder)](https://www.npmjs.com/package/@mediabunny/aac-encoder)
[![](https://img.shields.io/discord/1390044844285497344?logo=discord&label=Discord)](https://discord.gg/hmpkyYuS4U)
<div align="center">
<img src="../../docs/public/mediabunny-logo.svg" width="180" height="180">
</div>
Some browsers lack support for AAC encoding in their WebCodecs implementations. This extension package provides a reliable AAC-LC encoder for use with [Mediabunny](https://github.com/Vanilagy/mediabunny). It is implemented using Mediabunny's [custom coder API](https://mediabunny.dev/guide/supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [FFmpeg](https://ffmpeg.org/)'s AAC encoder under the hood.
> This package, like the rest of Mediabunny, is enabled by its [sponsors](https://mediabunny.dev/#sponsors) and their donations. If you've derived value from this package, please consider [leaving a donation](https://github.com/sponsors/Vanilagy)! 💘
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/aac-encoder
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-aac-encoder.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyAacEncoder`. Use `mediabunny-aac-encoder.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerAacEncoder } from '@mediabunny/aac-encoder';
registerAacEncoder();
```
That's it - Mediabunny now uses the registered AAC encoder automatically.
If you want to be more correct, check for native browser support first:
```ts
import { canEncodeAudio } from 'mediabunny';
import { registerAacEncoder } from '@mediabunny/aac-encoder';
if (!(await canEncodeAudio('aac'))) {
registerAacEncoder();
}
```
## Example
Here, we convert an input file to an MP4 with AAC audio:
```ts
import {
Input,
ALL_FORMATS,
BlobSource,
Output,
BufferTarget,
Mp4OutputFormat,
canEncodeAudio,
Conversion,
} from 'mediabunny';
import { registerAacEncoder } from '@mediabunny/aac-encoder';
if (!(await canEncodeAudio('aac'))) {
// Only register the custom encoder if there's no native support
registerAacEncoder();
}
const input = new Input({
source: new BlobSource(file), // From a file picker, for example
formats: ALL_FORMATS,
});
const output = new Output({
format: new Mp4OutputFormat(),
target: new BufferTarget(),
});
const conversion = await Conversion.init({
input,
output,
});
await conversion.execute();
output.target.buffer; // => ArrayBuffer containing the MP4 file
```
For more ways of using Mediabunny, refer to its [guide](https://mediabunny.dev/guide/introduction).
## Building and development
For simplicity, all built WASM artifacts are included in the repo, since these rarely change. However, here are the instructions for building them from scratch:
[Install Emscripten](https://emscripten.org/docs/getting_started/downloads.html) and clone [FFmpeg](https://github.com/FFmpeg/FFmpeg). Then, from the Mediabunny root and with Emscripten sourced in:
```bash
export FFMPEG_PATH=/path/to/ffmpeg
export MEDIABUNNY_ROOT=$PWD
# Build FFmpeg
cd $FFMPEG_PATH
emmake make distclean
emconfigure ./configure \
--target-os=none \
--arch=x86_32 \
--enable-cross-compile \
--disable-asm \
--disable-x86asm \
--disable-inline-asm \
--disable-programs \
--disable-doc \
--disable-debug \
--disable-all \
--disable-everything \
--disable-autodetect \
--disable-pthreads \
--disable-runtime-cpudetect \
--enable-avcodec \
--enable-encoder=aac \
--cc="emcc" \
--cxx=em++ \
--ar=emar \
--ranlib=emranlib \
--extra-cflags="-DNDEBUG -Oz -flto -msimd128" \
--extra-ldflags="-Oz -flto"
emmake make
# Compile the bridge between JavaScript and FFmpeg's API
cd $MEDIABUNNY_ROOT/packages/aac-encoder
emcc src/bridge.c \
$FFMPEG_PATH/libavcodec/libavcodec.a \
$FFMPEG_PATH/libavutil/libavutil.a \
-I$FFMPEG_PATH \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ENVIRONMENT=web,worker \
-s FILESYSTEM=0 \
-s MALLOC=emmalloc \
-s SUPPORT_LONGJMP=0 \
-s EXPORTED_RUNTIME_METHODS=cwrap,HEAPU8 \
-s EXPORTED_FUNCTIONS=_malloc,_free \
-msimd128 \
-flto \
-Oz \
-o build/aac.js
```
This generates `build/aac.js`, which contains both the JavaScript "glue code" as well as the compiled WASM inlined.
### Building the package
Then, the complete JavaScript package can be built alongside the rest of Mediabunny by running `npm run build` in Mediabunny's root.
+37
View File
@@ -0,0 +1,37 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "dist/modules/src/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "dist/mediabunny-aac-encoder.d.ts"
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
},
"newlineKind": "lf"
}
Binary file not shown.
+55
View File
@@ -0,0 +1,55 @@
{
"name": "@mediabunny/aac-encoder",
"author": "Vanilagy",
"version": "1.38.0",
"description": "AAC encoder extension for Mediabunny, based on FFmpeg.",
"main": "./dist/bundles/mediabunny-aac-encoder.mjs",
"module": "./dist/bundles/mediabunny-aac-encoder.mjs",
"types": "./dist/modules/src/index.d.ts",
"exports": {
"types": "./dist/modules/src/index.d.ts",
"import": "./dist/bundles/mediabunny-aac-encoder.mjs",
"require": "./dist/bundles/mediabunny-aac-encoder.mjs"
},
"files": [
"README.md",
"package.json",
"LICENSE",
"dist",
"src"
],
"browser": {
"worker_threads": false
},
"sideEffects": false,
"license": "MPL-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/Vanilagy/mediabunny.git",
"directory": "packages/aac-encoder"
},
"bugs": {
"url": "https://github.com/Vanilagy/mediabunny/issues"
},
"homepage": "https://mediabunny.dev/guide/extensions/aac-encoder",
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
},
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"keywords": [
"aac",
"encoding",
"codec",
"mediabunny",
"ffmpeg",
"browser",
"wasm",
"polyfill"
]
}
+190
View File
@@ -0,0 +1,190 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <emscripten.h>
#include <stdlib.h>
#include <string.h>
#include "libavcodec/avcodec.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/log.h"
typedef struct {
AVCodecContext *codec_ctx;
AVPacket *packet;
AVFrame *frame;
float *input_buffer;
int input_buffer_size;
int encoded_pts;
int encoded_duration;
} EncoderContext;
EMSCRIPTEN_KEEPALIVE
EncoderContext *init_encoder(int channels, int sample_rate, int bitrate) {
av_log_set_level(AV_LOG_ERROR);
const AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!codec) return NULL;
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) return NULL;
codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
codec_ctx->sample_rate = sample_rate;
codec_ctx->bit_rate = bitrate;
codec_ctx->time_base = (AVRational){1, sample_rate};
AVChannelLayout layout;
av_channel_layout_default(&layout, channels);
av_channel_layout_copy(&codec_ctx->ch_layout, &layout);
av_channel_layout_uninit(&layout);
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVPacket *packet = av_packet_alloc();
if (!packet) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVFrame *frame = av_frame_alloc();
if (!frame) {
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
frame->format = AV_SAMPLE_FMT_FLTP;
frame->sample_rate = sample_rate;
frame->nb_samples = codec_ctx->frame_size;
av_channel_layout_copy(&frame->ch_layout, &codec_ctx->ch_layout);
if (av_frame_get_buffer(frame, 0) < 0) {
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
EncoderContext *ctx = malloc(sizeof(EncoderContext));
if (!ctx) {
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
ctx->codec_ctx = codec_ctx;
ctx->packet = packet;
ctx->frame = frame;
ctx->input_buffer = NULL;
ctx->input_buffer_size = 0;
ctx->encoded_pts = 0;
ctx->encoded_duration = 0;
return ctx;
}
EMSCRIPTEN_KEEPALIVE
int get_encoder_frame_size(EncoderContext *ctx) {
return ctx->codec_ctx->frame_size;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_encoder_extradata(EncoderContext *ctx) {
return ctx->codec_ctx->extradata;
}
EMSCRIPTEN_KEEPALIVE
int get_encoder_extradata_size(EncoderContext *ctx) {
return ctx->codec_ctx->extradata_size;
}
EMSCRIPTEN_KEEPALIVE
float *get_encode_input_ptr(EncoderContext *ctx, int size) {
if (ctx->input_buffer_size < size) {
free(ctx->input_buffer);
ctx->input_buffer = malloc(size);
if (!ctx->input_buffer) {
ctx->input_buffer_size = 0;
return NULL;
}
ctx->input_buffer_size = size;
}
return ctx->input_buffer;
}
EMSCRIPTEN_KEEPALIVE
int send_frame(EncoderContext *ctx, int pts) {
int channels = ctx->codec_ctx->ch_layout.nb_channels;
int frame_size = ctx->frame->nb_samples;
ctx->frame->pts = pts;
// Deinterleave f32 input into the frame's f32-planar planes
float *input = ctx->input_buffer;
for (int ch = 0; ch < channels; ch++) {
float *plane = (float *)ctx->frame->data[ch];
for (int i = 0; i < frame_size; i++) {
plane[i] = input[i * channels + ch];
}
}
return avcodec_send_frame(ctx->codec_ctx, ctx->frame);
}
EMSCRIPTEN_KEEPALIVE
int receive_packet(EncoderContext *ctx) {
int ret = avcodec_receive_packet(ctx->codec_ctx, ctx->packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
}
if (ret < 0) return ret;
ctx->encoded_pts = ctx->packet->pts;
ctx->encoded_duration = ctx->packet->duration;
return ctx->packet->size;
}
EMSCRIPTEN_KEEPALIVE
void flush_encoder_start(EncoderContext *ctx) {
avcodec_send_frame(ctx->codec_ctx, NULL);
}
EMSCRIPTEN_KEEPALIVE
void reset_encoder(EncoderContext *ctx) {
avcodec_flush_buffers(ctx->codec_ctx);
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_encoded_data(EncoderContext *ctx) {
return ctx->packet->data;
}
EMSCRIPTEN_KEEPALIVE
int get_encoded_pts(EncoderContext *ctx) {
return ctx->encoded_pts;
}
EMSCRIPTEN_KEEPALIVE
int get_encoded_duration(EncoderContext *ctx) {
return ctx->encoded_duration;
}
EMSCRIPTEN_KEEPALIVE
void close_encoder(EncoderContext *ctx) {
free(ctx->input_buffer);
av_frame_free(&ctx->frame);
av_packet_free(&ctx->packet);
avcodec_free_context(&ctx->codec_ctx);
free(ctx);
}
+194
View File
@@ -0,0 +1,194 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import createModule from '../build/aac';
import type { PacketInfo, WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
type ExtendedEmscriptenModule = EmscriptenModule & {
cwrap: typeof cwrap;
};
let module: ExtendedEmscriptenModule;
let modulePromise: Promise<ExtendedEmscriptenModule> | null = null;
let initEncoderFn: (channels: number, sampleRate: number, bitrate: number) => number;
let getEncoderFrameSize: (ctx: number) => number;
let getEncoderExtradata: (ctx: number) => number;
let getEncoderExtradataSize: (ctx: number) => number;
let getEncodeInputPtr: (ctx: number, size: number) => number;
let sendFrameFn: (ctx: number, pts: number) => number;
let receivePacketFn: (ctx: number) => number;
let flushEncoderStartFn: (ctx: number) => void;
let resetEncoderFn: (ctx: number) => void;
let getEncodedData: (ctx: number) => number;
let getEncodedPts: (ctx: number) => number;
let getEncodedDuration: (ctx: number) => number;
const ensureModule = async () => {
if (!module) {
if (modulePromise) {
return modulePromise;
}
modulePromise = createModule() as Promise<ExtendedEmscriptenModule>;
module = await modulePromise;
modulePromise = null;
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number', 'number']);
getEncoderFrameSize = module.cwrap('get_encoder_frame_size', 'number', ['number']);
getEncoderExtradata = module.cwrap('get_encoder_extradata', 'number', ['number']);
getEncoderExtradataSize = module.cwrap('get_encoder_extradata_size', 'number', ['number']);
getEncodeInputPtr = module.cwrap('get_encode_input_ptr', 'number', ['number', 'number']);
sendFrameFn = module.cwrap('send_frame', 'number', ['number', 'number']);
receivePacketFn = module.cwrap('receive_packet', 'number', ['number']);
flushEncoderStartFn = module.cwrap('flush_encoder_start', null, ['number']);
resetEncoderFn = module.cwrap('reset_encoder', null, ['number']);
getEncodedData = module.cwrap('get_encoded_data', 'number', ['number']);
getEncodedPts = module.cwrap('get_encoded_pts', 'number', ['number']);
getEncodedDuration = module.cwrap('get_encoded_duration', 'number', ['number']);
}
};
const initEncoder = async (
numberOfChannels: number,
sampleRate: number,
bitrate: number,
) => {
await ensureModule();
const ctx = initEncoderFn(numberOfChannels, sampleRate, bitrate);
if (ctx === 0) {
throw new Error('Failed to initialize AAC encoder.');
}
const frameSize = getEncoderFrameSize(ctx);
const extradataPtr = getEncoderExtradata(ctx);
const extradataSize = getEncoderExtradataSize(ctx);
const extradata = module.HEAPU8.slice(extradataPtr, extradataPtr + extradataSize).buffer;
return { ctx, frameSize, extradata };
};
const drainPackets = (ctx: number) => {
const packets: PacketInfo[] = [];
let size: number;
while ((size = receivePacketFn(ctx)) > 0) {
const ptr = getEncodedData(ctx);
const encodedData = module.HEAPU8.slice(ptr, ptr + size).buffer;
const pts = getEncodedPts(ctx);
const duration = getEncodedDuration(ctx);
packets.push({ encodedData, pts, duration });
}
return packets;
};
const encode = (ctx: number, audioData: ArrayBuffer, timestamp: number) => {
const audioBytes = new Uint8Array(audioData);
const inputPtr = getEncodeInputPtr(ctx, audioBytes.length);
if (inputPtr === 0) {
throw new Error('Failed to allocate encoder input buffer.');
}
module.HEAPU8.set(audioBytes, inputPtr);
const ret = sendFrameFn(ctx, timestamp);
if (ret < 0) {
throw new Error(`Encode failed with error code ${ret}.`);
}
return drainPackets(ctx);
};
const onMessage = (data: { id: number; command: WorkerCommand }) => {
const { id, command } = data;
const handleCommand = async (): Promise<void> => {
try {
let result: WorkerResponseData;
const transferables: Transferable[] = [];
switch (command.type) {
case 'init': {
const { ctx, frameSize, extradata } = await initEncoder(
command.data.numberOfChannels,
command.data.sampleRate,
command.data.bitrate,
);
result = { type: command.type, ctx, frameSize, extradata };
transferables.push(extradata);
}; break;
case 'encode': {
const packets = encode(
command.data.ctx,
command.data.audioData,
command.data.timestamp,
);
for (const p of packets) {
transferables.push(p.encodedData);
}
result = { type: command.type, packets };
}; break;
case 'flush': {
flushEncoderStartFn(command.data.ctx);
const packets = drainPackets(command.data.ctx);
for (const p of packets) {
transferables.push(p.encodedData);
}
resetEncoderFn(command.data.ctx);
result = { type: command.type, packets };
}; break;
}
const response: WorkerResponse = {
id,
success: true,
data: result,
};
sendMessage(response, transferables);
} catch (error: unknown) {
const response: WorkerResponse = {
id,
success: false,
error,
};
sendMessage(response);
}
};
void handleCommand();
};
const sendMessage = (data: unknown, transferables?: Transferable[]) => {
if (parentPort) {
parentPort.postMessage(data, transferables ?? []);
} else {
self.postMessage(data, { transfer: transferables ?? [] });
}
};
let parentPort: {
postMessage: (data: unknown, transferables?: Transferable[]) => void;
on: (event: string, listener: (data: never) => void) => void;
} | null = null;
if (typeof self === 'undefined') {
const workerModule = 'worker_threads';
// eslint-disable-next-line @stylistic/max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access
parentPort = require(workerModule).parentPort;
}
if (parentPort) {
parentPort.on('message', onMessage);
} else {
self.addEventListener('message', event => onMessage(event.data as { id: number; command: WorkerCommand }));
}
+305
View File
@@ -0,0 +1,305 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
type AdtsHeaderTemplate,
buildAdtsHeaderTemplate,
parseAacAudioSpecificConfig,
writeAdtsFrameLength,
} from '../../../shared/aac-misc';
import {
CustomAudioEncoder,
AudioCodec,
AudioSample,
EncodedPacket,
registerEncoder,
} from 'mediabunny';
import type { PacketInfo, WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
import createWorker from './encode.worker';
const AAC_SAMPLE_RATES = [
96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350,
];
class AacEncoder extends CustomAudioEncoder {
private worker: Worker | null = null;
private nextMessageId = 0;
private pendingMessages = new Map<number, {
resolve: (value: WorkerResponseData) => void;
reject: (reason?: unknown) => void;
}>();
private ctx = 0;
private encoderFrameSize = 0;
private sampleRate = 0;
private numberOfChannels = 0;
private chunkMetadata: EncodedAudioChunkMetadata = {};
private useAdts = false;
private adtsHeaderTemplate: AdtsHeaderTemplate | null = null;
private description: Uint8Array | null = null;
// Accumulate interleaved f32 samples until we have a full frame
private pendingBuffer = new Float32Array(2 ** 16);
private pendingFrames = 0;
private nextSampleTimestampInSamples: number | null = null;
private nextPacketTimestampInSamples: number | null = null;
static override supports(codec: AudioCodec, config: AudioEncoderConfig): boolean {
return codec === 'aac'
&& config.numberOfChannels >= 1
&& config.numberOfChannels <= 8
&& AAC_SAMPLE_RATES.includes(config.sampleRate)
&& config.bitrate !== undefined;
}
async init() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
this.worker = (await createWorker()) as Worker;
const onMessage = (data: WorkerResponse) => {
const pending = this.pendingMessages.get(data.id);
assert(pending !== undefined);
this.pendingMessages.delete(data.id);
if (data.success) {
pending.resolve(data.data);
} else {
pending.reject(data.error);
}
};
if (this.worker.addEventListener) {
this.worker.addEventListener('message', event => onMessage(event.data as WorkerResponse));
} else {
const nodeWorker = this.worker as unknown as {
on: (event: string, listener: (data: never) => void) => void;
};
nodeWorker.on('message', onMessage);
}
assert(this.config.bitrate !== undefined);
this.sampleRate = this.config.sampleRate;
this.numberOfChannels = this.config.numberOfChannels;
const result = await this.sendCommand({
type: 'init',
data: {
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
bitrate: this.config.bitrate,
},
});
this.ctx = result.ctx;
this.encoderFrameSize = result.frameSize;
// The ffmpeg encoder provides an AudioSpecificConfig as extradata after init
const description = new Uint8Array(result.extradata);
const aacConfig = (this.config as { aac?: { format?: 'aac' | 'adts' } }).aac;
this.useAdts = aacConfig?.format === 'adts';
if (this.useAdts) {
const audioSpecificConfig = parseAacAudioSpecificConfig(description);
this.adtsHeaderTemplate = buildAdtsHeaderTemplate(audioSpecificConfig);
}
this.description = this.useAdts ? null : description;
this.resetInternalState();
}
private resetInternalState() {
this.pendingFrames = 0;
this.nextSampleTimestampInSamples = null;
this.nextPacketTimestampInSamples = null;
this.chunkMetadata = {
decoderConfig: {
codec: 'mp4a.40.2',
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
...(this.description ? { description: this.description } : {}),
},
};
}
async encode(audioSample: AudioSample) {
if (this.nextSampleTimestampInSamples === null) {
this.nextSampleTimestampInSamples = Math.round(audioSample.timestamp * this.sampleRate);
this.nextPacketTimestampInSamples = this.nextSampleTimestampInSamples;
}
const channels = this.numberOfChannels;
const incomingFrames = audioSample.numberOfFrames;
// Extract interleaved f32 data
const totalBytes = audioSample.allocationSize({ format: 'f32', planeIndex: 0 });
const audioBytes = new Uint8Array(totalBytes);
audioSample.copyTo(audioBytes, { format: 'f32', planeIndex: 0 });
const incomingData = new Float32Array(audioBytes.buffer);
const requiredSamples = (this.pendingFrames + incomingFrames) * channels;
if (requiredSamples > this.pendingBuffer.length) {
let newSize = this.pendingBuffer.length;
while (newSize < requiredSamples) {
newSize *= 2;
}
const newBuffer = new Float32Array(newSize);
newBuffer.set(this.pendingBuffer.subarray(0, this.pendingFrames * channels));
this.pendingBuffer = newBuffer;
}
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
this.pendingFrames += incomingFrames;
while (this.pendingFrames >= this.encoderFrameSize) {
await this.encodeOneFrame();
}
}
async flush() {
// Pad remaining samples with silence to fill a full frame
if (this.pendingFrames > 0) {
const channels = this.numberOfChannels;
const frameSize = this.encoderFrameSize;
const usedSamples = this.pendingFrames * channels;
const frameSamples = frameSize * channels;
this.pendingBuffer.fill(0, usedSamples, frameSamples);
this.pendingFrames = frameSize;
await this.encodeOneFrame();
}
const result = await this.sendCommand({ type: 'flush', data: { ctx: this.ctx } });
this.emitPackets(result.packets);
this.resetInternalState();
}
close() {
this.worker?.terminate();
}
private async encodeOneFrame() {
assert(this.nextSampleTimestampInSamples !== null);
assert(this.nextPacketTimestampInSamples !== null);
const channels = this.numberOfChannels;
const frameSize = this.encoderFrameSize;
const frameSamples = frameSize * channels;
const frameData = this.pendingBuffer.slice(0, frameSamples);
// Shift remaining using copyWithin
this.pendingFrames -= frameSize;
if (this.pendingFrames > 0) {
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
}
const audioData = frameData.buffer;
const result = await this.sendCommand({
type: 'encode',
data: {
ctx: this.ctx,
audioData,
timestamp: this.nextSampleTimestampInSamples,
},
}, [audioData]);
this.nextSampleTimestampInSamples += frameSize;
this.emitPackets(result.packets);
}
private emitPackets(packets: PacketInfo[]) {
assert(this.nextPacketTimestampInSamples !== null);
for (const p of packets) {
let data = new Uint8Array(p.encodedData);
if (this.useAdts) {
assert(this.adtsHeaderTemplate !== null);
const { header, bitstream } = this.adtsHeaderTemplate;
const frameLength = header.byteLength + data.byteLength;
writeAdtsFrameLength(bitstream, frameLength);
const adtsFrame = new Uint8Array(frameLength);
adtsFrame.set(header, 0);
adtsFrame.set(data, header.byteLength);
data = adtsFrame;
}
const packet = new EncodedPacket(
data,
'key',
this.nextPacketTimestampInSamples / this.sampleRate,
p.duration / this.sampleRate,
);
this.nextPacketTimestampInSamples += p.duration;
this.onPacket(
packet,
this.chunkMetadata,
);
this.chunkMetadata = {};
}
}
private sendCommand<T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
) {
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
const id = this.nextMessageId++;
this.pendingMessages.set(id, {
resolve: resolve as (value: WorkerResponseData) => void,
reject,
});
assert(this.worker);
if (transferables) {
this.worker.postMessage({ id, command }, transferables);
} else {
this.worker.postMessage({ id, command });
}
});
}
}
/**
* Registers the AAC encoder, which Mediabunny will then use automatically when applicable. Make sure to call this
* function before starting any encoding task.
*
* Preferably, wrap the call in a condition to avoid overriding any native AAC encoder:
*
* ```ts
* import { canEncodeAudio } from 'mediabunny';
* import { registerAacEncoder } from '@mediabunny/aac-encoder';
*
* if (!(await canEncodeAudio('aac'))) {
* registerAacEncoder();
* }
* ```
*
* @group \@mediabunny/aac-encoder
* @public
*/
export const registerAacEncoder = () => {
registerEncoder(AacEncoder);
};
function assert(x: unknown): asserts x {
if (!x) {
throw new Error('Assertion failed.');
}
}
+20
View File
@@ -0,0 +1,20 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
const AAC_ENCODER_LOADED_SYMBOL = Symbol.for('@mediabunny/aac-encoder loaded');
if ((globalThis as Record<symbol, unknown>)[AAC_ENCODER_LOADED_SYMBOL]) {
console.error(
'[WARNING]\n@mediabunny/aac-encoder was loaded twice.'
+ ' This will likely cause the encoder not to work correctly.'
+ ' Check if multiple dependencies are importing different versions of @mediabunny/aac-encoder,'
+ ' or if something is being bundled incorrectly.',
);
}
(globalThis as Record<symbol, unknown>)[AAC_ENCODER_LOADED_SYMBOL] = true;
export { registerAacEncoder } from './encoder';
+57
View File
@@ -0,0 +1,57 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
export type WorkerCommand = {
type: 'init';
data: {
numberOfChannels: number;
sampleRate: number;
bitrate: number;
};
} | {
type: 'encode';
data: {
ctx: number;
audioData: ArrayBuffer;
timestamp: number;
};
} | {
type: 'flush';
data: {
ctx: number;
};
};
export type PacketInfo = {
encodedData: ArrayBuffer;
pts: number;
duration: number;
};
export type WorkerResponseData = {
type: 'init';
ctx: number;
frameSize: number;
extradata: ArrayBuffer;
} | {
type: 'encode';
packets: PacketInfo[];
} | {
type: 'flush';
packets: PacketInfo[];
};
export type WorkerResponse = {
id: number;
} & ({
success: true;
data: WorkerResponseData;
} | {
success: false;
error: unknown;
});
@@ -1,7 +1,8 @@
{
"extends": "../../../tsconfig.json",
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../dist/modules",
"composite": true,
"outDir": "./dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
@@ -10,14 +11,14 @@
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../../src/index.ts"],
"mediabunny": ["../../src/index.ts"],
},
},
"include": [
"**/*",
"../../../shared/**/*"
"./src/**/*",
"./build/**/*",
],
"references": [
{ "path": "../../../src" }
{ "path": "../../src" }
]
}
}
+4
View File
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.json"]
}
+1
View File
@@ -0,0 +1 @@
build/* linguist-generated
+373
View File
@@ -0,0 +1,373 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.
+108
View File
@@ -0,0 +1,108 @@
# @mediabunny/ac3
[![](https://img.shields.io/npm/v/@mediabunny/ac3)](https://www.npmjs.com/package/@mediabunny/ac3)
[![](https://img.shields.io/bundlephobia/minzip/@mediabunny/ac3)](https://bundlephobia.com/package/@mediabunny/ac3)
[![](https://img.shields.io/npm/dm/@mediabunny/ac3)](https://www.npmjs.com/package/@mediabunny/ac3)
[![](https://img.shields.io/discord/1390044844285497344?logo=discord&label=Discord)](https://discord.gg/hmpkyYuS4U)
<div align="center">
<img src="../../docs/public/mediabunny-logo.svg" width="180" height="180">
</div>
Browsers have no support for AC-3 (Dolby Digital) or E-AC-3 (Dolby Digital Plus) in their WebCodecs implementations. This extension package provides both a decoder and encoder for use with [Mediabunny](https://github.com/Vanilagy/mediabunny), allowing you to decode and encode these codecs directly in the browser. It is implemented using Mediabunny's [custom coder API](https://mediabunny.dev/guide/supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [FFmpeg](https://ffmpeg.org/)'s AC-3 and E-AC-3 coders under the hood.
> This package, like the rest of Mediabunny, is enabled by its [sponsors](https://mediabunny.dev/#sponsors) and their donations. If you've derived value from this package, please consider [leaving a donation](https://github.com/sponsors/Vanilagy)! 💘
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/ac3
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-ac3.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyAc3`. Use `mediabunny-ac3.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerAc3Decoder, registerAc3Encoder } from '@mediabunny/ac3';
registerAc3Decoder();
registerAc3Encoder();
```
That's it - Mediabunny now uses the registered AC-3/E-AC-3 decoder and encoder automatically.
## Building and development
For simplicity, all built WASM artifacts are included in the repo, since these rarely change. However, here are the instructions for building them from scratch:
[Install Emscripten](https://emscripten.org/docs/getting_started/downloads.html) and clone [FFmpeg](https://github.com/FFmpeg/FFmpeg). Then, from the Mediabunny root and with Emscripten sourced in:
```bash
export FFMPEG_PATH=/path/to/ffmpeg
export MEDIABUNNY_ROOT=$PWD
# Build FFmpeg
cd $FFMPEG_PATH
emmake make distclean
emconfigure ./configure \
--target-os=none \
--arch=x86_32 \
--enable-cross-compile \
--disable-asm \
--disable-x86asm \
--disable-inline-asm \
--disable-programs \
--disable-doc \
--disable-debug \
--disable-all \
--disable-everything \
--disable-autodetect \
--disable-pthreads \
--disable-runtime-cpudetect \
--enable-avcodec \
--enable-decoder=ac3 \
--enable-decoder=eac3 \
--enable-encoder=ac3 \
--enable-encoder=eac3 \
--cc="emcc" \
--cxx=em++ \
--ar=emar \
--ranlib=emranlib \
--extra-cflags="-DNDEBUG -Oz -flto -msimd128" \
--extra-ldflags="-Oz -flto"
emmake make
# Compile the bridge between JavaScript and FFmpeg's API
cd $MEDIABUNNY_ROOT/packages/ac3
emcc src/bridge.c \
$FFMPEG_PATH/libavcodec/libavcodec.a \
$FFMPEG_PATH/libavutil/libavutil.a \
-I$FFMPEG_PATH \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ENVIRONMENT=web,worker \
-s FILESYSTEM=0 \
-s MALLOC=emmalloc \
-s SUPPORT_LONGJMP=0 \
-s EXPORTED_RUNTIME_METHODS=cwrap,HEAPU8 \
-s EXPORTED_FUNCTIONS=_malloc,_free \
-msimd128 \
-flto \
-Oz \
-o build/ac3.js
```
This generates `build/ac3.js`, which contains both the JavaScript "glue code" as well as the compiled WASM inlined.
### Building the package
Then, the complete JavaScript package can be built alongside the rest of Mediabunny by running `npm run build` in Mediabunny's root.
+37
View File
@@ -0,0 +1,37 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "dist/modules/src/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "dist/mediabunny-ac3.d.ts"
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
},
"newlineKind": "lf"
}
BIN
View File
Binary file not shown.
+59
View File
@@ -0,0 +1,59 @@
{
"name": "@mediabunny/ac3",
"author": "Vanilagy",
"version": "1.38.0",
"description": "AC-3 and E-AC-3 (Dolby Digital) decoder and encoder extension for Mediabunny, based on FFmpeg.",
"main": "./dist/bundles/mediabunny-ac3.mjs",
"module": "./dist/bundles/mediabunny-ac3.mjs",
"types": "./dist/modules/src/index.d.ts",
"exports": {
"types": "./dist/modules/src/index.d.ts",
"import": "./dist/bundles/mediabunny-ac3.mjs",
"require": "./dist/bundles/mediabunny-ac3.mjs"
},
"files": [
"README.md",
"package.json",
"LICENSE",
"dist",
"src"
],
"browser": {
"worker_threads": false
},
"sideEffects": false,
"license": "MPL-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/Vanilagy/mediabunny.git",
"directory": "packages/ac3"
},
"bugs": {
"url": "https://github.com/Vanilagy/mediabunny/issues"
},
"homepage": "https://mediabunny.dev/guide/extensions/ac3",
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
},
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"keywords": [
"ac3",
"eac3",
"dolby",
"dolby-digital",
"encoding",
"decoding",
"codec",
"mediabunny",
"ffmpeg",
"browser",
"wasm",
"polyfill"
]
}
+289
View File
@@ -0,0 +1,289 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <emscripten.h>
#include <stdlib.h>
#include <string.h>
#include "libavcodec/avcodec.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
typedef struct {
AVCodecContext *codec_ctx;
AVPacket *packet;
AVFrame *frame;
} DecoderContext;
EMSCRIPTEN_KEEPALIVE
DecoderContext *init_decoder(int codec_id) {
enum AVCodecID av_codec_id = codec_id == 0 ? AV_CODEC_ID_AC3 : AV_CODEC_ID_EAC3;
const AVCodec *codec = avcodec_find_decoder(av_codec_id);
if (!codec) return NULL;
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) return NULL;
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVPacket *packet = av_packet_alloc();
if (!packet) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVFrame *frame = av_frame_alloc();
if (!frame) {
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
DecoderContext *ctx = malloc(sizeof(DecoderContext));
if (!ctx) {
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
ctx->codec_ctx = codec_ctx;
ctx->packet = packet;
ctx->frame = frame;
return ctx;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *configure_decode_packet(DecoderContext *ctx, int size) {
if (av_new_packet(ctx->packet, size) < 0) {
return NULL;
}
return ctx->packet->data;
}
EMSCRIPTEN_KEEPALIVE
int decode_packet(DecoderContext *ctx, int pts) {
ctx->packet->pts = pts;
int ret = avcodec_send_packet(ctx->codec_ctx, ctx->packet);
av_packet_unref(ctx->packet);
if (ret < 0) return ret;
ret = avcodec_receive_frame(ctx->codec_ctx, ctx->frame);
if (ret < 0) return ret;
return 0;
}
EMSCRIPTEN_KEEPALIVE
int get_decoded_format(DecoderContext *ctx) {
return ctx->frame->format;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_decoded_plane_ptr(DecoderContext *ctx, int plane) {
return ctx->frame->data[plane];
}
EMSCRIPTEN_KEEPALIVE
int get_decoded_channels(DecoderContext *ctx) {
return ctx->frame->ch_layout.nb_channels;
}
EMSCRIPTEN_KEEPALIVE
int get_decoded_sample_rate(DecoderContext *ctx) {
return ctx->frame->sample_rate;
}
EMSCRIPTEN_KEEPALIVE
int get_decoded_sample_count(DecoderContext *ctx) {
return ctx->frame->nb_samples;
}
EMSCRIPTEN_KEEPALIVE
int get_decoded_pts(DecoderContext *ctx) {
return (int)ctx->frame->pts;
}
EMSCRIPTEN_KEEPALIVE
void flush_decoder(DecoderContext *ctx) {
avcodec_send_packet(ctx->codec_ctx, NULL);
while (avcodec_receive_frame(ctx->codec_ctx, ctx->frame) == 0) {}
avcodec_flush_buffers(ctx->codec_ctx);
}
EMSCRIPTEN_KEEPALIVE
void close_decoder(DecoderContext *ctx) {
av_frame_free(&ctx->frame);
av_packet_free(&ctx->packet);
avcodec_free_context(&ctx->codec_ctx);
free(ctx);
}
typedef struct {
AVCodecContext *codec_ctx;
AVPacket *packet;
AVFrame *frame;
float *input_buffer;
int input_buffer_size;
int encoded_pts;
int encoded_duration;
} EncoderContext;
EMSCRIPTEN_KEEPALIVE
EncoderContext *init_encoder(int codec_id, int channels, int sample_rate, int bitrate) {
enum AVCodecID av_codec_id = codec_id == 0 ? AV_CODEC_ID_AC3 : AV_CODEC_ID_EAC3;
const AVCodec *codec = avcodec_find_encoder(av_codec_id);
if (!codec) return NULL;
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) return NULL;
codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
codec_ctx->sample_rate = sample_rate;
codec_ctx->bit_rate = bitrate;
codec_ctx->time_base = (AVRational){1, sample_rate};
AVChannelLayout layout;
av_channel_layout_default(&layout, channels);
av_channel_layout_copy(&codec_ctx->ch_layout, &layout);
av_channel_layout_uninit(&layout);
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVPacket *packet = av_packet_alloc();
if (!packet) {
avcodec_free_context(&codec_ctx);
return NULL;
}
AVFrame *frame = av_frame_alloc();
if (!frame) {
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
// The frame has a fixed format, so let's create it now:
frame->format = AV_SAMPLE_FMT_FLTP;
frame->sample_rate = sample_rate;
frame->nb_samples = codec_ctx->frame_size;
av_channel_layout_copy(&frame->ch_layout, &codec_ctx->ch_layout);
if (av_frame_get_buffer(frame, 0) < 0) {
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
EncoderContext *ctx = malloc(sizeof(EncoderContext));
if (!ctx) {
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_free_context(&codec_ctx);
return NULL;
}
ctx->codec_ctx = codec_ctx;
ctx->packet = packet;
ctx->frame = frame;
ctx->input_buffer = NULL;
ctx->input_buffer_size = 0;
ctx->encoded_pts = 0;
ctx->encoded_duration = 0;
return ctx;
}
EMSCRIPTEN_KEEPALIVE
int get_encoder_frame_size(EncoderContext *ctx) {
return ctx->codec_ctx->frame_size;
}
EMSCRIPTEN_KEEPALIVE
float *get_encode_input_ptr(EncoderContext *ctx, int size) {
if (ctx->input_buffer_size < size) {
free(ctx->input_buffer);
ctx->input_buffer = malloc(size);
if (!ctx->input_buffer) {
ctx->input_buffer_size = 0;
return NULL;
}
ctx->input_buffer_size = size;
}
return ctx->input_buffer;
}
EMSCRIPTEN_KEEPALIVE
int encode_frame(EncoderContext *ctx, int pts) {
int channels = ctx->codec_ctx->ch_layout.nb_channels;
int frame_size = ctx->frame->nb_samples;
ctx->frame->pts = pts;
// Deinterleave f32 input into the frame's f32-planar planes
float *input = ctx->input_buffer;
for (int ch = 0; ch < channels; ch++) {
float *plane = (float *)ctx->frame->data[ch];
for (int i = 0; i < frame_size; i++) {
plane[i] = input[i * channels + ch];
}
}
int ret = avcodec_send_frame(ctx->codec_ctx, ctx->frame);
if (ret < 0) return ret;
ret = avcodec_receive_packet(ctx->codec_ctx, ctx->packet);
if (ret < 0) return ret;
ctx->encoded_pts = ctx->packet->pts;
ctx->encoded_duration = ctx->packet->duration;
return ctx->packet->size;
}
EMSCRIPTEN_KEEPALIVE
void flush_encoder(EncoderContext *ctx) {
avcodec_send_frame(ctx->codec_ctx, NULL);
while (avcodec_receive_packet(ctx->codec_ctx, ctx->packet) == 0) {
av_packet_unref(ctx->packet);
}
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_encoded_data(EncoderContext *ctx) {
return ctx->packet->data;
}
EMSCRIPTEN_KEEPALIVE
int get_encoded_pts(EncoderContext *ctx) {
return ctx->encoded_pts;
}
EMSCRIPTEN_KEEPALIVE
int get_encoded_duration(EncoderContext *ctx) {
return ctx->encoded_duration;
}
EMSCRIPTEN_KEEPALIVE
void close_encoder(EncoderContext *ctx) {
free(ctx->input_buffer);
av_frame_free(&ctx->frame);
av_packet_free(&ctx->packet);
avcodec_free_context(&ctx->codec_ctx);
free(ctx);
}
+306
View File
@@ -0,0 +1,306 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import createModule from '../build/ac3';
import type { WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
type ExtendedEmscriptenModule = EmscriptenModule & {
cwrap: typeof cwrap;
};
let module: ExtendedEmscriptenModule;
let modulePromise: Promise<ExtendedEmscriptenModule> | null = null;
let initDecoderFn: (codecId: number) => number;
let configureDecodePacket: (ctx: number, size: number) => number;
let decodePacket: (ctx: number, pts: number) => number;
let getDecodedFormat: (ctx: number) => number;
let getDecodedPlanePtr: (ctx: number, plane: number) => number;
let getDecodedChannels: (ctx: number) => number;
let getDecodedSampleRate: (ctx: number) => number;
let getDecodedSampleCount: (ctx: number) => number;
let getDecodedPts: (ctx: number) => number;
let flushDecoderFn: (ctx: number) => void;
let closeDecoderFn: (ctx: number) => void;
let initEncoderFn: (codecId: number, channels: number, sampleRate: number, bitrate: number) => number;
let getEncoderFrameSize: (ctx: number) => number;
let getEncodeInputPtr: (ctx: number, size: number) => number;
let encodeFrameFn: (ctx: number, pts: number) => number;
let flushEncoderFn: (ctx: number) => void;
let getEncodedData: (ctx: number) => number;
let getEncodedPts: (ctx: number) => number;
let getEncodedDuration: (ctx: number) => number;
let closeEncoderFn: (ctx: number) => void;
const codecToId = (codec: string) => codec === 'ac3' ? 0 : 1;
const ensureModule = async () => {
if (!module) {
if (modulePromise) {
// If we don't do this we can have a race condition
return modulePromise;
}
modulePromise = createModule() as Promise<ExtendedEmscriptenModule>;
module = await modulePromise;
modulePromise = null;
initDecoderFn = module.cwrap('init_decoder', 'number', ['number']);
configureDecodePacket = module.cwrap('configure_decode_packet', 'number', ['number', 'number']);
decodePacket = module.cwrap('decode_packet', 'number', ['number', 'number']);
getDecodedFormat = module.cwrap('get_decoded_format', 'number', ['number']);
getDecodedPlanePtr = module.cwrap('get_decoded_plane_ptr', 'number', ['number', 'number']);
getDecodedChannels = module.cwrap('get_decoded_channels', 'number', ['number']);
getDecodedSampleRate = module.cwrap('get_decoded_sample_rate', 'number', ['number']);
getDecodedSampleCount = module.cwrap('get_decoded_sample_count', 'number', ['number']);
getDecodedPts = module.cwrap('get_decoded_pts', 'number', ['number']);
flushDecoderFn = module.cwrap('flush_decoder', null, ['number']);
closeDecoderFn = module.cwrap('close_decoder', null, ['number']);
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number', 'number', 'number']);
getEncoderFrameSize = module.cwrap('get_encoder_frame_size', 'number', ['number']);
getEncodeInputPtr = module.cwrap('get_encode_input_ptr', 'number', ['number', 'number']);
encodeFrameFn = module.cwrap('encode_frame', 'number', ['number', 'number']);
flushEncoderFn = module.cwrap('flush_encoder', null, ['number']);
getEncodedData = module.cwrap('get_encoded_data', 'number', ['number']);
getEncodedPts = module.cwrap('get_encoded_pts', 'number', ['number']);
getEncodedDuration = module.cwrap('get_encoded_duration', 'number', ['number']);
closeEncoderFn = module.cwrap('close_encoder', null, ['number']);
}
};
const initDecoder = async (codec: string) => {
await ensureModule();
const ctx = initDecoderFn(codecToId(codec));
if (ctx === 0) {
throw new Error('Failed to initialize AC3 decoder.');
}
return { ctx, frameSize: 0 };
};
// Keys are AVSampleFormat enum values
const AV_FORMAT_MAP: Record<number, { format: AudioSampleFormat; bytesPerSample: number; planar: boolean }> = {
0: { format: 'u8', bytesPerSample: 1, planar: false },
1: { format: 's16', bytesPerSample: 2, planar: false },
2: { format: 's32', bytesPerSample: 4, planar: false },
3: { format: 'f32', bytesPerSample: 4, planar: false },
5: { format: 'u8-planar', bytesPerSample: 1, planar: true },
6: { format: 's16-planar', bytesPerSample: 2, planar: true },
7: { format: 's32-planar', bytesPerSample: 4, planar: true },
8: { format: 'f32-planar', bytesPerSample: 4, planar: true },
};
const decode = (ctx: number, encodedData: ArrayBuffer, timestamp: number) => {
const bytes = new Uint8Array(encodedData);
const dataPtr = configureDecodePacket(ctx, bytes.length);
if (dataPtr === 0) {
throw new Error('Failed to configure decode packet.');
}
module.HEAPU8.set(bytes, dataPtr);
const ret = decodePacket(ctx, timestamp);
if (ret < 0) {
throw new Error(`Decode failed with error code ${ret}.`);
}
const avFormat = getDecodedFormat(ctx);
const info = AV_FORMAT_MAP[avFormat];
if (!info) {
throw new Error(`Unsupported AVSampleFormat: ${avFormat}`);
}
const channels = getDecodedChannels(ctx);
const sampleRate = getDecodedSampleRate(ctx);
const sampleCount = getDecodedSampleCount(ctx);
const pts = getDecodedPts(ctx);
let pcmData: ArrayBuffer;
if (info.planar) {
const planeSize = sampleCount * info.bytesPerSample;
const buffer = new Uint8Array(planeSize * channels);
for (let ch = 0; ch < channels; ch++) {
const ptr = getDecodedPlanePtr(ctx, ch);
buffer.set(module.HEAPU8.subarray(ptr, ptr + planeSize), ch * planeSize);
}
pcmData = buffer.buffer;
} else {
const totalSize = sampleCount * channels * info.bytesPerSample;
const ptr = getDecodedPlanePtr(ctx, 0);
pcmData = module.HEAPU8.slice(ptr, ptr + totalSize).buffer;
}
return { pcmData, format: info.format, channels, sampleRate, sampleCount, pts };
};
const initEncoder = async (
codec: string,
numberOfChannels: number,
sampleRate: number,
bitrate: number,
) => {
await ensureModule();
const ctx = initEncoderFn(codecToId(codec), numberOfChannels, sampleRate, bitrate);
if (ctx === 0) {
throw new Error('Failed to initialize AC3 encoder.');
}
return { ctx, frameSize: getEncoderFrameSize(ctx) };
};
const encode = (ctx: number, audioData: ArrayBuffer, timestamp: number) => {
const audioBytes = new Uint8Array(audioData);
const inputPtr = getEncodeInputPtr(ctx, audioBytes.length);
if (inputPtr === 0) {
throw new Error('Failed to allocate encoder input buffer.');
}
module.HEAPU8.set(audioBytes, inputPtr);
const bytesWritten = encodeFrameFn(ctx, timestamp);
if (bytesWritten < 0) {
throw new Error(`Encode failed with error code ${bytesWritten}.`);
}
const ptr = getEncodedData(ctx);
const encodedData = module.HEAPU8.slice(ptr, ptr + bytesWritten).buffer;
const pts = getEncodedPts(ctx);
const duration = getEncodedDuration(ctx);
return { encodedData, pts, duration };
};
const flushEncoder = (ctx: number) => {
flushEncoderFn(ctx);
};
const onMessage = (data: { id: number; command: WorkerCommand }) => {
const { id, command } = data;
const handleCommand = async (): Promise<void> => {
try {
let result: WorkerResponseData;
const transferables: Transferable[] = [];
switch (command.type) {
case 'init-decoder': {
const { ctx, frameSize } = await initDecoder(command.data.codec);
result = { type: command.type, ctx, frameSize };
}; break;
case 'decode': {
const decoded = decode(command.data.ctx, command.data.encodedData, command.data.timestamp);
result = {
type: command.type,
pcmData: decoded.pcmData,
format: decoded.format,
channels: decoded.channels,
sampleRate: decoded.sampleRate,
sampleCount: decoded.sampleCount,
pts: decoded.pts,
};
transferables.push(decoded.pcmData);
}; break;
case 'flush-decoder': {
flushDecoderFn(command.data.ctx);
result = { type: command.type };
}; break;
case 'close-decoder': {
closeDecoderFn(command.data.ctx);
result = { type: command.type };
}; break;
case 'init-encoder': {
const { ctx, frameSize } = await initEncoder(
command.data.codec,
command.data.numberOfChannels,
command.data.sampleRate,
command.data.bitrate,
);
result = { type: command.type, ctx, frameSize };
}; break;
case 'encode': {
const encoded = encode(
command.data.ctx,
command.data.audioData,
command.data.timestamp,
);
result = {
type: command.type,
encodedData: encoded.encodedData,
pts: encoded.pts,
duration: encoded.duration,
};
transferables.push(encoded.encodedData);
}; break;
case 'flush-encoder': {
flushEncoder(command.data.ctx);
result = { type: command.type };
}; break;
case 'close-encoder': {
closeEncoderFn(command.data.ctx);
result = { type: command.type };
}; break;
}
const response: WorkerResponse = {
id,
success: true,
data: result,
};
sendMessage(response, transferables);
} catch (error: unknown) {
const response: WorkerResponse = {
id,
success: false,
error,
};
sendMessage(response);
}
};
void handleCommand();
};
const sendMessage = (data: unknown, transferables?: Transferable[]) => {
if (parentPort) {
parentPort.postMessage(data, transferables ?? []);
} else {
self.postMessage(data, { transfer: transferables ?? [] });
}
};
let parentPort: {
postMessage: (data: unknown, transferables?: Transferable[]) => void;
on: (event: string, listener: (data: never) => void) => void;
} | null = null;
if (typeof self === 'undefined') {
const workerModule = 'worker_threads';
// eslint-disable-next-line @stylistic/max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access
parentPort = require(workerModule).parentPort;
}
if (parentPort) {
parentPort.on('message', onMessage);
} else {
self.addEventListener('message', event => onMessage(event.data as { id: number; command: WorkerCommand }));
}
+73
View File
@@ -0,0 +1,73 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
CustomAudioDecoder,
AudioCodec,
AudioSample,
EncodedPacket,
registerDecoder,
} from 'mediabunny';
import { sendCommand, refWorker, unrefWorker } from './worker-client';
class Ac3Decoder extends CustomAudioDecoder {
private ctx = 0;
static override supports(codec: AudioCodec): boolean {
return codec === 'ac3' || codec === 'eac3';
}
async init() {
await refWorker();
const result = await sendCommand({
type: 'init-decoder',
data: { codec: this.codec },
});
this.ctx = result.ctx;
}
async decode(packet: EncodedPacket) {
const encodedData = packet.data.slice().buffer;
const timestamp = Math.round(packet.timestamp * this.config.sampleRate);
const result = await sendCommand({
type: 'decode',
data: { ctx: this.ctx, encodedData, timestamp },
}, [encodedData]);
const sample = new AudioSample({
data: result.pcmData,
format: result.format,
numberOfChannels: result.channels,
sampleRate: result.sampleRate,
timestamp: result.pts / result.sampleRate,
});
this.onSample(sample);
}
async flush() {
await sendCommand({ type: 'flush-decoder', data: { ctx: this.ctx } });
}
async close() {
void sendCommand({ type: 'close-decoder', data: { ctx: this.ctx } });
await unrefWorker();
}
}
/**
* Registers AC-3 and E-AC-3 decoders, which Mediabunny will then use automatically when applicable. Make sure to call
* this function before starting any decoding task.
*
* @group \@mediabunny/ac3
* @public
*/
export const registerAc3Decoder = () => {
registerDecoder(Ac3Decoder);
};
+195
View File
@@ -0,0 +1,195 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
CustomAudioEncoder,
AudioCodec,
AudioSample,
EncodedPacket,
registerEncoder,
} from 'mediabunny';
import { sendCommand, refWorker, unrefWorker } from './worker-client';
import { assert } from './shared';
import { AC3_SAMPLE_RATES, EAC3_REDUCED_SAMPLE_RATES } from '../../../shared/ac3-misc';
class Ac3Encoder extends CustomAudioEncoder {
private ctx = 0;
private encoderFrameSize = 0;
private sampleRate = 0;
private numberOfChannels = 0;
private chunkMetadata: EncodedAudioChunkMetadata = {};
// Accumulate interleaved f32 samples until we have a full frame
private pendingBuffer = new Float32Array(2 ** 16);
private pendingFrames = 0;
private nextSampleTimestampInSamples: number | null = null;
private nextPacketTimestampInSamples: number | null = null;
static override supports(codec: AudioCodec, config: AudioEncoderConfig): boolean {
const sampleRates = codec === 'eac3'
? [...AC3_SAMPLE_RATES, ...EAC3_REDUCED_SAMPLE_RATES]
: AC3_SAMPLE_RATES;
return (codec === 'ac3' || codec === 'eac3')
&& config.numberOfChannels >= 1
&& config.numberOfChannels <= 8
&& sampleRates.includes(config.sampleRate)
&& config.bitrate !== undefined;
}
async init() {
await refWorker();
assert(this.config.bitrate !== undefined);
this.sampleRate = this.config.sampleRate;
this.numberOfChannels = this.config.numberOfChannels;
const result = await sendCommand({
type: 'init-encoder',
data: {
codec: this.codec,
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
bitrate: this.config.bitrate,
},
});
this.ctx = result.ctx;
this.encoderFrameSize = result.frameSize;
this.resetInternalState();
}
private resetInternalState() {
this.pendingFrames = 0;
this.nextSampleTimestampInSamples = null;
this.nextPacketTimestampInSamples = null;
this.chunkMetadata = {
decoderConfig: {
codec: this.codec === 'ac3' ? 'ac-3' : 'ec-3',
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
},
};
}
async encode(audioSample: AudioSample) {
if (this.nextSampleTimestampInSamples === null) {
this.nextSampleTimestampInSamples = Math.round(audioSample.timestamp * this.sampleRate);
this.nextPacketTimestampInSamples = this.nextSampleTimestampInSamples;
}
const channels = this.numberOfChannels;
const incomingFrames = audioSample.numberOfFrames;
// Extract interleaved f32 data
const totalBytes = audioSample.allocationSize({ format: 'f32', planeIndex: 0 });
const audioBytes = new Uint8Array(totalBytes);
audioSample.copyTo(audioBytes, { format: 'f32', planeIndex: 0 });
const incomingData = new Float32Array(audioBytes.buffer);
const requiredSamples = (this.pendingFrames + incomingFrames) * channels;
if (requiredSamples > this.pendingBuffer.length) {
let newSize = this.pendingBuffer.length;
while (newSize < requiredSamples) {
newSize *= 2;
}
const newBuffer = new Float32Array(newSize);
newBuffer.set(this.pendingBuffer.subarray(0, this.pendingFrames * channels));
this.pendingBuffer = newBuffer;
}
this.pendingBuffer.set(incomingData, this.pendingFrames * channels);
this.pendingFrames += incomingFrames;
while (this.pendingFrames >= this.encoderFrameSize) {
await this.encodeOneFrame();
}
}
async flush() {
// Pad remaining samples with silence to fill a full frame
if (this.pendingFrames > 0) {
const channels = this.numberOfChannels;
const frameSize = this.encoderFrameSize;
const usedSamples = this.pendingFrames * channels;
const frameSamples = frameSize * channels;
this.pendingBuffer.fill(0, usedSamples, frameSamples);
this.pendingFrames = frameSize;
await this.encodeOneFrame();
}
await sendCommand({ type: 'flush-encoder', data: { ctx: this.ctx } });
this.resetInternalState();
}
close() {
void sendCommand({ type: 'close-encoder', data: { ctx: this.ctx } });
void unrefWorker();
}
private async encodeOneFrame() {
assert(this.nextSampleTimestampInSamples !== null);
assert(this.nextPacketTimestampInSamples !== null);
const channels = this.numberOfChannels;
const frameSize = this.encoderFrameSize;
const frameSamples = frameSize * channels;
const frameData = this.pendingBuffer.slice(0, frameSamples);
// Shift remaining using copyWithin
this.pendingFrames -= frameSize;
if (this.pendingFrames > 0) {
this.pendingBuffer.copyWithin(0, frameSamples, frameSamples + this.pendingFrames * channels);
}
const audioData = frameData.buffer;
const result = await sendCommand({
type: 'encode',
data: {
ctx: this.ctx,
audioData,
timestamp: this.nextSampleTimestampInSamples,
},
}, [audioData]);
this.nextSampleTimestampInSamples += frameSize;
// We always get exactly one packet because we encode the correct frame size
const packet = new EncodedPacket(
new Uint8Array(result.encodedData),
'key',
this.nextPacketTimestampInSamples / this.sampleRate,
result.duration / this.sampleRate,
);
this.nextPacketTimestampInSamples += result.duration;
this.onPacket(
packet,
this.chunkMetadata,
);
this.chunkMetadata = {};
}
}
/**
* Registers AC-3 and E-AC-3 encoders, which Mediabunny will then use automatically when applicable. Make sure to call
* this function before starting any encoding task.
*
* @group \@mediabunny/ac3
* @public
*/
export const registerAc3Encoder = () => {
registerEncoder(Ac3Encoder);
};
+21
View File
@@ -0,0 +1,21 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
const AC3_LOADED_SYMBOL = Symbol.for('@mediabunny/ac3 loaded');
if ((globalThis as Record<symbol, unknown>)[AC3_LOADED_SYMBOL]) {
console.error(
'[WARNING]\n@mediabunny/ac3 was loaded twice.'
+ ' This will likely cause the encoder/decoder not to work correctly.'
+ ' Check if multiple dependencies are importing different versions of @mediabunny/ac3,'
+ ' or if something is being bundled incorrectly.',
);
}
(globalThis as Record<symbol, unknown>)[AC3_LOADED_SYMBOL] = true;
export { registerAc3Decoder } from './decoder';
export { registerAc3Encoder } from './encoder';
+103
View File
@@ -0,0 +1,103 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
export type WorkerCommand = {
type: 'init-decoder';
data: {
codec: string;
};
} | {
type: 'decode';
data: {
ctx: number;
encodedData: ArrayBuffer;
timestamp: number;
};
} | {
type: 'flush-decoder';
data: {
ctx: number;
};
} | {
type: 'close-decoder';
data: {
ctx: number;
};
} | {
type: 'init-encoder';
data: {
codec: string;
numberOfChannels: number;
sampleRate: number;
bitrate: number;
};
} | {
type: 'encode';
data: {
ctx: number;
audioData: ArrayBuffer;
timestamp: number;
};
} | {
type: 'flush-encoder';
data: {
ctx: number;
};
} | {
type: 'close-encoder';
data: {
ctx: number;
};
};
export type WorkerResponseData = {
type: 'init-decoder';
ctx: number;
frameSize: number;
} | {
type: 'decode';
pcmData: ArrayBuffer;
format: AudioSampleFormat;
channels: number;
sampleRate: number;
sampleCount: number;
pts: number;
} | {
type: 'flush-decoder';
} | {
type: 'close-decoder';
} | {
type: 'init-encoder';
ctx: number;
frameSize: number;
} | {
type: 'encode';
encodedData: ArrayBuffer;
pts: number;
duration: number;
} | {
type: 'flush-encoder';
} | {
type: 'close-encoder';
};
export type WorkerResponse = {
id: number;
} & ({
success: true;
data: WorkerResponseData;
} | {
success: false;
error: unknown;
});
export function assert(x: unknown): asserts x {
if (!x) {
throw new Error('Assertion failed.');
}
}
+108
View File
@@ -0,0 +1,108 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { assert, type WorkerCommand, type WorkerResponse, type WorkerResponseData } from './shared';
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
import createWorker from './codec.worker';
type ExtendedWorker = Worker & {
ref?: () => void;
unref?: () => void;
};
let workerPromise: Promise<ExtendedWorker> | null;
let nextMessageId = 0;
const pendingMessages = new Map<number, {
resolve: (value: WorkerResponseData) => void;
reject: (reason?: unknown) => void;
}>();
let refCount = 0;
let keepAliveInterval: ReturnType<typeof setInterval> | null = null;
export const refWorker = async () => {
refCount++;
if (refCount === 1) {
keepAliveInterval = setInterval(() => {}, 2 ** 31 - 1);
const worker = await ensureWorker();
worker.ref?.();
}
};
export const unrefWorker = async () => {
refCount--;
if (refCount === 0) {
if (keepAliveInterval !== null) {
clearInterval(keepAliveInterval);
keepAliveInterval = null;
}
const worker = await workerPromise;
if (worker) {
if (worker.unref) {
worker.unref(); // If we don't do this, then the Node process never terminates by itself
} else if (typeof window === 'undefined') {
// Non-browser environment without unref - terminate instead
worker.terminate();
workerPromise = null;
}
}
}
};
export const sendCommand = async <T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
) => {
const worker = await ensureWorker();
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
const id = nextMessageId++;
pendingMessages.set(id, {
resolve: resolve as (value: WorkerResponseData) => void,
reject,
});
if (transferables) {
worker.postMessage({ id, command }, transferables);
} else {
worker.postMessage({ id, command });
}
});
};
const ensureWorker = () => {
return workerPromise ??= (async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const worker = (await createWorker()) as ExtendedWorker;
worker.unref?.(); // Start unreffed
const onMessage = (data: WorkerResponse) => {
const pending = pendingMessages.get(data.id);
assert(pending !== undefined);
pendingMessages.delete(data.id);
if (data.success) {
pending.resolve(data.data);
} else {
pending.reject(data.error);
}
};
if (worker.addEventListener) {
worker.addEventListener('message', event => onMessage(event.data as WorkerResponse));
} else {
const nodeWorker = worker as unknown as {
on: (event: string, listener: (data: never) => void) => void;
};
nodeWorker.on('message', onMessage);
}
return worker;
})();
};
+24
View File
@@ -0,0 +1,24 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "./dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
"noEmit": false,
"moduleResolution": "nodenext",
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../src/index.ts"],
},
},
"include": [
"./src/**/*",
"./build/**/*",
],
"references": [
{ "path": "../../src" }
]
}
+4
View File
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.json"]
}
+1
View File
@@ -0,0 +1 @@
build/* linguist-generated
+373
View File
@@ -0,0 +1,373 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.
+140
View File
@@ -0,0 +1,140 @@
# @mediabunny/flac-encoder
[![](https://img.shields.io/npm/v/@mediabunny/flac-encoder)](https://www.npmjs.com/package/@mediabunny/flac-encoder)
[![](https://img.shields.io/bundlephobia/minzip/@mediabunny/flac-encoder)](https://bundlephobia.com/package/@mediabunny/flac-encoder)
[![](https://img.shields.io/npm/dm/@mediabunny/flac-encoder)](https://www.npmjs.com/package/@mediabunny/flac-encoder)
[![](https://img.shields.io/discord/1390044844285497344?logo=discord&label=Discord)](https://discord.gg/hmpkyYuS4U)
<div align="center">
<img src="../../docs/public/mediabunny-logo.svg" width="180" height="180">
</div>
No browser currently supports FLAC encoding in their WebCodecs implementations. This extension package provides a reliable FLAC encoder for use with [Mediabunny](https://github.com/Vanilagy/mediabunny). It is implemented using Mediabunny's [custom coder API](https://mediabunny.dev/guide/supported-formats-and-codecs#custom-coders) and uses a fast, size-optimized WASM build of [libFLAC](https://github.com/xiph/flac) under the hood.
> This package, like the rest of Mediabunny, is enabled by its [sponsors](https://mediabunny.dev/#sponsors) and their donations. If you've derived value from this package, please consider [leaving a donation](https://github.com/sponsors/Vanilagy)! 💘
## Installation
This library peer-depends on Mediabunny. Install both using npm:
```bash
npm install mediabunny @mediabunny/flac-encoder
```
Alternatively, directly include them using a script tag:
```html
<script src="mediabunny.js"></script>
<script src="mediabunny-flac-encoder.js"></script>
```
This will expose the global objects `Mediabunny` and `MediabunnyFlacEncoder`. Use `mediabunny-flac-encoder.d.ts` to provide types for these globals. You can download the built distribution files from the [releases page](https://github.com/Vanilagy/mediabunny/releases).
## Usage
```ts
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
registerFlacEncoder();
```
That's it - Mediabunny now uses the registered FLAC encoder automatically.
If you want to be more correct, check for native browser support first:
```ts
import { canEncodeAudio } from 'mediabunny';
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
if (!(await canEncodeAudio('flac'))) {
registerFlacEncoder();
}
```
## Example
Here, we convert an input file to a FLAC file:
```ts
import {
Input,
ALL_FORMATS,
BlobSource,
Output,
BufferTarget,
FlacOutputFormat,
canEncodeAudio,
Conversion,
} from 'mediabunny';
import { registerFlacEncoder } from '@mediabunny/flac-encoder';
if (!(await canEncodeAudio('flac'))) {
registerFlacEncoder();
}
const input = new Input({
source: new BlobSource(file), // From a file picker, for example
formats: ALL_FORMATS,
});
const output = new Output({
format: new FlacOutputFormat(),
target: new BufferTarget(),
});
const conversion = await Conversion.init({
input,
output,
});
await conversion.execute();
output.target.buffer; // => ArrayBuffer containing the FLAC file
```
For more ways of using Mediabunny, refer to its [guide](https://mediabunny.dev/guide/introduction).
## Building and development
For simplicity, all built WASM artifacts are included in the repo, since these rarely change. However, here are the instructions for building them from scratch:
[Install Emscripten](https://emscripten.org/docs/getting_started/downloads.html), install CMake, and clone [libFLAC](https://github.com/xiph/flac). Then, from the Mediabunny root and with Emscripten sourced in:
```bash
export FLAC_PATH=/path/to/flac
export MEDIABUNNY_ROOT=$PWD
cd $FLAC_PATH
mkdir -p build && cd build
emcmake cmake .. \
-DBUILD_PROGRAMS=OFF \
-DBUILD_CXXLIBS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTING=OFF \
-DWITH_OGG=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DENABLE_MULTITHREADING=OFF \
-DINSTALL_MANPAGES=OFF \
-DCMAKE_C_FLAGS="-DNDEBUG -Oz -flto -msimd128"
emmake make
# Compile the bridge between JavaScript and libFLAC's API
cd $MEDIABUNNY_ROOT/packages/flac-encoder
emcc src/bridge.c \
$FLAC_PATH/build/src/libFLAC/libFLAC.a \
-I$FLAC_PATH/include \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ENVIRONMENT=web,worker \
-s FILESYSTEM=0 \
-s MALLOC=emmalloc \
-s SUPPORT_LONGJMP=0 \
-s EXPORTED_RUNTIME_METHODS=cwrap,HEAPU8 \
-s EXPORTED_FUNCTIONS=_malloc,_free \
-msimd128 \
-flto \
-Oz \
-o build/flac.js
```
This generates `build/flac.js`, which contains both the JavaScript "glue code" as well as the compiled WASM inlined.
### Building the package
Then, the complete JavaScript package can be built alongside the rest of Mediabunny by running `npm run build` in Mediabunny's root.
+37
View File
@@ -0,0 +1,37 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "dist/modules/src/index.d.ts",
"bundledPackages": [],
"compiler": {},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "dist/mediabunny-flac-encoder.d.ts"
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
},
"newlineKind": "lf"
}
Binary file not shown.
+54
View File
@@ -0,0 +1,54 @@
{
"name": "@mediabunny/flac-encoder",
"author": "Vanilagy",
"version": "1.38.0",
"description": "FLAC encoder extension for Mediabunny, based on libFLAC.",
"main": "./dist/bundles/mediabunny-flac-encoder.mjs",
"module": "./dist/bundles/mediabunny-flac-encoder.mjs",
"types": "./dist/modules/src/index.d.ts",
"exports": {
"types": "./dist/modules/src/index.d.ts",
"import": "./dist/bundles/mediabunny-flac-encoder.mjs",
"require": "./dist/bundles/mediabunny-flac-encoder.mjs"
},
"files": [
"README.md",
"package.json",
"LICENSE",
"dist",
"src"
],
"browser": {
"worker_threads": false
},
"sideEffects": false,
"license": "MPL-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/Vanilagy/mediabunny.git",
"directory": "packages/flac-encoder"
},
"bugs": {
"url": "https://github.com/Vanilagy/mediabunny/issues"
},
"homepage": "https://mediabunny.dev/guide/extensions/flac-encoder",
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/Vanilagy"
},
"peerDependencies": {
"mediabunny": "^1.0.0"
},
"devDependencies": {
"@types/emscripten": "^1.40.1"
},
"keywords": [
"flac",
"encoding",
"codec",
"mediabunny",
"lossless",
"browser",
"wasm"
]
}
+255
View File
@@ -0,0 +1,255 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <emscripten.h>
#include <FLAC/stream_encoder.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define BITS_PER_SAMPLE 16
#define COMPRESSION_LEVEL 5
typedef struct {
int size;
int samples;
} FrameInfo;
typedef struct {
FLAC__StreamEncoder *encoder;
// Input buffer for interleaved int16 samples from JS
int16_t *input_buffer;
int input_buffer_size;
// Widened to int32 for libFLAC
FLAC__int32 *int32_buffer;
int int32_buffer_size;
// Contiguous output buffer for encoded frame data
uint8_t *output_buffer;
int output_size;
int output_capacity;
// Per-frame metadata so JS can split the output buffer into individual packets
FrameInfo *frames;
int frame_count;
int frames_capacity;
// Stream header captured during init (fLaC + metadata blocks)
uint8_t *header_buffer;
int header_size;
int header_capacity;
bool header_done;
int channels;
} EncoderContext;
static void ensure_output_capacity(EncoderContext *ctx, int needed) {
if (needed <= ctx->output_capacity) {
return;
}
int new_capacity = ctx->output_capacity;
if (new_capacity < 4096) {
new_capacity = 4096;
}
while (new_capacity < needed) {
new_capacity *= 2;
}
ctx->output_buffer = realloc(ctx->output_buffer, new_capacity);
ctx->output_capacity = new_capacity;
}
static FLAC__StreamEncoderWriteStatus write_callback(
const FLAC__StreamEncoder *encoder,
const FLAC__byte buffer[],
size_t bytes,
uint32_t samples,
uint32_t current_frame,
void *client_data
) {
EncoderContext *ctx = (EncoderContext *)client_data;
// samples == 0 means this is metadata (stream header)
if (samples == 0) {
if (!ctx->header_done) {
int needed = ctx->header_size + bytes;
if (needed > ctx->header_capacity) {
int new_cap = ctx->header_capacity < 256 ? 256 : ctx->header_capacity;
while (new_cap < needed) { new_cap *= 2; }
ctx->header_buffer = realloc(ctx->header_buffer, new_cap);
ctx->header_capacity = new_cap;
}
memcpy(ctx->header_buffer + ctx->header_size, buffer, bytes);
ctx->header_size += bytes;
}
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
ctx->header_done = true;
// Append encoded data
ensure_output_capacity(ctx, ctx->output_size + bytes);
memcpy(ctx->output_buffer + ctx->output_size, buffer, bytes);
ctx->output_size += bytes;
// Record frame metadata
if (ctx->frame_count >= ctx->frames_capacity) {
int new_cap = ctx->frames_capacity < 16 ? 16 : ctx->frames_capacity * 2;
ctx->frames = realloc(ctx->frames, new_cap * sizeof(FrameInfo));
ctx->frames_capacity = new_cap;
}
ctx->frames[ctx->frame_count].size = bytes;
ctx->frames[ctx->frame_count].samples = samples;
ctx->frame_count++;
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}
static void reset_output(EncoderContext *ctx) {
ctx->output_size = 0;
ctx->frame_count = 0;
}
EMSCRIPTEN_KEEPALIVE
int init_encoder(int channels, int sample_rate) {
EncoderContext *ctx = calloc(1, sizeof(EncoderContext));
if (!ctx) {
return 0;
}
ctx->channels = channels;
ctx->encoder = FLAC__stream_encoder_new();
if (!ctx->encoder) {
free(ctx);
return 0;
}
FLAC__stream_encoder_set_channels(ctx->encoder, channels);
FLAC__stream_encoder_set_sample_rate(ctx->encoder, sample_rate);
FLAC__stream_encoder_set_bits_per_sample(ctx->encoder, BITS_PER_SAMPLE);
FLAC__stream_encoder_set_compression_level(ctx->encoder, COMPRESSION_LEVEL);
FLAC__stream_encoder_set_verify(ctx->encoder, false);
FLAC__StreamEncoderInitStatus status = FLAC__stream_encoder_init_stream(
ctx->encoder,
write_callback,
NULL, // seek callback
NULL, // tell callback
NULL, // metadata callback
ctx
);
if (status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
FLAC__stream_encoder_delete(ctx->encoder);
free(ctx);
return 0;
}
return (int)ctx;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_encode_input_ptr(int ctx_ptr, int size) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
if (size > ctx->input_buffer_size) {
ctx->input_buffer = realloc(ctx->input_buffer, size);
ctx->input_buffer_size = size;
}
return (uint8_t *)ctx->input_buffer;
}
EMSCRIPTEN_KEEPALIVE
int send_samples(int ctx_ptr, int num_samples) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
// Widen int16 to int32 for libFLAC
int total = num_samples * ctx->channels;
if (total > ctx->int32_buffer_size) {
ctx->int32_buffer = realloc(ctx->int32_buffer, total * sizeof(FLAC__int32));
ctx->int32_buffer_size = total;
}
for (int i = 0; i < total; i++) {
ctx->int32_buffer[i] = ctx->input_buffer[i];
}
reset_output(ctx);
FLAC__bool ok = FLAC__stream_encoder_process_interleaved(ctx->encoder, ctx->int32_buffer, num_samples);
return ok ? 0 : -1;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_output_data(int ctx_ptr) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->output_buffer;
}
EMSCRIPTEN_KEEPALIVE
int get_frame_count(int ctx_ptr) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->frame_count;
}
EMSCRIPTEN_KEEPALIVE
int get_frame_size(int ctx_ptr, int index) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->frames[index].size;
}
EMSCRIPTEN_KEEPALIVE
int get_frame_samples(int ctx_ptr, int index) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->frames[index].samples;
}
EMSCRIPTEN_KEEPALIVE
uint8_t *get_header_data(int ctx_ptr) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->header_buffer;
}
EMSCRIPTEN_KEEPALIVE
int get_header_size(int ctx_ptr) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
return ctx->header_size;
}
EMSCRIPTEN_KEEPALIVE
int finish_encoder(int ctx_ptr) {
EncoderContext *ctx = (EncoderContext *)ctx_ptr;
reset_output(ctx);
FLAC__bool ok = FLAC__stream_encoder_finish(ctx->encoder);
if (!ok) {
return -1;
}
// finish() leaves the encoder uninitialized but retains configuration (channels, sample rate,
// etc.), so we just re-init the stream to be ready for the next batch of samples.
ctx->header_size = 0;
ctx->header_done = false;
FLAC__StreamEncoderInitStatus status = FLAC__stream_encoder_init_stream(
ctx->encoder,
write_callback,
NULL,
NULL,
NULL,
ctx
);
return status == FLAC__STREAM_ENCODER_INIT_STATUS_OK ? 0 : -1;
}
+193
View File
@@ -0,0 +1,193 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import createModule from '../build/flac';
import type { PacketInfo, WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
type ExtendedEmscriptenModule = EmscriptenModule & {
cwrap: typeof cwrap;
};
let module: ExtendedEmscriptenModule;
let modulePromise: Promise<ExtendedEmscriptenModule> | null = null;
let initEncoderFn: (channels: number, sampleRate: number) => number;
let getEncodeInputPtr: (ctx: number, size: number) => number;
let sendSamplesFn: (ctx: number, numSamples: number) => number;
let getOutputData: (ctx: number) => number;
let getFrameCount: (ctx: number) => number;
let getFrameSize: (ctx: number, index: number) => number;
let getFrameSamples: (ctx: number, index: number) => number;
let getHeaderData: (ctx: number) => number;
let getHeaderSize: (ctx: number) => number;
let finishEncoderFn: (ctx: number) => number;
const ensureModule = async () => {
if (!module) {
if (modulePromise) {
return modulePromise;
}
modulePromise = createModule() as Promise<ExtendedEmscriptenModule>;
module = await modulePromise;
modulePromise = null;
initEncoderFn = module.cwrap('init_encoder', 'number', ['number', 'number']);
getEncodeInputPtr = module.cwrap('get_encode_input_ptr', 'number', ['number', 'number']);
sendSamplesFn = module.cwrap('send_samples', 'number', ['number', 'number']);
getOutputData = module.cwrap('get_output_data', 'number', ['number']);
getFrameCount = module.cwrap('get_frame_count', 'number', ['number']);
getFrameSize = module.cwrap('get_frame_size', 'number', ['number', 'number']);
getFrameSamples = module.cwrap('get_frame_samples', 'number', ['number', 'number']);
getHeaderData = module.cwrap('get_header_data', 'number', ['number']);
getHeaderSize = module.cwrap('get_header_size', 'number', ['number']);
finishEncoderFn = module.cwrap('finish_encoder', 'number', ['number']);
}
};
const initEncoder = async (numberOfChannels: number, sampleRate: number) => {
await ensureModule();
const ctx = initEncoderFn(numberOfChannels, sampleRate);
if (ctx === 0) {
throw new Error('Failed to initialize FLAC encoder.');
}
const headerPtr = getHeaderData(ctx);
const headerSize = getHeaderSize(ctx);
const header = module.HEAPU8.slice(headerPtr, headerPtr + headerSize).buffer;
return { ctx, header };
};
const readPackets = (ctx: number) => {
const packets: PacketInfo[] = [];
const frameCount = getFrameCount(ctx);
const outputPtr = getOutputData(ctx);
let offset = 0;
for (let i = 0; i < frameCount; i++) {
const size = getFrameSize(ctx, i);
const samples = getFrameSamples(ctx, i);
const encodedData = module.HEAPU8.slice(outputPtr + offset, outputPtr + offset + size).buffer;
packets.push({ encodedData, samples });
offset += size;
}
return packets;
};
const encode = (ctx: number, audioData: ArrayBuffer, numSamples: number) => {
const audioBytes = new Uint8Array(audioData);
const inputPtr = getEncodeInputPtr(ctx, audioBytes.length);
if (inputPtr === 0) {
throw new Error('Failed to allocate encoder input buffer.');
}
module.HEAPU8.set(audioBytes, inputPtr);
const ret = sendSamplesFn(ctx, numSamples);
if (ret < 0) {
throw new Error(`Encode failed with error code ${ret}.`);
}
return readPackets(ctx);
};
const flush = (ctx: number) => {
const ret = finishEncoderFn(ctx);
if (ret < 0) {
throw new Error('Flush failed.');
}
return readPackets(ctx);
};
const onMessage = (data: { id: number; command: WorkerCommand }) => {
const { id, command } = data;
const handleCommand = async (): Promise<void> => {
try {
let result: WorkerResponseData;
const transferables: Transferable[] = [];
switch (command.type) {
case 'init': {
const { ctx, header } = await initEncoder(
command.data.numberOfChannels,
command.data.sampleRate,
);
result = { type: command.type, ctx, header };
transferables.push(header);
}; break;
case 'encode': {
const packets = encode(
command.data.ctx,
command.data.audioData,
command.data.numSamples,
);
for (const p of packets) {
transferables.push(p.encodedData);
}
result = { type: command.type, packets };
}; break;
case 'flush': {
const packets = flush(command.data.ctx);
for (const p of packets) {
transferables.push(p.encodedData);
}
result = { type: command.type, packets };
}; break;
}
const response: WorkerResponse = {
id,
success: true,
data: result,
};
sendMessage(response, transferables);
} catch (error: unknown) {
const response: WorkerResponse = {
id,
success: false,
error,
};
sendMessage(response);
}
};
void handleCommand();
};
const sendMessage = (data: unknown, transferables?: Transferable[]) => {
if (parentPort) {
parentPort.postMessage(data, transferables ?? []);
} else {
self.postMessage(data, { transfer: transferables ?? [] });
}
};
let parentPort: {
postMessage: (data: unknown, transferables?: Transferable[]) => void;
on: (event: string, listener: (data: never) => void) => void;
} | null = null;
if (typeof self === 'undefined') {
const workerModule = 'worker_threads';
// eslint-disable-next-line @stylistic/max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-member-access
parentPort = require(workerModule).parentPort;
}
if (parentPort) {
parentPort.on('message', onMessage);
} else {
self.addEventListener('message', event => onMessage(event.data as { id: number; command: WorkerCommand }));
}
+200
View File
@@ -0,0 +1,200 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import {
CustomAudioEncoder,
AudioCodec,
AudioSample,
EncodedPacket,
registerEncoder,
} from 'mediabunny';
import type { PacketInfo, WorkerCommand, WorkerResponse, WorkerResponseData } from './shared';
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
import createWorker from './encode.worker';
const FLAC_SAMPLE_RATES = [
8000, 16000, 22050, 24000, 32000, 44100, 48000, 88200, 96000, 176400, 192000,
];
class FlacEncoder extends CustomAudioEncoder {
private worker: Worker | null = null;
private nextMessageId = 0;
private pendingMessages = new Map<number, {
resolve: (value: WorkerResponseData) => void;
reject: (reason?: unknown) => void;
}>();
private ctx = 0;
private chunkMetadata: EncodedAudioChunkMetadata = {};
private description: Uint8Array | null = null;
private nextTimestampInSamples: number | null = null;
static override supports(codec: AudioCodec, config: AudioEncoderConfig): boolean {
return codec === 'flac'
&& config.numberOfChannels >= 1
&& config.numberOfChannels <= 8
&& FLAC_SAMPLE_RATES.includes(config.sampleRate);
}
async init() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
this.worker = (await createWorker()) as Worker;
const onMessage = (data: WorkerResponse) => {
const pending = this.pendingMessages.get(data.id);
assert(pending !== undefined);
this.pendingMessages.delete(data.id);
if (data.success) {
pending.resolve(data.data);
} else {
pending.reject(data.error);
}
};
if (this.worker.addEventListener) {
this.worker.addEventListener('message', event => onMessage(event.data as WorkerResponse));
} else {
const nodeWorker = this.worker as unknown as {
on: (event: string, listener: (data: never) => void) => void;
};
nodeWorker.on('message', onMessage);
}
const result = await this.sendCommand({
type: 'init',
data: {
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
},
});
this.ctx = result.ctx;
this.description = new Uint8Array(result.header);
this.resetInternalState();
}
private resetInternalState() {
this.nextTimestampInSamples = null;
this.chunkMetadata = {
decoderConfig: {
codec: 'flac',
numberOfChannels: this.config.numberOfChannels,
sampleRate: this.config.sampleRate,
description: this.description!,
},
};
}
async encode(audioSample: AudioSample) {
if (this.nextTimestampInSamples === null) {
this.nextTimestampInSamples = Math.round(audioSample.timestamp * this.config.sampleRate);
}
const totalBytes = audioSample.allocationSize({ format: 's16', planeIndex: 0 });
const audioBytes = new Uint8Array(totalBytes);
audioSample.copyTo(audioBytes, { format: 's16', planeIndex: 0 });
const audioData = audioBytes.buffer;
const result = await this.sendCommand({
type: 'encode',
data: {
ctx: this.ctx,
audioData,
numSamples: audioSample.numberOfFrames,
},
}, [audioData]);
this.emitPackets(result.packets);
}
async flush() {
const result = await this.sendCommand({ type: 'flush', data: { ctx: this.ctx } });
this.emitPackets(result.packets);
this.resetInternalState();
}
close() {
this.worker?.terminate();
}
private emitPackets(packets: PacketInfo[]) {
assert(this.nextTimestampInSamples !== null);
for (const p of packets) {
const data = new Uint8Array(p.encodedData);
const packet = new EncodedPacket(
data,
'key',
this.nextTimestampInSamples / this.config.sampleRate,
p.samples / this.config.sampleRate,
);
this.nextTimestampInSamples += p.samples;
this.onPacket(
packet,
this.chunkMetadata,
);
this.chunkMetadata = {};
}
}
private sendCommand<T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
) {
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
const id = this.nextMessageId++;
this.pendingMessages.set(id, {
resolve: resolve as (value: WorkerResponseData) => void,
reject,
});
assert(this.worker);
if (transferables) {
this.worker.postMessage({ id, command }, transferables);
} else {
this.worker.postMessage({ id, command });
}
});
}
}
/**
* Registers the FLAC encoder, which Mediabunny will then use automatically when applicable. Make sure to call this
* function before starting any encoding task.
*
* Preferably, wrap the call in a condition to avoid overriding any native FLAC encoder:
*
* ```ts
* import { canEncodeAudio } from 'mediabunny';
* import { registerFlacEncoder } from '@mediabunny/flac-encoder';
*
* if (!(await canEncodeAudio('flac'))) {
* registerFlacEncoder();
* }
* ```
*
* @group \@mediabunny/flac-encoder
* @public
*/
export const registerFlacEncoder = () => {
registerEncoder(FlacEncoder);
};
function assert(x: unknown): asserts x {
if (!x) {
throw new Error('Assertion failed.');
}
}
+20
View File
@@ -0,0 +1,20 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
const FLAC_ENCODER_LOADED_SYMBOL = Symbol.for('@mediabunny/flac-encoder loaded');
if ((globalThis as Record<symbol, unknown>)[FLAC_ENCODER_LOADED_SYMBOL]) {
console.error(
'[WARNING]\n@mediabunny/flac-encoder was loaded twice.'
+ ' This will likely cause the encoder not to work correctly.'
+ ' Check if multiple dependencies are importing different versions of @mediabunny/flac-encoder,'
+ ' or if something is being bundled incorrectly.',
);
}
(globalThis as Record<symbol, unknown>)[FLAC_ENCODER_LOADED_SYMBOL] = true;
export { registerFlacEncoder } from './encoder';
+54
View File
@@ -0,0 +1,54 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
export type PacketInfo = {
encodedData: ArrayBuffer;
samples: number;
};
export type WorkerCommand = {
type: 'init';
data: {
numberOfChannels: number;
sampleRate: number;
};
} | {
type: 'encode';
data: {
ctx: number;
audioData: ArrayBuffer;
numSamples: number;
};
} | {
type: 'flush';
data: {
ctx: number;
};
};
export type WorkerResponseData = {
type: 'init';
ctx: number;
header: ArrayBuffer;
} | {
type: 'encode';
packets: PacketInfo[];
} | {
type: 'flush';
packets: PacketInfo[];
};
export type WorkerResponse = {
id: number;
} & ({
success: true;
data: WorkerResponseData;
} | {
success: false;
error: unknown;
});
+24
View File
@@ -0,0 +1,24 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "./dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
"noEmit": false,
"moduleResolution": "nodenext",
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../src/index.ts"],
},
},
"include": [
"./src/**/*",
"./build/**/*",
],
"references": [
{ "path": "../../src" }
]
}
+4
View File
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.json"]
}
+2 -3
View File
@@ -3,6 +3,7 @@
[![](https://img.shields.io/npm/v/@mediabunny/mp3-encoder)](https://www.npmjs.com/package/@mediabunny/mp3-encoder)
[![](https://img.shields.io/bundlephobia/minzip/@mediabunny/mp3-encoder)](https://bundlephobia.com/package/@mediabunny/mp3-encoder)
[![](https://img.shields.io/npm/dm/@mediabunny/mp3-encoder)](https://www.npmjs.com/package/@mediabunny/mp3-encoder)
[![](https://img.shields.io/discord/1390044844285497344?logo=discord&label=Discord)](https://discord.gg/hmpkyYuS4U)
<div align="center">
<img src="./logo.svg" width="180" height="180">
@@ -102,8 +103,6 @@ The WASM build itself is a performance-optimized, SIMD-enabled build of LAME 3.1
## Building and development
Building this library is done using the build commands in the [Mediabunny root](https://github.com/Vanilagy/mediabunny).
For simplicity, all built WASM artifacts are included in the repo, since these rarely change. However, here are the instructions for building them from scratch:
### Prerequisites
@@ -148,7 +147,7 @@ emcc src/lame-bridge.c build/libmp3lame.a \
-o build/lame.js
```
This generates `build/lame.js`, which contains both the JavaScript "glue code" as well as the compiled WASM encoded with Base64.
This generates `build/lame.js`, which contains both the JavaScript "glue code" as well as the compiled WASM inlined.
### Building the package
+4 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@mediabunny/mp3-encoder",
"author": "Vanilagy",
"version": "1.31.1",
"version": "1.38.0",
"description": "MP3 encoder extension for Mediabunny, based on LAME.",
"main": "./dist/bundles/mediabunny-mp3-encoder.mjs",
"module": "./dist/bundles/mediabunny-mp3-encoder.mjs",
@@ -18,6 +18,9 @@
"dist",
"src"
],
"browser": {
"worker_threads": false
},
"sideEffects": false,
"license": "MPL-2.0",
"repository": {
+12 -3
View File
@@ -146,7 +146,10 @@ const onMessage = (data: { id: number; command: WorkerCommand }) => {
command.data.sampleRate,
command.data.bitrate,
);
result = { success: true };
result = {
type: command.type,
success: true,
};
}; break;
case 'encode': {
@@ -154,13 +157,19 @@ const onMessage = (data: { id: number; command: WorkerCommand }) => {
command.data.audioData,
command.data.numberOfFrames,
);
result = { encodedData };
result = {
type: command.type,
encodedData,
};
transferables.push(encodedData);
}; break;
case 'flush': {
const flushedData = flush();
result = { flushedData };
result = {
type: command.type,
flushedData,
};
transferables.push(flushedData);
}; break;
}
+16 -9
View File
@@ -80,6 +80,12 @@ class Mp3Encoder extends CustomAudioEncoder {
},
});
this.resetInternalState();
}
private resetInternalState() {
this.currentBufferOffset = 0;
this.currentTimestamp = null;
this.chunkMetadata = {
decoderConfig: {
codec: 'mp3',
@@ -119,15 +125,15 @@ class Mp3Encoder extends CustomAudioEncoder {
},
}, [audioData]);
assert('encodedData' in result);
this.digestOutput(new Uint8Array(result.encodedData));
}
async flush() {
const result = await this.sendCommand({ type: 'flush' });
assert('flushedData' in result);
this.digestOutput(new Uint8Array(result.flushedData));
this.resetInternalState();
}
close() {
@@ -171,9 +177,7 @@ class Mp3Encoder extends CustomAudioEncoder {
const duration = header.audioSamplesInFrame / header.sampleRate;
this.onPacket(new EncodedPacket(data, 'key', this.currentTimestamp, duration), this.chunkMetadata);
if (this.currentTimestamp === 0) {
this.chunkMetadata = {}; // Mimic WebCodecs-like behavior
}
this.chunkMetadata = {}; // Mimic WebCodecs-like behavior
this.currentTimestamp += duration;
pos += header.totalSize;
@@ -186,13 +190,16 @@ class Mp3Encoder extends CustomAudioEncoder {
}
}
private sendCommand(
command: WorkerCommand,
private sendCommand<T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
) {
return new Promise<WorkerResponseData>((resolve, reject) => {
return new Promise<WorkerResponseData & { type: T }>((resolve, reject) => {
const id = this.nextMessageId++;
this.pendingMessages.set(id, { resolve, reject });
this.pendingMessages.set(id, {
resolve: resolve as (value: WorkerResponseData) => void,
reject,
});
assert(this.worker);
+3
View File
@@ -24,10 +24,13 @@ export type WorkerCommand = {
};
export type WorkerResponseData = {
type: 'init';
success: boolean;
} | {
type: 'encode';
encodedData: ArrayBuffer;
} | {
type: 'flush';
flushedData: ArrayBuffer;
};
+24
View File
@@ -0,0 +1,24 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "./dist/modules",
"declaration": true,
"declarationMap": true,
"stripInternal": true,
"noEmit": false,
"moduleResolution": "nodenext",
"module": "nodenext",
"allowJs": true,
"paths": {
"mediabunny": ["../../src/index.ts"],
},
},
"include": [
"./src/**/*",
"./build/**/*",
],
"references": [
{ "path": "../../src" }
]
}
+4
View File
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.json"]
}
+18 -2
View File
@@ -6,13 +6,19 @@ set -e
# Clear the stuff from last build
rm -rf dist
rm -rf packages/mp3-encoder/dist
rm -rf packages/ac3/dist
rm -rf packages/aac-encoder/dist
rm -rf packages/flac-encoder/dist
# Ensure license headers on all source files
tsx scripts/ensure-license-headers.ts
# Type check & generate .js and .d.ts files
tsc -p src
tsc -p packages/mp3-encoder/src
tsc -p packages/mp3-encoder
tsc -p packages/ac3
tsc -p packages/aac-encoder
tsc -p packages/flac-encoder
# So that the resulting files use valid ESM imports with file extension. This only runs for the core Mediabunny as only
# it ships the individual files to npm (for tree shaking, because it's large)
@@ -24,14 +30,24 @@ tsx scripts/bundle.ts
# Declaration file rollup and checks
api-extractor run
api-extractor run -c packages/mp3-encoder/api-extractor.json
api-extractor run -c packages/ac3/api-extractor.json
api-extractor run -c packages/aac-encoder/api-extractor.json
api-extractor run -c packages/flac-encoder/api-extractor.json
# Checks that all symbols are documented
tsx scripts/check-docblocks.ts dist/mediabunny.d.ts
tsx scripts/check-docblocks.ts packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
tsx scripts/check-docblocks.ts packages/ac3/dist/mediabunny-ac3.d.ts
tsx scripts/check-docblocks.ts packages/aac-encoder/dist/mediabunny-aac-encoder.d.ts
tsx scripts/check-docblocks.ts packages/flac-encoder/dist/mediabunny-flac-encoder.d.ts
# Checks that API docs are generatable
npm run docs:generate -- --dry
# Appends stuff to the declaration files to register the global variables these libraries expose
echo 'export as namespace Mediabunny;' >> dist/mediabunny.d.ts
echo 'export as namespace MediabunnyMp3Encoder;' >> packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
echo 'export as namespace MediabunnyMp3Encoder;' >> packages/mp3-encoder/dist/mediabunny-mp3-encoder.d.ts
echo 'export as namespace MediabunnyAc3;' >> packages/ac3/dist/mediabunny-ac3.d.ts
echo 'export as namespace MediabunnyAacEncoder;' >> packages/aac-encoder/dist/mediabunny-aac-encoder.d.ts
echo 'export as namespace MediabunnyFlacEncoder;' >> packages/flac-encoder/dist/mediabunny-flac-encoder.d.ts
+96
View File
@@ -113,9 +113,105 @@ const mp3EncoderVariants = await createVariants(
},
);
const ac3Variants = await createVariants(
'packages/ac3/src/index.ts',
'MediabunnyAc3',
'packages/ac3/dist/bundles/mediabunny-ac3',
'js', // The bundles are purely for the browser, not for Node (due to the peer dependecy)
{
plugins: [
PluginExternalGlobal.externalGlobalPlugin({
mediabunny: 'Mediabunny',
}),
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
{
external: ['mediabunny'],
plugins: [
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
);
const aacEncoderVariants = await createVariants(
'packages/aac-encoder/src/index.ts',
'MediabunnyAacEncoder',
'packages/aac-encoder/dist/bundles/mediabunny-aac-encoder',
'js', // The bundles are purely for the browser, not for Node (due to the peer dependecy)
{
plugins: [
PluginExternalGlobal.externalGlobalPlugin({
mediabunny: 'Mediabunny',
}),
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
{
external: ['mediabunny'],
plugins: [
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
);
const flacEncoderVariants = await createVariants(
'packages/flac-encoder/src/index.ts',
'MediabunnyFlacEncoder',
'packages/flac-encoder/dist/bundles/mediabunny-flac-encoder',
'js', // The bundles are purely for the browser, not for Node (due to the peer dependecy)
{
plugins: [
PluginExternalGlobal.externalGlobalPlugin({
mediabunny: 'Mediabunny',
}),
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
{
external: ['mediabunny'],
plugins: [
inlineWorkerPlugin({
define: {
'import.meta.url': '""',
},
legalComments: 'none',
}),
],
},
);
const contexts = [
...mediabunnyVariants,
...mp3EncoderVariants,
...ac3Variants,
...aacEncoderVariants,
...flacEncoderVariants,
];
if (process.argv[2] === '--watch') {
+23
View File
@@ -0,0 +1,23 @@
#!/bin/bash
set -e
rm -rf dist/modules
tsc -p src --stripInternal false
rm -rf packages/mp3-encoder/dist/modules
tsc -p packages/mp3-encoder
rm -rf packages/ac3/dist/modules
tsc -p packages/ac3
rm -rf packages/aac-encoder/dist/modules
tsc -p packages/aac-encoder
rm -rf packages/flac-encoder/dist/modules
tsc -p packages/flac-encoder
tsc -p tsconfig.vitest.json --noEmit
tsc -p scripts --noEmit
tsc -p tsconfig.vite.json --noEmit
+1
View File
@@ -36,6 +36,7 @@ const checkDirectory = (dirPath: string) => {
checkDirectory(path.join(__dirname, '..', 'src'));
checkDirectory(path.join(__dirname, '..', 'packages', 'mp3-encoder', 'src'));
checkDirectory(path.join(__dirname, '..', 'packages', 'ac3', 'src'));
checkDirectory(path.join(__dirname, '..', 'shared'));
if (missingFiles.length > 0) {
+5 -3
View File
@@ -28,7 +28,7 @@ export default function Worker() {
const inlineWorkerFunctionCode = `
export default async function inlineWorker(scriptText) {
if (typeof Worker !== 'undefined' && typeof Bun === 'undefined') {
// Browser, Deno
// Browser, Deno (Deno can't do dynamic import of worker_threads)
const blob = new Blob([scriptText], { type: "text/javascript" });
const url = URL.createObjectURL(blob);
@@ -42,8 +42,7 @@ export default async function inlineWorker(scriptText) {
try {
Worker = (await import('worker_threads')).Worker;
} catch {
const workerModule = 'worker_threads';
Worker = require(workerModule).Worker;
Worker = require('worker_threads').Worker;
}
const worker = new Worker(scriptText, { eval: true });
@@ -56,6 +55,9 @@ export default async function inlineWorker(scriptText) {
build.onResolve({ filter: /^__inline-worker$/ }, ({ path }) => {
return { path, namespace: 'inline-worker' };
});
build.onResolve({ filter: /^worker_threads$/ }, ({ path }) => {
return { path, external: true }; // Keep it in the bundle
});
build.onLoad({ filter: /.*/, namespace: 'inline-worker' }, () => {
return { contents: inlineWorkerFunctionCode, loader: 'js' };
});
+146
View File
@@ -0,0 +1,146 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { Bitstream } from './bitstream';
export type AacAudioSpecificConfig = {
objectType: number;
frequencyIndex: number;
sampleRate: number | null;
channelConfiguration: number;
numberOfChannels: number | null;
};
export const aacFrequencyTable = [
96000, 88200, 64000, 48000, 44100, 32000,
24000, 22050, 16000, 12000, 11025, 8000, 7350,
];
export const aacChannelMap = [-1, 1, 2, 3, 4, 5, 6, 8];
export const parseAacAudioSpecificConfig = (bytes: Uint8Array | null): AacAudioSpecificConfig => {
if (!bytes || bytes.byteLength < 2) {
throw new TypeError('AAC description must be at least 2 bytes long.');
}
const bitstream = new Bitstream(bytes);
let objectType = bitstream.readBits(5);
if (objectType === 31) {
objectType = 32 + bitstream.readBits(6);
}
const frequencyIndex = bitstream.readBits(4);
let sampleRate: number | null = null;
if (frequencyIndex === 15) {
sampleRate = bitstream.readBits(24);
} else {
if (frequencyIndex < aacFrequencyTable.length) {
sampleRate = aacFrequencyTable[frequencyIndex]!;
}
}
const channelConfiguration = bitstream.readBits(4);
let numberOfChannels: number | null = null;
if (channelConfiguration >= 1 && channelConfiguration <= 7) {
numberOfChannels = aacChannelMap[channelConfiguration]!;
}
return {
objectType,
frequencyIndex,
sampleRate,
channelConfiguration,
numberOfChannels,
};
};
export const buildAacAudioSpecificConfig = (config: {
objectType: number;
sampleRate: number;
numberOfChannels: number;
}) => {
let frequencyIndex = aacFrequencyTable.indexOf(config.sampleRate);
let customSampleRate: number | null = null;
if (frequencyIndex === -1) {
frequencyIndex = 15;
customSampleRate = config.sampleRate;
}
const channelConfiguration = aacChannelMap.indexOf(config.numberOfChannels);
if (channelConfiguration === -1) {
throw new TypeError(`Unsupported number of channels: ${config.numberOfChannels}`);
}
let bitCount = 5 + 4 + 4;
if (config.objectType >= 32) {
bitCount += 6;
}
if (frequencyIndex === 15) {
bitCount += 24;
}
const byteCount = Math.ceil(bitCount / 8);
const bytes = new Uint8Array(byteCount);
const bitstream = new Bitstream(bytes);
if (config.objectType < 32) {
bitstream.writeBits(5, config.objectType);
} else {
bitstream.writeBits(5, 31);
bitstream.writeBits(6, config.objectType - 32);
}
bitstream.writeBits(4, frequencyIndex);
if (frequencyIndex === 15) {
bitstream.writeBits(24, customSampleRate!);
}
bitstream.writeBits(4, channelConfiguration);
return bytes;
};
export type AdtsHeaderTemplate = {
header: Uint8Array;
bitstream: Bitstream;
};
export const buildAdtsHeaderTemplate = (config: AacAudioSpecificConfig): AdtsHeaderTemplate => {
const header = new Uint8Array(7);
const bitstream = new Bitstream(header);
const { objectType, frequencyIndex, channelConfiguration } = config;
const profile = objectType - 1;
bitstream.writeBits(12, 0b1111_11111111); // Syncword
bitstream.writeBits(1, 0); // MPEG Version
bitstream.writeBits(2, 0); // Layer
bitstream.writeBits(1, 1); // Protection absence
bitstream.writeBits(2, profile); // Profile
bitstream.writeBits(4, frequencyIndex); // MPEG-4 Sampling Frequency Index
bitstream.writeBits(1, 0); // Private bit
bitstream.writeBits(3, channelConfiguration); // MPEG-4 Channel Configuration
bitstream.writeBits(1, 0); // Originality
bitstream.writeBits(1, 0); // Home
bitstream.writeBits(1, 0); // Copyright ID bit
bitstream.writeBits(1, 0); // Copyright ID start
bitstream.skipBits(13); // Frame length (to be filled per packet)
bitstream.writeBits(11, 0x7ff); // Buffer fullness
bitstream.writeBits(2, 0); // Number of AAC frames minus 1
// Omit CRC check
return { header, bitstream };
};
export const writeAdtsFrameLength = (bitstream: Bitstream, frameLength: number) => {
bitstream.pos = 30;
bitstream.writeBits(13, frameLength);
};
+13
View File
@@ -0,0 +1,13 @@
/*!
* Copyright (c) 2026-present, Vanilagy and contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/** Sample rates indexed by fscod (Table 4.1) */
export const AC3_SAMPLE_RATES = [48000, 44100, 32000];
/** E-AC-3 reduced sample rates for fscod2 per ATSC A/52:2018 */
export const EAC3_REDUCED_SAMPLE_RATES = [24000, 22050, 16000];

Some files were not shown because too many files have changed in this diff Show More