Skip to content

🎥 Make videos programmatically with React

nice!

Jonny Burgera2813d098b4f
diff --git a/packages/example/src/OffthreadRemoteVideo/OffthreadRemoteVideo.tsx b/packages/example/src/OffthreadRemoteVideo/OffthreadRemoteVideo.tsx
index a0ad3b5bb3..ec0fb62d09 100644
--- a/packages/example/src/OffthreadRemoteVideo/OffthreadRemoteVideo.tsx
+++ b/packages/example/src/OffthreadRemoteVideo/OffthreadRemoteVideo.tsx
@@ -1,9 +1,9 @@
-import {Audio, Loop, OffthreadVideo, staticFile} from 'remotion';
+import {Loop, OffthreadVideo, staticFile} from 'remotion';
export const OffthreadRemoteVideo: React.FC = () => {
return (
- <Loop durationInFrames={20 * 30}>
- <Audio src={staticFile('sine.wav')} />
+ <Loop durationInFrames={15 * 30}>
+ <OffthreadVideo src={staticFile('vid1.mp4')} />
</Loop>
);
};
diff --git a/packages/renderer/src/calculate-ffmpeg-filters.ts b/packages/renderer/src/calculate-ffmpeg-filters.ts
index f0a97bf2c6..029e523564 100644
--- a/packages/renderer/src/calculate-ffmpeg-filters.ts
+++ b/packages/renderer/src/calculate-ffmpeg-filters.ts
@@ -1,5 +1,6 @@
import {flattenVolumeArray} from './assets/flatten-volume-array';
import type {MediaAsset} from './assets/types';
+import {getClosestAlignedTime} from './combine-audio';
import {DEFAULT_SAMPLE_RATE} from './sample-rate';
import type {FilterWithoutPaddingApplied} from './stringify-ffmpeg-filter';
import {stringifyFfmpegFilter} from './stringify-ffmpeg-filter';
@@ -23,12 +24,18 @@ export const calculateFfmpegFilter = ({
const assetTrimLeft = Math.max(
0,
- (asset.trimLeft * asset.playbackRate) / fps -
+ getClosestAlignedTime(
+ ((asset.trimLeft * asset.playbackRate) / fps) * 1_000_000,
+ ) /
+ 1_000_000 -
2 * (1024 / DEFAULT_SAMPLE_RATE),
);
const assetTrimRight =
- assetTrimLeft +
- (asset.duration * asset.playbackRate) / fps +
+ getClosestAlignedTime(
+ ((asset.trimLeft + asset.duration * asset.playbackRate) / fps) *
+ 1_000_000,
+ ) /
+ 1_000_000 +
2 * (1024 / DEFAULT_SAMPLE_RATE);
return stringifyFfmpegFilter({
diff --git a/packages/renderer/src/combine-audio.ts b/packages/renderer/src/combine-audio.ts
index d75bcb28e7..799728388c 100644
--- a/packages/renderer/src/combine-audio.ts
+++ b/packages/renderer/src/combine-audio.ts
@@ -103,6 +103,7 @@ const combineAudioSeamlessly = async ({
}) => {
const fileList = files
.map((p, i) => {
+ const isLast = i === files.length - 1;
const targetStart = i * chunkDurationInSeconds * 1_000_000;
const endStart = (i + 1) * chunkDurationInSeconds * 1_000_000;
@@ -111,21 +112,22 @@ const combineAudioSeamlessly = async ({
const realDuration = endTime - startTime;
- console.log({realDuration});
-
let inpoint = 0;
if (i > 0) {
// Although we only asked for two frames of padding, ffmpeg will add an
// additional 2 frames of silence at the start of the segment. When we slice out
// our real data with inpoint and outpoint, we'll want remove both the silence
// and the extra frames we asked for.
- inpoint = durationOf1Frame * 5;
+ inpoint = durationOf1Frame * 4;
}
// inpoint is inclusive and outpoint is exclusive. To avoid overlap, we subtract
// the duration of one frame from the outpoint.
// we don't have to subtract a frame if this is the last segment.
- const outpoint = realDuration + inpoint;
+ const outpoint =
+ (i === 0 ? durationOf1Frame * 2 : inpoint) +
+ realDuration -
+ (isLast ? 0 : durationOf1Frame);
return [`file '${p}'`, `inpoint ${inpoint}us`, `outpoint ${outpoint}us`]
.filter(truthy)
diff --git a/packages/renderer/src/preprocess-audio-track.ts b/packages/renderer/src/preprocess-audio-track.ts
index 6325ecc320..7440cfa4df 100644
--- a/packages/renderer/src/preprocess-audio-track.ts
+++ b/packages/renderer/src/preprocess-audio-track.ts
@@ -1,3 +1,5 @@
+import {cpSync} from 'fs';
+import path from 'path';
import type {DownloadMap} from './assets/download-map';
import {getAudioChannelsAndDuration} from './assets/get-audio-channels';
import type {MediaAsset} from './assets/types';
@@ -67,7 +69,8 @@ const preprocessAudioTrackUnlimited = async ({
].flat(2);
await callFf({bin: 'ffmpeg', args, indent, logLevel});
-
+ cpSync(outName, process.cwd() + '/' + 'raw' + path.basename(outName));
+ console.log({outName});
cleanup();
return {outName, filter};
};
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 935bd8795b..6cf9720cd1 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -1,7 +1,6 @@
-import {promises} from 'node:fs';
+import {cpSync, promises} from 'node:fs';
import path from 'node:path';
import type {TRenderAsset} from 'remotion/no-react';
-import {NoReactInternals} from 'remotion/no-react';
import {VERSION} from 'remotion/version';
import {calculateAssetPositions} from './assets/calculate-asset-positions';
import {convertAssetsToFileUrls} from './assets/convert-assets-to-file-urls';
@@ -11,11 +10,8 @@ import type {DownloadMap, RenderAssetInfo} from './assets/download-map';
import {cleanDownloadMap} from './assets/download-map';
import type {Assets} from './assets/types';
import type {AudioCodec} from './audio-codec';
-import {
- getDefaultAudioCodec,
- mapAudioCodecToFfmpegAudioCodecName,
-} from './audio-codec';
-import {callFf, callFfNative} from './call-ffmpeg';
+import {getDefaultAudioCodec} from './audio-codec';
+import {callFfNative} from './call-ffmpeg';
import type {Codec} from './codec';
import {DEFAULT_CODEC} from './codec';
import {codecSupportsMedia} from './codec-supports-media';
@@ -175,7 +171,7 @@ const getAssetsData = async ({
)
).filter(truthy);
- const outName = path.join(downloadMap.audioPreprocessing, `audio.wav`);
+ const outName = path.join(downloadMap.audioPreprocessing, `audio.aac`);
await mergeAudioTrack({
files: preprocessed,
@@ -187,6 +183,8 @@ const getAssetsData = async ({
logLevel,
});
+ cpSync(outName, path.join(process.cwd(), 'mixed.aac'));
+
onProgress(1);
deleteDirectory(downloadMap.audioMixing);
@@ -363,27 +361,8 @@ const innerStitchFramesToVideo = async (
);
}
- // TODO: This can just a `copy` now
- const ffmpegTask = callFf({
- bin: 'ffmpeg',
- args: [
- '-i',
- audio,
- '-c:a',
- mapAudioCodecToFfmpegAudioCodecName(resolvedAudioCodec),
- '-b:a',
- audioBitrate ? audioBitrate : '320k',
- force ? '-y' : null,
- outputLocation ?? tempFile,
- ].filter(NoReactInternals.truthy),
- indent,
- logLevel,
- });
+ cpSync(audio as string, outputLocation ?? (tempFile as string));
- cancelSignal?.(() => {
- ffmpegTask.kill();
- });
- await ffmpegTask;
onProgress?.(expectedFrames);
if (audio) {
deleteDirectory(path.dirname(audio));