Skip to content

Commit

Permalink
Minor touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanilagy committed Jun 12, 2024
1 parent 08ad562 commit 84482cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,16 @@ set explicitly:
- Use `'offset'` to offset the timestamp of each track by that track's first chunk's timestamp. This way, it
starts at 0.
- Use `'cross-track-offset'` to offset the timestamp of each track by the _minimum of all tracks' first chunk timestamp_.
This works like `'offset'`, but should be used when the all tracks use the same timebase.
This works like `'offset'`, but should be used when the all tracks use the same clock.
### Muxing media chunks
Then, with VideoEncoder and AudioEncoder set up, send encoded chunks to the muxer using the following methods:
```ts
addVideoChunk(
chunk: EncodedVideoChunk,
meta?: EncodedVideoChunkMetadata,
timestamp?: number
timestamp?: number,
compositionTimeOffset?: number
): void;
addAudioChunk(
Expand All @@ -274,6 +275,11 @@ let videoEncoder = new VideoEncoder({
videoEncoder.configure(/* ... */);
```

The optional field `compositionTimeOffset` can be used when the decode time of the chunk doesn't equal its presentation
time; this is the case when [B-frames](https://en.wikipedia.org/wiki/Video_compression_picture_types) are present.
B-frames don't occur when using the WebCodecs API for encoding. The decode time is calculated by subtracting
`compositionTimeOffset` from `timestamp`, meaning `timestamp` dictates the presentation time.

Should you have obtained your encoded media data from a source other than the WebCodecs API, you can use these following
methods to directly send your raw data to the muxer:
```ts
Expand All @@ -283,7 +289,7 @@ addVideoChunkRaw(
timestamp: number, // in microseconds
duration: number, // in microseconds
meta?: EncodedVideoChunkMetadata,
compositionTimeOffset: number // in microseconds
compositionTimeOffset?: number // in microseconds
): void;

addAudioChunkRaw(
Expand Down
2 changes: 1 addition & 1 deletion build/mp4-muxer.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,17 @@ export const url = () => fullBox('url ', 0, 1); // Self-reference flag enabled
* also indicates how to interpret the sample (for example, whether to decompress the video data and, if so, how).
*/
export const stbl = (track: Track) => {
const needsCTTS = track.compositionTimeOffsetTable.length > 1 ||
const needsCtts = track.compositionTimeOffsetTable.length > 1 ||
track.compositionTimeOffsetTable.some((x) => x.sampleCompositionTimeOffset !== 0);

return box('stbl', null, [
stsd(track),
stts(track),
stss(track),
stsc(track),
stsz(track),
stco(track),
needsCTTS ? ctts(track) : null
needsCtts ? ctts(track) : null
]);
};

Expand Down
8 changes: 5 additions & 3 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ export const last = <T>(arr: T[]) => {

export const lastPresentedSample = (samples: Sample[]): Sample | undefined => {
let result: Sample | undefined = undefined;
for (const s of samples) {
if (!result || s.presentationTimestamp > result.presentationTimestamp) {
result = s;

for (let sample of samples) {
if (!result || sample.presentationTimestamp > result.presentationTimestamp) {
result = sample;
}
}

return result;
};

Expand Down

0 comments on commit 84482cb

Please sign in to comment.