Add AppendOnlyStreamSource, adjust examples to use it

This commit is contained in:
Vanilagy
2026-04-21 14:58:33 +02:00
parent 2b3e5f45c6
commit 783cfa6fbe
17 changed files with 328 additions and 68 deletions
+3 -6
View File
@@ -308,17 +308,14 @@ await output.finalize();
```ts
import {
Output,
StreamTarget,
StreamTargetChunk,
AppendOnlyStreamTarget,
Mp4OutputFormat,
} from 'mediabunny';
const { writable, readable } = new TransformStream<StreamTargetChunk, Uint8Array>({
transform: (chunk, controller) => controller.enqueue(chunk.data),
});
const { writable, readable } = new TransformStream<Uint8Array, Uint8Array>();
const output = new Output({
target: new StreamTarget(writable),
target: new AppendOnlyStreamTarget(writable),
// We must use an append-only format here, such as fragmented MP4
format: new Mp4OutputFormat({ fastStart: 'fragmented' }),
});
+7 -15
View File
@@ -71,14 +71,9 @@ const writtenFiles = new Map<string, ArrayBuffer>();
const output = new Output({
target: new PathedTarget(
'master.m3u8',
({ path }) => {
const target = new BufferTarget();
target.on('finalized', () => {
writtenFiles.set(path, target.buffer!);
});
return target;
},
({ path }) => new BufferTarget({
onFinalized: buffer => writtenFiles.set(path, buffer),
}),
),
// ...
});
@@ -127,12 +122,8 @@ const output = new Output({
'master.m3u8',
async ({ path, mimeType }) => {
const { writable, readable } = new TransformStream<
StreamTargetChunk,
Uint8Array
>({
transform: (chunk, controller) =>
controller.enqueue(chunk.data),
});
Uint8Array, Uint8Array,
>();
const url = `/upload?file=${encodeURIComponent(path)}`;
const promise = fetch(url, {
@@ -145,7 +136,8 @@ const output = new Output({
});
promises.push(promise);
return new StreamTarget(writable);
// Requires that all segments use an append-only format
return new AppendOnlyStreamTarget(writable);
},
),
onFinalize: () => Promise.all(promises),
+24
View File
@@ -388,6 +388,30 @@ const output = new Output({
await output.finalize(); // Will automatically close the writable stream
```
### `AppendOnlyStreamTarget`
Similar to a `StreamTarget` but for writing files in a purely append-only way:
```ts
import { Output, AppendOnlyStreamTarget } from 'mediabunny';
const writable = new WritableStream({
write(data: Uint8Array) {
// Do something with the data...
},
});
const output = new Output({
target: new AppendOnlyStreamTarget(writable),
// ...
});
```
Useful for consumers that can only read sequentially, like an HTTP server processing an incoming upload.
::: warning
The underlying data source doesn't magically become append-only just because you use this source. Instead, you can only use this source when the underlying format is *append-only*. See [Output formats](./output-formats) to see which formats are append-only.
:::
### `FilePathTarget`
This target writes to a file at the specified path. It is intended for server-side usage in Node, Bun, or Deno, and offers a simpler API than `StreamTarget` when you just want to write directly to a file path.