Skip to content

🎥 Make videos programmatically with React

higher granularity

Jonny Burger204e8c9caffa
diff --git a/packages/media-parser/src/containers/aac/parse-aac.ts b/packages/media-parser/src/containers/aac/parse-aac.ts
index b92847da34..6ecb2ee3a5 100644
--- a/packages/media-parser/src/containers/aac/parse-aac.ts
+++ b/packages/media-parser/src/containers/aac/parse-aac.ts
@@ -91,20 +91,20 @@ export const parseAac = async (state: ParserState): Promise<ParseResult> => {
// One ADTS frame contains 1024 samples
await emitAudioSample({
trackId: 0,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
duration,
type: 'key',
data,
offset: startOffset,
- timescale: 1_000_000,
+ timescale: 1000000,
trackId: 0,
cts: timestamp,
dts: timestamp,
timestamp,
},
- 1,
- ),
+ timescale: 1,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/flac/parse-flac-frame.ts b/packages/media-parser/src/containers/flac/parse-flac-frame.ts
index 0c0f503c91..411f77d0f9 100644
--- a/packages/media-parser/src/containers/flac/parse-flac-frame.ts
+++ b/packages/media-parser/src/containers/flac/parse-flac-frame.ts
@@ -118,8 +118,8 @@ const emitSample = async ({
await emitAudioSample({
trackId: 0,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
data,
duration,
cts: timestamp,
@@ -127,11 +127,11 @@ const emitSample = async ({
timestamp,
type: 'key',
offset,
- timescale: 1_000_000,
+ timescale: 1000000,
trackId: 0,
},
- 1,
- ),
+ timescale: 1,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
index f27d26e670..beeab73dc5 100644
--- a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
+++ b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
@@ -90,8 +90,8 @@ export const parseMdatSection = async (
if (samplesWithIndex.track.type === 'audio') {
await emitAudioSample({
trackId: samplesWithIndex.track.trackId,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
data: bytes,
timestamp: cts,
duration,
@@ -102,8 +102,8 @@ export const parseMdatSection = async (
offset,
timescale: samplesWithIndex.track.timescale,
},
- samplesWithIndex.track.timescale,
- ),
+ timescale: samplesWithIndex.track.timescale,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
@@ -125,8 +125,8 @@ export const parseMdatSection = async (
await emitVideoSample({
trackId: samplesWithIndex.track.trackId,
- videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
data: bytes,
timestamp: cts,
duration,
@@ -137,8 +137,8 @@ export const parseMdatSection = async (
offset,
timescale: samplesWithIndex.track.timescale,
},
- samplesWithIndex.track.timescale,
- ),
+ timescale: samplesWithIndex.track.timescale,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/riff/parse-movi.ts b/packages/media-parser/src/containers/riff/parse-movi.ts
index c208e3fed2..d970262b7c 100644
--- a/packages/media-parser/src/containers/riff/parse-movi.ts
+++ b/packages/media-parser/src/containers/riff/parse-movi.ts
@@ -63,8 +63,8 @@ export const handleChunk = async ({
// If we'd pass a duration instead, it would shift the audio and we think that audio is not finished
await emitVideoSample({
trackId,
- videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
cts: timestamp,
dts: timestamp,
data,
@@ -75,8 +75,8 @@ export const handleChunk = async ({
offset,
timescale: samplesPerSecond,
},
- 1,
- ),
+ timescale: 1,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
@@ -104,8 +104,8 @@ export const handleChunk = async ({
// If we'd pass a duration instead, it would shift the audio and we think that audio is not finished
await emitAudioSample({
trackId,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
cts: timestamp,
dts: timestamp,
data,
@@ -116,8 +116,8 @@ export const handleChunk = async ({
offset,
timescale: samplesPerSecond,
},
- 1,
- ),
+ timescale: 1,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/transport-stream/handle-aac-packet.ts b/packages/media-parser/src/containers/transport-stream/handle-aac-packet.ts
index 7aa48b4f79..f2d6cb0ab6 100644
--- a/packages/media-parser/src/containers/transport-stream/handle-aac-packet.ts
+++ b/packages/media-parser/src/containers/transport-stream/handle-aac-packet.ts
@@ -72,10 +72,10 @@ export const handleAacPacket = async ({
await emitAudioSample({
trackId: programId,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
sample,
- MPEG_TIMESCALE,
- ),
+ timescale: MPEG_TIMESCALE,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/transport-stream/handle-avc-packet.ts b/packages/media-parser/src/containers/transport-stream/handle-avc-packet.ts
index 46e2796f8f..910d80a7c5 100644
--- a/packages/media-parser/src/containers/transport-stream/handle-avc-packet.ts
+++ b/packages/media-parser/src/containers/transport-stream/handle-avc-packet.ts
@@ -93,10 +93,10 @@ export const handleAvcPacket = async ({
await emitVideoSample({
trackId: programId,
- videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
+ videoSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
sample,
- MPEG_TIMESCALE,
- ),
+ timescale: MPEG_TIMESCALE,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/containers/wav/get-seeking-byte.ts b/packages/media-parser/src/containers/wav/get-seeking-byte.ts
index f1cc1ca09d..ac1c5585e1 100644
--- a/packages/media-parser/src/containers/wav/get-seeking-byte.ts
+++ b/packages/media-parser/src/containers/wav/get-seeking-byte.ts
@@ -1,7 +1,7 @@
import type {WavSeekingInfo} from '../../seeking-info';
import type {SeekResolution} from '../../work-on-seek-request';
-export const WAVE_SECONDS_PER_SAMPLE = 1;
+export const WAVE_SAMPLES_PER_SECOND = 25;
export const getSeekingByteFromWav = ({
info,
@@ -15,8 +15,8 @@ export const getSeekingByteFromWav = ({
const timeRoundedDown =
Math.floor(
- Math.min(time, durationInSeconds - 0.0000001) / WAVE_SECONDS_PER_SAMPLE,
- ) * WAVE_SECONDS_PER_SAMPLE;
+ Math.min(time, durationInSeconds - 0.0000001) * WAVE_SAMPLES_PER_SECOND,
+ ) / WAVE_SAMPLES_PER_SECOND;
const byteOffset = bytesPerSecond * timeRoundedDown;
diff --git a/packages/media-parser/src/containers/wav/parse-media-section.ts b/packages/media-parser/src/containers/wav/parse-media-section.ts
index bd6afaf928..aa61b39441 100644
--- a/packages/media-parser/src/containers/wav/parse-media-section.ts
+++ b/packages/media-parser/src/containers/wav/parse-media-section.ts
@@ -3,7 +3,7 @@ import {emitAudioSample} from '../../emit-audio-sample';
import type {ParseResult} from '../../parse-result';
import type {ParserState} from '../../state/parser-state';
import {getWorkOnSeekRequestOptions} from '../../work-on-seek-request';
-import {WAVE_SECONDS_PER_SAMPLE} from './get-seeking-byte';
+import {WAVE_SAMPLES_PER_SECOND} from './get-seeking-byte';
import type {WavFmt} from './types';
export const parseMediaSection = async ({
@@ -29,7 +29,7 @@ export const parseMediaSection = async ({
const toRead = Math.min(
maxRead,
- fmtBox.sampleRate * fmtBox.blockAlign * WAVE_SECONDS_PER_SAMPLE,
+ (fmtBox.sampleRate * fmtBox.blockAlign) / WAVE_SAMPLES_PER_SECOND,
);
const duration = toRead / (fmtBox.sampleRate * fmtBox.blockAlign);
@@ -39,8 +39,8 @@ export const parseMediaSection = async ({
const data = iterator.getSlice(toRead);
await emitAudioSample({
trackId: 0,
- audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps(
- {
+ audioSample: convertAudioOrVideoSampleToWebCodecsTimestamps({
+ sample: {
cts: timestamp,
dts: timestamp,
data,
@@ -51,8 +51,8 @@ export const parseMediaSection = async ({
offset,
timescale: 1_000_000,
},
- 1,
- ),
+ timescale: 1,
+ }),
workOnSeekRequestOptions: getWorkOnSeekRequestOptions(state),
callbacks: state.callbacks,
});
diff --git a/packages/media-parser/src/convert-audio-or-video-sample.ts b/packages/media-parser/src/convert-audio-or-video-sample.ts
index 6070e447dd..e3c27ab0a7 100644
--- a/packages/media-parser/src/convert-audio-or-video-sample.ts
+++ b/packages/media-parser/src/convert-audio-or-video-sample.ts
@@ -1,23 +1,44 @@
import type {AudioOrVideoSample} from './webcodec-sample-types';
-export const convertAudioOrVideoSampleToWebCodecsTimestamps = (
- sample: AudioOrVideoSample,
- timescale: number,
-): AudioOrVideoSample => {
+const TARGET_TIMESCALE = 1_000_000;
+
+const fixFloat = (value: number) => {
+ if (value % 1 < 0.0000001) {
+ return Math.floor(value);
+ }
+
+ if (value % 1 > 0.9999999) {
+ return Math.ceil(value);
+ }
+
+ return value;
+};
+
+export const convertAudioOrVideoSampleToWebCodecsTimestamps = ({
+ sample,
+ timescale,
+}: {
+ sample: AudioOrVideoSample;
+ timescale: number;
+}): AudioOrVideoSample => {
+ if (timescale === TARGET_TIMESCALE) {
+ return sample;
+ }
+
const {cts, dts, timestamp} = sample;
return {
- cts: (cts * 1_000_000) / timescale,
- dts: (dts * 1_000_000) / timescale,
- timestamp: (timestamp * 1_000_000) / timescale,
+ cts: fixFloat(cts * (TARGET_TIMESCALE / timescale)),
+ dts: fixFloat(dts * (TARGET_TIMESCALE / timescale)),
+ timestamp: fixFloat(timestamp * (TARGET_TIMESCALE / timescale)),
duration:
sample.duration === undefined
? undefined
- : (sample.duration * 1_000_000) / timescale,
+ : fixFloat(sample.duration * (TARGET_TIMESCALE / timescale)),
data: sample.data,
trackId: sample.trackId,
type: sample.type,
offset: sample.offset,
- timescale: 1_000_000,
+ timescale: TARGET_TIMESCALE,
};
};
diff --git a/packages/media-parser/src/test/iso-base-media.test.ts b/packages/media-parser/src/test/iso-base-media.test.ts
index 326af9dd3a..e8a6282148 100644
--- a/packages/media-parser/src/test/iso-base-media.test.ts
+++ b/packages/media-parser/src/test/iso-base-media.test.ts
@@ -97,7 +97,7 @@ if (process.platform !== 'win32') {
trackId: 1,
},
{
- decodingTimeInSeconds: 2.033333333333333,
+ decodingTimeInSeconds: 2.0333333333333337,
positionInBytes: 5928534,
presentationTimeInSeconds: 2.1333333333333333,
sizeInBytes: 204071,
@@ -111,16 +111,16 @@ if (process.platform !== 'win32') {
trackId: 1,
},
{
- decodingTimeInSeconds: 4.166666666666666,
+ decodingTimeInSeconds: 4.166666666666667,
positionInBytes: 13242642,
presentationTimeInSeconds: 4.266666666666667,
sizeInBytes: 213213,
trackId: 1,
},
{
- decodingTimeInSeconds: 5.233333333333333,
+ decodingTimeInSeconds: 5.233333333333334,
positionInBytes: 16387233,
- presentationTimeInSeconds: 5.333333333333333,
+ presentationTimeInSeconds: 5.333333333333334,
sizeInBytes: 250679,
trackId: 1,
},
@@ -146,9 +146,9 @@ if (process.platform !== 'win32') {
trackId: 1,
},
{
- decodingTimeInSeconds: 9.501666666666667,
+ decodingTimeInSeconds: 9.501666666666669,
positionInBytes: 29786012,
- presentationTimeInSeconds: 9.601666666666667,
+ presentationTimeInSeconds: 9.601666666666668,
sizeInBytes: 250183,
trackId: 1,
},
diff --git a/packages/media-parser/src/test/keyframes.test.ts b/packages/media-parser/src/test/keyframes.test.ts
index 7f16c64512..da8f943f89 100644
--- a/packages/media-parser/src/test/keyframes.test.ts
+++ b/packages/media-parser/src/test/keyframes.test.ts
@@ -106,7 +106,7 @@ test('should be able to get keyframes from .ts', async () => {
{
decodingTimeInSeconds: 10,
positionInBytes: 564,
- presentationTimeInSeconds: 10.033333333333333,
+ presentationTimeInSeconds: 10.033333333333331,
sizeInBytes: 23814,
trackId: 256,
},
diff --git a/packages/media-parser/src/test/mp4-av1.test.ts b/packages/media-parser/src/test/mp4-av1.test.ts
index 55ec46107e..a56019f10e 100644
--- a/packages/media-parser/src/test/mp4-av1.test.ts
+++ b/packages/media-parser/src/test/mp4-av1.test.ts
@@ -36,7 +36,7 @@ test('mp4-av1', async () => {
expect(samples).toBe(1185);
- expect(slowFps).toBe(60.000000000000014);
+ expect(slowFps).toBe(60);
expect(slowNumberOfFrames).toBe(1185);
expect(videoCodec).toBe('av1');
expect(audioCodec).toBe(null);
diff --git a/packages/media-parser/src/test/seeking/seek-moof.test.ts b/packages/media-parser/src/test/seeking/seek-moof.test.ts
index 79f3886b0e..a0cb5352f3 100644
--- a/packages/media-parser/src/test/seeking/seek-moof.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-moof.test.ts
@@ -33,7 +33,7 @@ test('seek moof, should make use of the mfra atom if available', async () => {
throw new Error('should not reach here');
}
- if (sample.cts === 19533333.333333332) {
+ if (sample.cts === 19533333.333333336) {
expect(sample.type).toBe('key');
controller._experimentalSeek({
@@ -57,6 +57,10 @@ test('seek moof, should make use of the mfra atom if available', async () => {
});
throw new Error('should not reach here');
} catch (err) {
+ if (!hasBeenAborted(err)) {
+ throw err;
+ }
+
expect(hasBeenAborted(err)).toBe(true);
expect(
controller._internals.performedSeeksSignal.getPerformedSeeks(),
diff --git a/packages/media-parser/src/test/seeking/seeking-wav.test.ts b/packages/media-parser/src/test/seeking/seeking-wav.test.ts
index 8bc1778a61..304e7c160c 100644
--- a/packages/media-parser/src/test/seeking/seeking-wav.test.ts
+++ b/packages/media-parser/src/test/seeking/seeking-wav.test.ts
@@ -51,7 +51,7 @@ test('seeking in a wav', async () => {
}
if (samples === 4) {
- expect(sample.timestamp).toBe(29000000);
+ expect(sample.timestamp).toBe(29960000);
}
};
},
@@ -67,15 +67,20 @@ test('seeking in a wav', async () => {
type: 'user-initiated',
},
{
- from: 970244,
+ from: 885572,
to: 44,
type: 'user-initiated',
},
{
- from: 88244,
+ from: 3572,
to: 2469644,
type: 'user-initiated',
},
+ {
+ from: 2473172,
+ to: 2642516,
+ type: 'user-initiated',
+ },
]);
expect(internalStats.finalCursorOffset).toBe(2646150);
expect(internalStats.skippedBytes).toBe(0);
diff --git a/packages/media-parser/src/test/transportstream.test.ts b/packages/media-parser/src/test/transportstream.test.ts
index 4fa2123588..c875e8f0b9 100644
--- a/packages/media-parser/src/test/transportstream.test.ts
+++ b/packages/media-parser/src/test/transportstream.test.ts
@@ -225,7 +225,7 @@ test('Transport stream', async () => {
{
decodingTimeInSeconds: 10,
positionInBytes: 564,
- presentationTimeInSeconds: 10.033333333333333,
+ presentationTimeInSeconds: 10.033333333333331,
sizeInBytes: 23814,
trackId: 256,
},
diff --git a/packages/media-parser/src/test/wav.test.ts b/packages/media-parser/src/test/wav.test.ts
index 120e2713e5..2d38a2427b 100644
--- a/packages/media-parser/src/test/wav.test.ts
+++ b/packages/media-parser/src/test/wav.test.ts
@@ -1,5 +1,6 @@
import {exampleVideos} from '@remotion/example-videos';
import {expect, test} from 'bun:test';
+import {WAVE_SAMPLES_PER_SECOND} from '../containers/wav/get-seeking-byte';
import {parseMedia} from '../parse-media';
import {nodeReader} from '../readers/from-node';
@@ -179,14 +180,16 @@ test('should get all samples', async () => {
},
onAudioTrack: () => {
return (sample) => {
- expect(sample.dts % 1_000_000).toBe(0);
- expect(sample.cts % 1_000_000).toBe(0);
- expect(sample.timestamp % 1_000_000).toBe(0);
+ expect(sample.dts % (1_000_000 / WAVE_SAMPLES_PER_SECOND)).toBe(0);
+ expect(sample.cts % (1_000_000 / WAVE_SAMPLES_PER_SECOND)).toBe(0);
+ expect(sample.timestamp % (1_000_000 / WAVE_SAMPLES_PER_SECOND)).toBe(
+ 0,
+ );
samples++;
};
},
});
- expect(samples).toBe(30);
+ expect(samples).toBe(750);
expect(internalStats).toEqual({
skippedBytes: 0,
finalCursorOffset: 2646150,