@remotion/media 4.0.357 → 4.0.358

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,12 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext, useEffect, useMemo, useRef, useState } from 'react';
3
- import { Internals, useBufferState, useCurrentFrame, Video } from 'remotion';
3
+ import { Html5Video, Internals, useBufferState, useCurrentFrame } from 'remotion';
4
+ import { useLoopDisplay } from '../show-in-timeline';
5
+ import { useMediaInTimeline } from '../use-media-in-timeline';
4
6
  import { MediaPlayer } from './media-player';
5
- const { useUnsafeVideoConfig, Timeline, SharedAudioContext, useMediaMutedState, useMediaVolumeState, useFrameForVolumeProp, evaluateVolume, warnAboutTooHighVolume, usePreload, useMediaInTimeline, SequenceContext, } = Internals;
6
- const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, muted, volume, loopVolumeCurveBehavior, onVideoFrame, showInTimeline, loop, name, trimAfter, trimBefore, stack, disallowFallbackToOffthreadVideo, fallbackOffthreadVideoProps, audioStreamIndex, }) => {
7
+ const { useUnsafeVideoConfig, Timeline, SharedAudioContext, useMediaMutedState, useMediaVolumeState, useFrameForVolumeProp, evaluateVolume, warnAboutTooHighVolume, usePreload, SequenceContext, SequenceVisibilityToggleContext, } = Internals;
8
+ export const VideoForPreview = ({ src: unpreloadedSrc, style, playbackRate, logLevel, className, muted, volume, loopVolumeCurveBehavior, onVideoFrame, showInTimeline, loop, name, trimAfter, trimBefore, stack, disallowFallbackToOffthreadVideo, fallbackOffthreadVideoProps, audioStreamIndex, }) => {
9
+ const src = usePreload(unpreloadedSrc);
7
10
  const canvasRef = useRef(null);
8
11
  const videoConfig = useUnsafeVideoConfig();
9
12
  const frame = useCurrentFrame();
@@ -17,6 +20,8 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
17
20
  const buffer = useBufferState();
18
21
  const [mediaMuted] = useMediaMutedState();
19
22
  const [mediaVolume] = useMediaVolumeState();
23
+ const [mediaDurationInSeconds, setMediaDurationInSeconds] = useState(null);
24
+ const { hidden } = useContext(SequenceVisibilityToggleContext);
20
25
  const volumePropFrame = useFrameForVolumeProp(loopVolumeCurveBehavior);
21
26
  const userPreferredVolume = evaluateVolume({
22
27
  frame: volumePropFrame,
@@ -24,21 +29,30 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
24
29
  mediaVolume,
25
30
  });
26
31
  warnAboutTooHighVolume(userPreferredVolume);
27
- const [timelineId] = useState(() => String(Math.random()));
28
32
  const parentSequence = useContext(SequenceContext);
29
- useMediaInTimeline({
33
+ const loopDisplay = useLoopDisplay({
34
+ loop,
35
+ mediaDurationInSeconds,
36
+ playbackRate,
37
+ trimAfter,
38
+ trimBefore,
39
+ });
40
+ const { id: timelineId } = useMediaInTimeline({
30
41
  volume,
31
- mediaVolume,
32
42
  mediaType: 'video',
33
43
  src,
34
44
  playbackRate,
35
45
  displayName: name ?? null,
36
- id: timelineId,
37
46
  stack,
38
47
  showInTimeline,
39
48
  premountDisplay: parentSequence?.premountDisplay ?? null,
40
49
  postmountDisplay: parentSequence?.postmountDisplay ?? null,
50
+ loopDisplay,
51
+ mediaVolume,
52
+ trimAfter,
53
+ trimBefore,
41
54
  });
55
+ const isSequenceHidden = hidden[timelineId] ?? false;
42
56
  if (!videoConfig) {
43
57
  throw new Error('No video config found');
44
58
  }
@@ -63,10 +77,9 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
63
77
  logLevel,
64
78
  sharedAudioContext: sharedAudioContext.audioContext,
65
79
  loop,
66
- trimAfterSeconds: trimAfter ? trimAfter / videoConfig.fps : undefined,
67
- trimBeforeSeconds: trimBefore
68
- ? trimBefore / videoConfig.fps
69
- : undefined,
80
+ trimAfter,
81
+ trimBefore,
82
+ fps: videoConfig.fps,
70
83
  playbackRate,
71
84
  audioStreamIndex,
72
85
  });
@@ -108,6 +121,7 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
108
121
  }
109
122
  if (result.type === 'success') {
110
123
  setMediaPlayerReady(true);
124
+ setMediaDurationInSeconds(result.durationInSeconds);
111
125
  }
112
126
  })
