Skip to content

🎥 Make videos programmatically with React

refactor trimleft

Jonny Burger246ed9595ad1
diff --git a/packages/renderer/src/render-frame-with-option-to-reject.ts b/packages/renderer/src/render-frame-with-option-to-reject.ts
index cf5761b6eb..6356076f15 100644
--- a/packages/renderer/src/render-frame-with-option-to-reject.ts
+++ b/packages/renderer/src/render-frame-with-option-to-reject.ts
@@ -256,7 +256,6 @@ export const renderFrameWithOptionToReject = async ({
}
for (const renderAsset of inlineAudioAssets) {
- // TODO: Hardcoded FPS
downloadMap.inlineAudioMixing.addAsset({
asset: renderAsset,
fps,
diff --git a/packages/renderer/src/seamless-aac-trim.ts b/packages/renderer/src/seamless-aac-trim.ts
new file mode 100644
index 0000000000..260e0ced2f
--- /dev/null
+++ b/packages/renderer/src/seamless-aac-trim.ts
@@ -0,0 +1,44 @@
+export const getActualTrimLeft = ({
+ fps,
+ trimLeftOffset,
+ seamless,
+ assetDuration,
+ audioStartFrame,
+ trimLeft,
+ playbackRate,
+}: {
+ trimLeft: number;
+ audioStartFrame: number;
+ fps: number;
+ trimLeftOffset: number;
+ seamless: boolean;
+ assetDuration: number | null;
+ playbackRate: number;
+}): {
+ trimLeft: number;
+ maxTrim: number | null;
+} => {
+ const sinceStart = trimLeft - audioStartFrame;
+
+ if (!seamless) {
+ return {
+ trimLeft:
+ audioStartFrame / fps +
+ (sinceStart / fps) * playbackRate +
+ trimLeftOffset,
+ maxTrim: assetDuration,
+ };
+ }
+
+ if (seamless) {
+ return {
+ trimLeft:
+ audioStartFrame / fps / playbackRate +
+ sinceStart / fps +
+ trimLeftOffset,
+ maxTrim: assetDuration ? assetDuration / playbackRate : null,
+ };
+ }
+
+ throw new Error('This should never happen');
+};
diff --git a/packages/renderer/src/stringify-ffmpeg-filter.ts b/packages/renderer/src/stringify-ffmpeg-filter.ts
index e8ce32b521..118b916a37 100644
--- a/packages/renderer/src/stringify-ffmpeg-filter.ts
+++ b/packages/renderer/src/stringify-ffmpeg-filter.ts
@@ -4,6 +4,7 @@ import type {AssetVolume, MediaAsset} from './assets/types';
import type {LogLevel} from './log-level';
import {Log} from './logger';
import {DEFAULT_SAMPLE_RATE} from './sample-rate';
+import {getActualTrimLeft} from './seamless-aac-trim';
import {truthy} from './truthy';
export type FilterWithoutPaddingApplied = ProcessedTrack & {
@@ -41,47 +42,6 @@ const stringifyTrim = (trim: number) => {
return asString;
};
-export const getActualTrimLeft = ({
- asset,
- fps,
- trimLeftOffset,
- seamless,
- assetDuration,
-}: {
- asset: MediaAsset;
- fps: number;
- trimLeftOffset: number;
- seamless: boolean;
- assetDuration: number | null;
-}): {
- trimLeft: number;
- maxTrim: number | null;
-} => {
- const sinceStart = asset.trimLeft - asset.audioStartFrame;
-
- if (!seamless) {
- return {
- trimLeft:
- asset.audioStartFrame / fps +
- (sinceStart / fps) * asset.playbackRate +
- trimLeftOffset,
- maxTrim: assetDuration,
- };
- }
-
- if (seamless) {
- return {
- trimLeft:
- asset.audioStartFrame / fps / asset.playbackRate +
- sinceStart / fps +
- trimLeftOffset,
- maxTrim: assetDuration ? assetDuration / asset.playbackRate : null,
- };
- }
-
- throw new Error('This should never happen');
-};
-
const trimAndSetTempo = ({
assetDuration,
asset,
@@ -108,11 +68,13 @@ const trimAndSetTempo = ({
// It creates a small offset and the offset needs to be the same for all audio tracks, before processing it further.
// This also affects the trimLeft and trimRight values, as they need to be adjusted.
const {trimLeft, maxTrim} = getActualTrimLeft({
- asset,
+ trimLeft: asset.trimLeft,
fps,
trimLeftOffset,
seamless: true,
assetDuration,
+ audioStartFrame: asset.audioStartFrame,
+ playbackRate: asset.playbackRate,
});
const trimRight =
trimLeft + asset.duration / fps - trimLeftOffset + trimRightOffset;
@@ -181,11 +143,13 @@ export const stringifyFfmpegFilter = ({
const startInVideoSeconds = startInVideo / fps;
const {trimLeft, maxTrim} = getActualTrimLeft({
- asset,
+ trimLeft: asset.trimLeft,
fps,
trimLeftOffset,
seamless: forSeamlessAacConcatenation,
assetDuration,
+ audioStartFrame: asset.audioStartFrame,
+ playbackRate: asset.playbackRate,
});
if (maxTrim && trimLeft >= maxTrim) {