Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurger75f098b9ecdf
diff --git a/packages/media/src/audio-extraction/extract-audio.ts b/packages/media/src/audio-extraction/extract-audio.ts
index bb47bc0be2..64bed46edd 100644
--- a/packages/media/src/audio-extraction/extract-audio.ts
+++ b/packages/media/src/audio-extraction/extract-audio.ts
@@ -72,6 +72,10 @@ const extractAudioInternal = async ({
trimBefore,
fps,
});
+ if (timeInSeconds === null) {
+ return {data: null, durationInSeconds: mediaDurationInSeconds};
+ }
+
const sampleIterator = await audioManager.getIterator({
src,
timeInSeconds,
diff --git a/packages/media/src/get-time-in-seconds.ts b/packages/media/src/get-time-in-seconds.ts
index 6c021df61a..c492cf5de1 100644
--- a/packages/media/src/get-time-in-seconds.ts
+++ b/packages/media/src/get-time-in-seconds.ts
@@ -37,5 +37,15 @@ export const getTimeInSeconds = ({
const timeInSeconds = (unloopedTimeInSeconds * playbackRate) % loopDuration;
+ if ((trimAfter ?? null) !== null) {
+ if (!loop) {
+ const time = (trimAfter! - (trimBefore ?? 0)) / fps;
+
+ if (timeInSeconds >= time) {
+ return null;
+ }
+ }
+ }
+
return timeInSeconds + (trimBefore ?? 0) / fps;
};
diff --git a/packages/media/src/test/loop-trim-time-calculation.test.ts b/packages/media/src/test/loop-trim-time-calculation.test.ts
new file mode 100644
index 0000000000..de9eb5dfc9
--- /dev/null
+++ b/packages/media/src/test/loop-trim-time-calculation.test.ts
@@ -0,0 +1,62 @@
+import {expect, test} from 'vitest';
+import {getTimeInSeconds} from '../get-time-in-seconds';
+
+test('should inhibit the same behavior as <Html5Video> - no loop', () => {
+ const timestamp = getTimeInSeconds({
+ fps: 25,
+ loop: false,
+ mediaDurationInSeconds: 10,
+ playbackRate: 1,
+ src: 'https://remotion.media/video.mp4',
+ trimAfter: 200,
+ trimBefore: 100,
+ unloopedTimeInSeconds: 4.04,
+ });
+
+ expect(timestamp).toBe(null);
+});
+
+test('should inhibit the same loop behavior as <Html5Video> - loop + trim after', () => {
+ const timestamp = getTimeInSeconds({
+ fps: 25,
+ loop: true,
+ mediaDurationInSeconds: 10,
+ playbackRate: 1,
+ src: 'https://remotion.media/video.mp4',
+ trimAfter: 200,
+ trimBefore: 100,
+ unloopedTimeInSeconds: 4.04,
+ });
+
+ expect(timestamp).toBe(4.04);
+});
+
+test('should inhibit the same loop behavior as <Html5Video> - loop + no trim after', () => {
+ const timestamp = getTimeInSeconds({
+ fps: 25,
+ loop: true,
+ mediaDurationInSeconds: 10,
+ playbackRate: 1,
+ src: 'https://remotion.media/video.mp4',
+ trimAfter: undefined,
+ trimBefore: 100,
+ unloopedTimeInSeconds: 4.04,
+ });
+
+ expect(timestamp).toBe(8.04);
+});
+
+test('should inhibit the same loop behavior as <Html5Video> - edge case', () => {
+ const timestamp = getTimeInSeconds({
+ fps: 25,
+ loop: false,
+ mediaDurationInSeconds: 10,
+ playbackRate: 1,
+ src: 'https://remotion.media/video.mp4',
+ trimAfter: 100,
+ trimBefore: 0,
+ unloopedTimeInSeconds: 4,
+ });
+
+ expect(timestamp).toBe(null);
+});
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index e9f61058ee..37bb164bed 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -67,6 +67,14 @@ const extractFrameInternal = async ({
fps,
});
+ if (timeInSeconds === null) {
+ return {
+ type: 'success',
+ frame: null,
+ durationInSeconds: await sink.getDuration(),
+ };
+ }
+
const keyframeBank = await keyframeManager.requestKeyframeBank({
packetSink: video.packetSink,
videoSampleSink: video.sampleSink,