113
127
  .catch((error) => {
@@ -189,7 +203,7 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
189
203
  }
190
204
  };
191
205
  }, [mediaPlayerReady, buffer, logLevel]);
192
- const effectiveMuted = muted || mediaMuted || userPreferredVolume <= 0;
206
+ const effectiveMuted = isSequenceHidden || muted || mediaMuted || userPreferredVolume <= 0;
193
207
  useEffect(() => {
194
208
  const mediaPlayer = mediaPlayerRef.current;
195
209
  if (!mediaPlayer || !mediaPlayerReady)
@@ -202,7 +216,7 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
202
216
  return;
203
217
  }
204
218
  mediaPlayer.setVolume(userPreferredVolume);
205
- }, [userPreferredVolume, mediaPlayerReady, logLevel]);
219
+ }, [userPreferredVolume, mediaPlayerReady]);
206
220
  const effectivePlaybackRate = useMemo(() => playbackRate * globalPlaybackRate, [playbackRate, globalPlaybackRate]);
207
221
  useEffect(() => {
208
222
  const mediaPlayer = mediaPlayerRef.current;
@@ -210,7 +224,7 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
210
224
  return;
211
225
  }
212
226
  mediaPlayer.setPlaybackRate(effectivePlaybackRate);
213
- }, [effectivePlaybackRate, mediaPlayerReady, logLevel]);
227
+ }, [effectivePlaybackRate, mediaPlayerReady]);
214
228
  useEffect(() => {
215
229
  const mediaPlayer = mediaPlayerRef.current;
216
230
  if (!mediaPlayer || !mediaPlayerReady) {
@@ -218,6 +232,13 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
218
232
  }
219
233
  mediaPlayer.setLoop(loop);
220
234
  }, [loop, mediaPlayerReady]);
235
+ useEffect(() => {
236
+ const mediaPlayer = mediaPlayerRef.current;
237
+ if (!mediaPlayer || !mediaPlayerReady) {
238
+ return;
239
+ }
240
+ mediaPlayer.setFps(videoConfig.fps);
241
+ }, [videoConfig.fps, mediaPlayerReady]);
221
242
  useEffect(() => {
222
243
  const mediaPlayer = mediaPlayerRef.current;
223
244
  if (!mediaPlayer || !mediaPlayerReady || !onVideoFrame) {
@@ -228,14 +249,16 @@ const NewVideoForPreview = ({ src, style, playbackRate, logLevel, className, mut
228
249
  unsubscribe();
229
250
  };
230
251
  }, [onVideoFrame, mediaPlayerReady]);
252
+ const actualStyle = useMemo(() => {
253
+ return {
254
+ ...style,
255
+ opacity: isSequenceHidden ? 0 : (style?.opacity ?? 1),
256
+ };
257
+ }, [isSequenceHidden, style]);
231
258
  if (shouldFallbackToNativeVideo && !disallowFallbackToOffthreadVideo) {
232
259
  // <Video> will fallback to <VideoForPreview> anyway
233
260
  // not using <OffthreadVideo> because it does not support looping
234
- return (_jsx(Video, { src: src, style: style, className: className, muted: muted, volume: volume, trimAfter: trimAfter, trimBefore: trimBefore, playbackRate: playbackRate, loopVolumeCurveBehavior: loopVolumeCurveBehavior, name: name, loop: loop, showInTimeline: showInTimeline, stack: stack ?? undefined, ...fallbackOffthreadVideoProps }));
261
+ return (_jsx(Html5Video, { src: src, style: actualStyle, className: className, muted: muted, volume: volume, trimAfter: trimAfter, trimBefore: trimBefore, playbackRate: playbackRate, loopVolumeCurveBehavior: loopVolumeCurveBehavior, name: name, loop: loop, showInTimeline: showInTimeline, stack: stack ?? undefined, ...fallbackOffthreadVideoProps }));
235
262
  }
236
- return (_jsx("canvas", { ref: canvasRef, width: videoConfig.width, height: videoConfig.height, style: style, className: classNameValue }));
237
- };
238
- export const VideoForPreview = ({ className, loop, src, logLevel, muted, name, volume, loopVolumeCurveBehavior, onVideoFrame, playbackRate, style, showInTimeline, trimAfter, trimBefore, stack, disallowFallbackToOffthreadVideo, fallbackOffthreadVideoProps, audioStreamIndex, }) => {
239
- const preloadedSrc = usePreload(src);
240
- return (_jsx(NewVideoForPreview, { className: className, logLevel: logLevel, muted: muted, onVideoFrame: onVideoFrame, playbackRate: playbackRate, src: preloadedSrc, style: style, volume: volume, name: name, trimAfter: trimAfter, trimBefore: trimBefore, loop: loop, loopVolumeCurveBehavior: loopVolumeCurveBehavior, showInTimeline: showInTimeline, stack: stack, disallowFallbackToOffthreadVideo: disallowFallbackToOffthreadVideo, fallbackOffthreadVideoProps: fallbackOffthreadVideoProps, audioStreamIndex: audioStreamIndex }));
263
+ return (_jsx("canvas", { ref: canvasRef, width: videoConfig.width, height: videoConfig.height, style: actualStyle, className: classNameValue }));
241
264
  };
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext, useLayoutEffect, useMemo, useRef, useState, } from 'react';
3
3
  import { cancelRender, Internals, Loop, random, useCurrentFrame, useDelayRender, useRemotionEnvironment, useVideoConfig, } from 'remotion';
