Skip to content

🎥 Make videos programmatically with React

aac seeking

Jonny Burger4fd25d8fffc4
diff --git a/packages/docs/docs/media-parser/seeking-hints.mdx b/packages/docs/docs/media-parser/seeking-hints.mdx
index a0737fce71..22443dd018 100644
--- a/packages/docs/docs/media-parser/seeking-hints.mdx
+++ b/packages/docs/docs/media-parser/seeking-hints.mdx
@@ -6,6 +6,10 @@ slug: /media-parser/seeking-hints
crumb: '@remotion/media-parser'
---
+:::warning
+This feature is experimental and not yet ready for production use.
+:::
+
When Media Parser wants to seek to a specific position in the media file, it can be expensive to find out where to go.
With Seeking Hints, you can provide a hint to the media parsing process upfront about the structure to the file, so that seeking can be short-circuited without needing to figure out the position.
diff --git a/packages/media-parser/src/containers/aac/parse-aac.ts b/packages/media-parser/src/containers/aac/parse-aac.ts
index 91c0833138..a3d0f1e335 100644
--- a/packages/media-parser/src/containers/aac/parse-aac.ts
+++ b/packages/media-parser/src/containers/aac/parse-aac.ts
@@ -59,6 +59,10 @@ export const parseAac = async (state: ParserState): Promise<ParseResult> => {
const data = iterator.getSlice(frameLength);
if (state.callbacks.tracks.getTracks().length === 0) {
+ state.mediaSection.addMediaSection({
+ start: startOffset,
+ size: state.contentLength - startOffset,
+ });
await registerAudioTrack({
container: 'aac',
track: {
diff --git a/packages/media-parser/src/containers/aac/seeking-hints.ts b/packages/media-parser/src/containers/aac/seeking-hints.ts
index 445ff9452d..a12a3e1ae2 100644
--- a/packages/media-parser/src/containers/aac/seeking-hints.ts
+++ b/packages/media-parser/src/containers/aac/seeking-hints.ts
@@ -1,6 +1,5 @@
import type {AacState} from '../../state/aac-state';
import type {AudioSampleOffset} from '../../state/audio-sample-map';
-import type {ParserState} from '../../state/parser-state';
import type {SamplesObservedState} from '../../state/samples-observed/slow-duration-fps';
export type AacSeekingHints = {
@@ -23,12 +22,5 @@ export const getSeekingHintsForAac = ({
};
};
-export const setSeekingHintsForAac = ({
- hints,
- state,
-}: {
- hints: AacSeekingHints;
- state: ParserState;
-}) => {
- state.aac.audioSamples.setFromSeekingHints(hints.audioSampleMap);
-};
+// TODO: Implement this and maintain index
+export const setSeekingHintsForAac = () => {};
diff --git a/packages/media-parser/src/set-seeking-hints.ts b/packages/media-parser/src/set-seeking-hints.ts
index 989a916bcf..af6fc3141e 100644
--- a/packages/media-parser/src/set-seeking-hints.ts
+++ b/packages/media-parser/src/set-seeking-hints.ts
@@ -52,7 +52,7 @@ export const setSeekingHints = ({
}
if (hints.type === 'aac-seeking-hints') {
- setSeekingHintsForAac({hints, state});
+ setSeekingHintsForAac();
return;
}
diff --git a/packages/media-parser/src/state/aac-state.ts b/packages/media-parser/src/state/aac-state.ts
index bad4547cc4..3cc92e8bf3 100644
--- a/packages/media-parser/src/state/aac-state.ts
+++ b/packages/media-parser/src/state/aac-state.ts
@@ -14,8 +14,9 @@ export const aacState = () => {
return {
addSample: ({offset, size}: {offset: number; size: number}) => {
- if (samples.find((s) => s.offset === offset)) {
- throw new Error('Duplicate sample');
+ const index = samples.findIndex((s) => s.offset === offset);
+ if (index !== -1) {
+ return samples[index];
}
samples.push({offset, index: samples.length, size});
diff --git a/packages/media-parser/src/test/seeking/seek-aac.test.ts b/packages/media-parser/src/test/seeking/seek-aac.test.ts
new file mode 100644
index 0000000000..816b686ed0
--- /dev/null
+++ b/packages/media-parser/src/test/seeking/seek-aac.test.ts
@@ -0,0 +1,90 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {expect, test} from 'bun:test';
+import {mediaParserController} from '../../controller/media-parser-controller';
+import {nodeReader} from '../../node';
+import {parseMedia} from '../../parse-media';
+
+test('seek aac', async () => {
+ const controller = mediaParserController();
+ controller._experimentalSeek({
+ timeInSeconds: 3,
+ type: 'keyframe-before-time',
+ });
+
+ await parseMedia({
+ src: exampleVideos.aac,
+ reader: nodeReader,
+ controller,
+ acknowledgeRemotionLicense: true,
+ onAudioTrack: () => {
+ let samples = 0;
+ return (s) => {
+ samples++;
+ if (samples === 1) {
+ expect(s.timestamp / s.timescale).toBe(2.995374149659864);
+ controller._experimentalSeek({
+ timeInSeconds: 10,
+ type: 'keyframe-before-time',
+ });
+ }
+
+ if (samples === 2) {
+ expect(s.timestamp / s.timescale).toBe(9.984580498866213);
+ controller._experimentalSeek({
+ timeInSeconds: 5,
+ type: 'keyframe-before-time',
+ });
+ }
+
+ if (samples === 3) {
+ expect(s.timestamp / s.timescale).toBe(4.992290249433107);
+ controller._experimentalSeek({
+ timeInSeconds: 1000,
+ type: 'keyframe-before-time',
+ });
+ }
+
+ if (samples === 4) {
+ expect(s.timestamp / s.timescale).toBe(105.79011337868481);
+ }
+
+ if (samples === 5) {
+ throw new Error('this should have been the last sample');
+ }
+ };
+ },
+ });
+
+ const hints = await controller.getSeekingHints();
+ if (!hints || hints.type !== 'aac-seeking-hints') {
+ throw new Error('No hints');
+ }
+
+ expect(hints.audioSampleMap.length).toBe(4557);
+
+ // TODO: Get seeking hints to work
+ const performedSeeks =
+ controller._internals.performedSeeksSignal.getPerformedSeeks();
+ expect(performedSeeks).toEqual([
+ {
+ from: 53471,
+ to: 53071,
+ type: 'user-initiated',
+ },
+ {
+ from: 171047,
+ to: 170658,
+ type: 'user-initiated',
+ },
+ {
+ from: 171047,
+ to: 87851,
+ type: 'user-initiated',
+ },
+ {
+ from: 1758426,
+ to: 1758412,
+ type: 'user-initiated',
+ },
+ ]);
+});