4
- import { calculateLoopDuration } from '../../../core/src/calculate-loop';
4
+ import { calculateMediaDuration } from '../../../core/src/calculate-media-duration';
5
5
  import { applyVolume } from '../convert-audiodata/apply-volume';
6
6
  import { TARGET_SAMPLE_RATE } from '../convert-audiodata/resample-audiodata';
7
7
  import { frameForVolumeProp } from '../looped-frame';
@@ -202,7 +202,7 @@ export const VideoForRendering = ({ volume: volumeProp, playbackRate, src, muted
202
202
  if (!replaceWithOffthreadVideo.durationInSeconds) {
203
203
  cancelRender(new Error(`Cannot render video ${src}: @remotion/media was unable to render, and fell back to <OffthreadVideo>. Also, "loop" was set, but <OffthreadVideo> does not support looping and @remotion/media could also not determine the duration of the video.`));
204
204
  }
205
- return (_jsx(Loop, { layout: "none", durationInFrames: calculateLoopDuration({
205
+ return (_jsx(Loop, { layout: "none", durationInFrames: calculateMediaDuration({
206
206
  trimAfter: trimAfterValue,
207
207
  mediaDurationInFrames: replaceWithOffthreadVideo.durationInSeconds * fps,
208
208
  playbackRate,
@@ -26,6 +26,7 @@ const extractFrameInternal = async ({ src, timeInSeconds: unloopedTimeInSeconds,
26
26
  playbackRate,
27
27
  trimBefore,
28
28
  fps,
29
+ ifNoMediaDuration: 'fail',
29
30
  });
30
31
  if (timeInSeconds === null) {
31
32
  return {
package/package.json CHANGED
@@ -1,55 +1,55 @@
1
1
  {
2
- "name": "@remotion/media",
3
- "version": "4.0.357",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "module": "dist/esm/index.mjs",
7
- "repository": {
8
- "url": "https://github.com/remotion-dev/remotion/tree/main/packages/media"
9
- },
10
- "sideEffects": false,
11
- "author": "Jonny Burger <jonny@remotion.dev>, Hunain Ahmed <junaidhunain6@gmail.com>",
12
- "bugs": {
13
- "url": "https://github.com/remotion-dev/remotion/issues"
14
- },
15
- "dependencies": {
16
- "mediabunny": "1.23.0",
17
- "webdriverio": "9.19.2",
18
- "remotion": "4.0.357"
19
- },
20
- "peerDependencies": {
21
- "react": ">=16.8.0",
22
- "react-dom": ">=16.8.0"
23
- },
24
- "devDependencies": {
25
- "@vitest/browser": "^3.2.4",
26
- "eslint": "9.19.0",
27
- "react": "19.0.0",
28
- "react-dom": "19.0.0",
29
- "vitest": "3.2.4",
30
- "@remotion/eslint-config-internal": "4.0.357"
31
- },
32
- "keywords": [],
33
- "publishConfig": {
34
- "access": "public"
35
- },
36
- "exports": {
37
- ".": {
38
- "types": "./dist/index.d.ts",
39
- "require": "./dist/index.js",
40
- "module": "./dist/esm/index.mjs",
41
- "import": "./dist/esm/index.mjs"
42
- },
43
- "./package.json": "./package.json"
44
- },
45
- "description": "Experimental WebCodecs-based media tags",
46
- "homepage": "https://remotion.dev/docs/media",
47
- "scripts": {
48
- "if-node-18+": "node -e \"const [maj]=process.versions.node.split('.').map(Number); process.exit(maj>=18?0:1)\"",
49
- "formatting": "prettier --experimental-cli src --check",
50
- "lint": "eslint src",
51
- "watch": "tsc -w",
52
- "test": "node src/test/execute.mjs",
53
- "make": "tsc -d && bun --env-file=../.env.bundle bundle.ts"
54
- }
55
- }
2
+ "name": "@remotion/media",
3
+ "version": "4.0.358",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "module": "dist/esm/index.mjs",
7
+ "repository": {
8
+ "url": "https://github.com/remotion-dev/remotion/tree/main/packages/media"
9
+ },
10
+ "sideEffects": false,
11
+ "author": "Jonny Burger <jonny@remotion.dev>, Hunain Ahmed <junaidhunain6@gmail.com>",
12
+ "bugs": {
13
+ "url": "https://github.com/remotion-dev/remotion/issues"
14
+ },
15
+ "scripts": {
16
+ "if-node-18+": "node -e \"const [maj]=process.versions.node.split('.').map(Number); process.exit(maj>=18?0:1)\"",
17
+ "formatting": "prettier --experimental-cli src --check",
18
+ "lint": "eslint src",
19
+ "watch": "tsc -w",
20
+ "test": "node src/test/execute.mjs",
21
+ "make": "tsc -d && bun --env-file=../.env.bundle bundle.ts"
22
+ },
23
+ "dependencies": {
24
+ "mediabunny": "1.23.0",
25
+ "remotion": "4.0.357",
26
+ "webdriverio": "9.19.2"
27
+ },
28
+ "peerDependencies": {
29
+ "react": ">=16.8.0",
30
+ "react-dom": ">=16.8.0"
31
+ },
32
+ "devDependencies": {
33
+ "@remotion/eslint-config-internal": "4.0.357",
34
+ "@vitest/browser": "^3.2.4",
35
+ "eslint": "9.19.0",
36
+ "react": "19.0.0",
37
+ "react-dom": "19.0.0",
38
+ "vitest": "3.2.4"
39
+ },
40
+ "keywords": [],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "exports": {
45
+ ".": {
46
+ "types": "./dist/index.d.ts",
47
+ "require": "./dist/index.js",
48
+ "module": "./dist/esm/index.mjs",
49
+ "import": "./dist/esm/index.mjs"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "description": "Experimental WebCodecs-based media tags",
54
+ "homepage": "https://remotion.dev/docs/media"
55
+ }
package/LICENSE.md DELETED
@@ -1,49 +0,0 @@
1
- # Remotion License
2
-
3
- In Remotion 5.0, the license will slightly change. [View the changes here](https://github.com/remotion-dev/remotion/pull/3750).
4
-
5
- ---
6
-
7
- Depending on the type of your legal entity, you are granted permission to use Remotion for your project. Individuals and small companies are allowed to use Remotion to create videos for free (even commercial), while a company license is required for for-profit organizations of a certain size. This two-tier system was designed to ensure funding for this project while still allowing the source code to be available and the program to be free for most. Read below for the exact terms of use.
8
-
9
- - [Free License](#free-license)
10
- - [Company License](#company-license)
11
-
12
- ## Free License
13
-
14
- Copyright © 2025 [Remotion](https://www.remotion.dev)
15
-
16
- ### Eligibility
17
-
18
- You are eligible to use Remotion for free if you are:
19
-
20
- - an individual
21
- - a for-profit organization with up to 3 employees
22
- - a non-profit or not-for-profit organization
23
- - evaluating whether Remotion is a good fit, and are not yet using it in a commercial way
24
-
25
- ### Allowed use cases
26
-
27
- Permission is hereby granted, free of charge, to any person eligible for the "Free License", to use the software non-commercially or commercially for the purpose of creating videos and images and to modify the software to their own liking, for the purpose of fulfilling their custom use case or to contribute bug fixes or improvements back to Remotion.
28
-
29
- ### Disallowed use cases
30
-
31
- It is not allowed to copy or modify Remotion code for the purpose of selling, renting, licensing, relicensing, or sublicensing your own derivate of Remotion.
32
-
33
- ### Warranty notice
34
-
35
- The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the author or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
36
-
37
- ### Support
38
-
39
- Support is provided on a best-we-can-do basis via GitHub Issues and Discord.
40
-
41
- ## Company License
42
-
43
- You are required to obtain a Company License to use Remotion if you are not within the group of entities eligible for a Free License. This license will enable you to use Remotion for the allowed use cases specified in the Free License, and give you access to prioritized support (read the [Support Policy](https://www.remotion.dev/docs/support)).
44
-
45
- Visit [remotion.pro](https://www.remotion.pro/license) for pricing and to buy a license.
46
-
47
- ### FAQs
48
-
49
- Are you not sure whether you need a Company License because of an edge case? Here are some [frequently asked questions](https://www.remotion.pro/faq).
@@ -1,2 +0,0 @@
1
- import { type PcmS16AudioData } from './convert-audiodata';
2
- export declare const applyToneFrequency: (audioData: PcmS16AudioData, toneFrequency: number) => PcmS16AudioData;
@@ -1,43 +0,0 @@
1
- import { FORMAT } from './convert-audiodata';
2
- import { resampleAudioData, TARGET_SAMPLE_RATE } from './resample-audiodata';
3
- export const applyToneFrequency = (audioData, toneFrequency) => {
4
- // In FFmpeg, we apply toneFrequency as follows:
5
- // `asetrate=${DEFAULT_SAMPLE_RATE}*${toneFrequency},aresample=${DEFAULT_SAMPLE_RATE},atempo=1/${toneFrequency}`
6
- // So there are 2 steps:
7
- // 1. Change the assumed sample rate
8
- // 2. Resample to 48Khz
9
- // 3. Apply playback rate
10
- const step1 = {
11
- ...audioData,
12
- sampleRate: audioData.sampleRate * toneFrequency,
13
- };
14
- const newNumberOfFrames = Math.round(audioData.numberOfFrames * (TARGET_SAMPLE_RATE / step1.sampleRate));
15
- const step2Data = new Int16Array(newNumberOfFrames * audioData.numberOfChannels);
16
- const chunkSize = audioData.numberOfFrames / newNumberOfFrames;
17
- resampleAudioData({
18
- srcNumberOfChannels: step1.numberOfChannels,
19
- sourceChannels: step1.data,
20
- destination: step2Data,
21
- targetFrames: newNumberOfFrames,
22
- chunkSize,
23
- });
24
- const step2AudioData = {
25
- data: step2Data,
26
- format: FORMAT,
27
- numberOfChannels: step1.numberOfChannels,
28
- numberOfFrames: newNumberOfFrames,
29
- sampleRate: TARGET_SAMPLE_RATE,
30
- timestamp: audioData.timestamp,
31
- };
32
- const step3Data = wsolaInt16Interleaved(step2AudioData.data, step2AudioData.numberOfChannels, toneFrequency);
33
- // Target per-channel length and interleave
34
- const targetPerChan = Math.max(1, Math.round(step2AudioData.numberOfFrames * toneFrequency));
35
- const targetTotal = targetPerChan * step2AudioData.numberOfChannels;
36
- return {
37
- data: step3Data,
38
- numberOfChannels: step2AudioData.numberOfChannels,
39
- numberOfFrames: targetTotal,
40
- sampleRate: TARGET_SAMPLE_RATE,
41
- timestamp: audioData.timestamp,
42
- };
43
- };
@@ -1,13 +0,0 @@
1
- /**
2
- * WSOLA time-scale modification for interleaved Int16 PCM (multi-channel).
3
- * - Preserves pitch approximately while changing tempo by factor f.
4
- * - Works for N interleaved channels.
5
- * - Mitigates head/tail fade-out via overlap-weight normalization and boundary reinforcement.
6
- *
7
- * @param input Interleaved Int16 PCM (e.g., LRLRLR... for stereo)
8
- * @param channels Number of channels (>=1)
9
- * @param f Tempo factor: >1 = faster/shorter, <1 = slower/longer
10
- * @param opts Optional tuning parameters
11
- * @returns Interleaved Int16Array with length ≈ round(input.length * f)
12
- */
13
- export declare function wsolaInt16Interleaved(input: Int16Array, channels: number, f: number): Int16Array;
@@ -1,197 +0,0 @@
1
- function clamp16(x) {
2
- const y = Math.round(x);
3
- return y < -32768 ? -32768 : y > 32767 ? 32767 : y;
4
- }
5
- /**
6
- * WSOLA time-scale modification for interleaved Int16 PCM (multi-channel).
7
- * - Preserves pitch approximately while changing tempo by factor f.
8
- * - Works for N interleaved channels.
9
- * - Mitigates head/tail fade-out via overlap-weight normalization and boundary reinforcement.
10
- *
11
- * @param input Interleaved Int16 PCM (e.g., LRLRLR... for stereo)
12
- * @param channels Number of channels (>=1)
13
- * @param f Tempo factor: >1 = faster/shorter, <1 = slower/longer
14
- * @param opts Optional tuning parameters
15
- * @returns Interleaved Int16Array with length ≈ round(input.length * f)
16
- */
17
- export function wsolaInt16Interleaved(input, channels, f) {
18
- if (!Number.isFinite(f) || f <= 0)
19
- throw new Error('f must be a positive finite number');
20
- if (!Number.isInteger(channels) || channels <= 0)
21
- throw new Error('channels must be a positive integer');
22
- const n = input.length;
23
- if (n === 0)
24
- return new Int16Array(0);
25
- if (n % channels !== 0)
26
- throw new Error('input length must be a multiple of channels');
27
- // Parameters and sensible defaults
28
- const sampleRate = 48000;
29
- const frameMs = 30; // 20–40 ms typical
30
- const overlapRatio = 0.5;
31
- const searchMs = 8; // +/- 8 ms local search
32
- const winKind = 'hann';
33
- const headReinf = 3;
34
- const tailReinf = 3;
35
- // Work per-channel
36
- const samplesPerChannel = (n / channels) | 0;
37
- // Frame and hop sizing
38
- const frameSize = Math.max(128, Math.floor((sampleRate * frameMs) / 1000));
39
- const overlap = Math.floor(frameSize * overlapRatio);
40
- const anaHop = Math.max(1, frameSize - overlap);
41
- const synHop = Math.max(1, Math.round(anaHop * f));
42
- // Search radius in samples
43
- const searchRadius = Math.max(0, Math.floor((sampleRate * searchMs) / 1000));
44
- // Window
45
- const win = new Float32Array(frameSize);
46
- for (let i = 0; i < frameSize; i++) {
47
- const x = (Math.PI * 2 * i) / (frameSize - 1);
48
- win[i] =
49
- winKind === 'hann' ? 0.5 * (1 - Math.cos(x)) : 0.54 - 0.46 * Math.cos(x); // Hamming
50
- }
51
- // Estimate output length per channel and allocate with extra headroom
52
- const estFrames = Math.max(1, Math.ceil(Math.max(0, samplesPerChannel - frameSize) / anaHop) + 1);
53
- const estLen = Math.max(0, frameSize + synHop * (estFrames - 1));
54
- const extraHead = frameSize * (headReinf + 1);
55
- const extraTail = frameSize * (tailReinf + 2);
56
- const outLenAlloc = estLen + searchRadius + extraHead + extraTail;
57
- const out = Array.from({ length: channels }, () => new Float32Array(outLenAlloc));
58
- const outWeight = new Float32Array(outLenAlloc);
59
- // Temporary buffers
60
- const chanFrames = Array.from({ length: channels }, () => new Float32Array(frameSize));
61
- const guideFrame = new Float32Array(frameSize);
62
- // Helpers
63
- function readChannelFrame(chan, start, dst) {
64
- let srcIndex = start * channels + chan;
65
- for (let i = 0; i < frameSize; i++) {
66
- const pos = start + i;
67
- dst[i] = pos >= 0 && pos < samplesPerChannel ? input[srcIndex] : 0;
68
- srcIndex += channels;
69
- }
70
- }
71
- function readGuideFrame(start) {
72
- for (let i = 0; i < frameSize; i++) {
73
- const pos = start + i;
74
- if (pos >= 0 && pos < samplesPerChannel) {
75
- let sum = 0;
76
- const base = pos * channels;
77
- for (let c = 0; c < channels; c++)
78
- sum += input[base + c];
79
- guideFrame[i] = sum / channels;
80
- }
81
- else {
82
- guideFrame[i] = 0;
83
- }
84
- }
85
- }
86
- // Find best local alignment around outPos using normalized cross-correlation
87
- function bestAlignment(outPoss) {
88
- let bestShift = 0;
89
- let bestScore = -Infinity;
90
- for (let shift = -searchRadius; shift <= searchRadius; shift++) {
91
- const pos = outPoss + shift - overlap;
92
- let score = 0;
93
- let normA = 0;
94
- let normB = 0;
95
- for (let i = 0; i < overlap; i++) {
96
- const idx = pos + i;
97
- const outVal = idx >= 0 && idx < outLenAlloc ? out[0][idx] : 0; // channel 0 proxy
98
- const frmVal = guideFrame[i];
99
- score += outVal * frmVal;
100
- normA += outVal * outVal;
101
- normB += frmVal * frmVal;
102
- }
103
- const denom = Math.sqrt((normA || 1e-9) * (normB || 1e-9));
104
- const corr = score / denom;
105
- if (corr > bestScore) {
106
- bestScore = corr;
107
- bestShift = shift;
108
- }
109
- }
110
- return bestShift;
111
- }
112
- // Overlap-add a frame for all channels at writeStart with windowing
113
- function olaAllChannels(writeStart) {
114
- for (let c = 0; c < channels; c++) {
115
- for (let i = 0; i < frameSize; i++) {
116
- const idx = writeStart + i;
117
- if (idx >= 0 && idx < outLenAlloc) {
118
- const w = win[i];
119
- out[c][idx] += chanFrames[c][i] * w;
120
- if (c === 0)
121
- outWeight[idx] += w; // track weights once
122
- }
123
- }
124
- }
125
- }
126
- // 1) Seed: place the first frame at t=0
127
- readGuideFrame(0);
128
- for (let c = 0; c < channels; c++)
129
- readChannelFrame(c, 0, chanFrames[c]);
130
- olaAllChannels(0);
131
- // 2) Head reinforcement: place extra frames whose writeStart <= 0
132
- for (let h = 0; h < headReinf; h++) {
133
- // Option 1: reuse the first analysis position to strengthen early region
134
- const headIn = Math.min(anaHop * h, Math.max(0, samplesPerChannel - frameSize));
135
- readGuideFrame(headIn);
136
- for (let c = 0; c < channels; c++)
137
- readChannelFrame(c, headIn, chanFrames[c]);
138
- // Align around outPos=0 so we bias writeStart near/before 0
139
- const shift = bestAlignment(0);
140
- const writeStart = shift - overlap; // likely negative; ok, we clamp on write
141
- olaAllChannels(writeStart);
142
- }
143
- // 3) Main WSOLA loop
144
- let inPos = anaHop; // next analysis position (we already seeded at 0)
145
- let outPos = synHop; // next synthesis position
146
- while (inPos < samplesPerChannel - 1) {
147
- readGuideFrame(inPos);
148
- for (let c = 0; c < channels; c++)
149
- readChannelFrame(c, inPos, chanFrames[c]);
150
- const shift = bestAlignment(outPos);
151
- const writeStart = outPos + shift - overlap;
152
- olaAllChannels(writeStart);
153
- inPos += anaHop;
154
- outPos += synHop;
155
- // Safety: if we're very close to capacity, break to handle tail separately
156
- if (outPos > outLenAlloc - (frameSize + searchRadius + 8))
157
- break;
158
- }
159
- // 4) Tail reinforcement: ensure the end gets full coverage
160
- // Place a few extra frames around the last outPos using the last available input frames.
161
- for (let t = 0; t < tailReinf; t++) {
162
- const tailIn = Math.max(0, Math.min(samplesPerChannel - frameSize, inPos - anaHop * t));
163
- readGuideFrame(tailIn);
164
- for (let c = 0; c < channels; c++)
165
- readChannelFrame(c, tailIn, chanFrames[c]);
166
- const shift = bestAlignment(outPos);
167
- const writeStart = outPos + shift - overlap;
168
- olaAllChannels(writeStart);
169
- outPos += synHop;
170
- }
171
- // 5) Normalize by accumulated weights BEFORE trimming
172
- for (let i = 0; i < outLenAlloc; i++) {
173
- const w = outWeight[i];
174
- if (w > 1e-9) {
175
- const inv = 1 / w;
176
- for (let c = 0; c < channels; c++)
177
- out[c][i] *= inv;
178
- }
179
- else {
180
- for (let c = 0; c < channels; c++)
181
- out[c][i] = 0;
182
- }
183
- }
184
- // 6) Produce final interleaved Int16Array with length ≈ round(n * f)
185
- const targetPerChan = Math.max(1, Math.round(samplesPerChannel * f));
186
- const targetTotal = targetPerChan * channels;
187
- const result = new Int16Array(targetTotal);
188
- // Interleave and clamp/round
189
- for (let i = 0; i < targetPerChan; i++) {
190
- for (let c = 0; c < channels; c++) {
191
- const v = i < out[c].length ? out[c][i] : 0;
192
- const y = clamp16(v);
193
- result[i * channels + c] = y;
194
- }
195
- }
196
- return result;
197
- }
@@ -1,13 +0,0 @@
1
- import type { LogLevel } from 'remotion';
2
- import type { GetSink } from './video-extraction/get-frames-since-keyframe';
3
- export declare const sinkPromises: Record<string, Promise<GetSink>>;
4
- export declare const getSink: (src: string, logLevel: LogLevel) => Promise<{
5
- getVideo: () => Promise<import("./video-extraction/get-frames-since-keyframe").VideoSinkResult>;
6
- getAudio: (index: number) => Promise<import("./video-extraction/get-frames-since-keyframe").AudioSinkResult>;
7
- actualMatroskaTimestamps: {
8
- observeTimestamp: (startTime: number) => void;
9
- getRealTimestamp: (observedTimestamp: number) => number | null;
10
- };
11
- isMatroska: boolean;
12
- getDuration: () => Promise<number>;
13
- }>;
@@ -1,15 +0,0 @@
1
- import { Internals } from 'remotion';
2
- import { getSinks } from './video-extraction/get-frames-since-keyframe';
3
- export const sinkPromises = {};
4
- export const getSink = (src, logLevel) => {
5
- let promise = sinkPromises[src];
6
- if (!promise) {
7
- Internals.Log.verbose({
8
- logLevel,
9
- tag: '@remotion/media',
10
- }, `Sink for ${src} was not found, creating new sink`);
11
- promise = getSinks(src);
12
- sinkPromises[src] = promise;
13
- }
14
- return promise;
15
- };
package/dist/log.d.ts DELETED
@@ -1,10 +0,0 @@
1
- export declare const logLevels: readonly ["trace", "verbose", "info", "warn", "error"];
2
- export type LogLevel = (typeof logLevels)[number];
3
- export declare const isEqualOrBelowLogLevel: (currentLevel: LogLevel, level: LogLevel) => boolean;
4
- export declare const Log: {
5
- trace: (logLevel: LogLevel, message?: any, ...optionalParams: any[]) => void;
6
- verbose: (logLevel: LogLevel, message?: any, ...optionalParams: any[]) => void;
7
- info: (logLevel: LogLevel, message?: any, ...optionalParams: any[]) => void;
8
- warn: (logLevel: LogLevel, message?: any, ...optionalParams: any[]) => void;
9
- error: (message?: any, ...optionalParams: any[]) => void;
10
- };