Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ interface Window {
options?: {
audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
audioSourcePath?: string | null;
audioSourceSampleRate?: number;
trimSegments?: Array<{ startMs: number; endMs: number }>;
editedTrackStrategy?: "filtergraph-fast-path" | "offline-render-fallback";
editedTrackSegments?: Array<{ startMs: number; endMs: number; speed: number }>;
editedAudioData?: ArrayBuffer;
editedAudioMimeType?: string | null;
},
Expand All @@ -213,7 +216,10 @@ interface Window {
options?: {
audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
audioSourcePath?: string | null;
audioSourceSampleRate?: number;
trimSegments?: Array<{ startMs: number; endMs: number }>;
editedTrackStrategy?: "filtergraph-fast-path" | "offline-render-fallback";
editedTrackSegments?: Array<{ startMs: number; endMs: number; speed: number }>;
editedAudioData?: ArrayBuffer;
editedAudioMimeType?: string | null;
},
Expand Down
14 changes: 13 additions & 1 deletion electron/ipc/export/native-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
NativeVideoExportFinishOptions,
} from "../nativeVideoExport";
import {
buildEditedTrackSourceAudioFilter,
buildNativeVideoExportArgs,
buildTrimmedSourceAudioFilter,
getEditedAudioExtension,
Expand Down Expand Up @@ -387,8 +388,10 @@ export async function muxNativeVideoExportAudio(
const metrics: NativeVideoAudioMuxMetrics = {};
const tempArtifacts: string[] = [];
let audioInputPath = options.audioSourcePath ?? null;
const useEditedTrackFiltergraph =
audioMode === "edited-track" && options.editedTrackStrategy === "filtergraph-fast-path";

if (audioMode === "edited-track") {
if (audioMode === "edited-track" && !useEditedTrackFiltergraph) {
if (!options.editedAudioData) {
throw new Error("Edited audio data is missing for native export");
}
Expand Down Expand Up @@ -435,6 +438,15 @@ export async function muxNativeVideoExportAudio(
} else {
args.push("-map", "0:v:0", "-map", "1:a:0");
}
} else if (useEditedTrackFiltergraph) {
const filter = buildEditedTrackSourceAudioFilter(
options.editedTrackSegments ?? [],
options.audioSourceSampleRate ?? 0,
);
if (!filter) {
throw new Error("Edited-track filtergraph inputs are incomplete for native export");
}
args.push("-filter_complex", filter, "-map", "0:v:0", "-map", "[aout]");
} else {
args.push("-map", "0:v:0", "-map", "1:a:0");
}
Expand Down
93 changes: 93 additions & 0 deletions electron/ipc/nativeVideoExport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expect, it } from "vitest";
import {
buildEditedTrackSourceAudioFilter,
buildTrimmedSourceAudioFilter,
} from "./nativeVideoExport";

describe("buildTrimmedSourceAudioFilter", () => {
it("concatenates trimmed source segments into a single output label", () => {
expect(
buildTrimmedSourceAudioFilter([
{ startMs: 0, endMs: 2_000 },
{ startMs: 4_000, endMs: 6_000 },
]),
).toBe(
"[1:a]atrim=start=0.000:end=2.000,asetpts=PTS-STARTPTS[trimmed_audio_0];" +
"[1:a]atrim=start=4.000:end=6.000,asetpts=PTS-STARTPTS[trimmed_audio_1];" +
"[trimmed_audio_0][trimmed_audio_1]concat=n=2:v=0:a=1[aout]",
);
});
});

describe("buildEditedTrackSourceAudioFilter", () => {
it("builds a concat filtergraph that pitch-shifts via asetrate for speed changes", () => {
const filter = buildEditedTrackSourceAudioFilter(
[
{ startMs: 0, endMs: 2_000, speed: 1 },
{ startMs: 2_000, endMs: 6_000, speed: 1.5 },
],
44_100,
);

expect(filter).toBe(
"[1:a]atrim=start=0.000:end=2.000,asetpts=PTS-STARTPTS[edited_audio_0];" +
"[1:a]atrim=start=2.000:end=6.000,asetpts=PTS-STARTPTS,asetrate=66150,aresample=44100[edited_audio_1];" +
"[edited_audio_0][edited_audio_1]concat=n=2:v=0:a=1[aout]",
);
});

it("builds a filtergraph for slowdown segments by lowering then resampling the source rate", () => {
const filter = buildEditedTrackSourceAudioFilter(
[{ startMs: 0, endMs: 2_000, speed: 0.5 }],
44_100,
);

expect(filter).toBe(
"[1:a]atrim=start=0.000:end=2.000,asetpts=PTS-STARTPTS,asetrate=22050,aresample=44100[edited_audio_0];" +
"[edited_audio_0]anull[aout]",
);
});

it("returns null when the edited-track filtergraph inputs are incomplete", () => {
expect(buildEditedTrackSourceAudioFilter([], 44_100)).toBeNull();
expect(
buildEditedTrackSourceAudioFilter(
[{ startMs: 0, endMs: 2_000, speed: 1.5 }],
Number.NaN,
),
).toBeNull();
});

it("returns null when the edited-track segments are malformed", () => {
expect(
buildEditedTrackSourceAudioFilter(
[{ startMs: Number.NaN, endMs: 2_000, speed: 1.5 }],
44_100,
),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter([{ startMs: 0, endMs: 2_000, speed: 0 }], 44_100),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter([{ startMs: 0, endMs: 2_000, speed: -1 }], 44_100),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter(
[{ startMs: 0, endMs: 2_000, speed: Number.NaN }],
44_100,
),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter([{ startMs: 0, endMs: 2_000, speed: 1 }], 0.4),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter([{ startMs: -100, endMs: 2_000, speed: 1 }], 44_100),
).toBeNull();
expect(
buildEditedTrackSourceAudioFilter(
[{ startMs: 0, endMs: 2_000, speed: Number.MAX_SAFE_INTEGER }],
44_100,
),
).toBeNull();
});
});
85 changes: 85 additions & 0 deletions electron/ipc/nativeVideoExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const NATIVE_EXPORT_INPUT_BYTES_PER_PIXEL = 4;
export type NativeExportEncodingMode = "fast" | "balanced" | "quality";

export type NativeVideoExportAudioMode = "none" | "copy-source" | "trim-source" | "edited-track";
export type NativeVideoExportEditedTrackStrategy =
| "filtergraph-fast-path"
| "offline-render-fallback";

export interface NativeVideoExportStartOptions {
width: number;
Expand All @@ -18,10 +21,17 @@ export interface NativeVideoExportAudioSegment {
endMs: number;
}

export interface NativeVideoExportEditedTrackSegment extends NativeVideoExportAudioSegment {
speed: number;
}

export interface NativeVideoExportFinishOptions {
audioMode?: NativeVideoExportAudioMode;
audioSourcePath?: string | null;
audioSourceSampleRate?: number;
trimSegments?: NativeVideoExportAudioSegment[];
editedTrackStrategy?: NativeVideoExportEditedTrackStrategy;
editedTrackSegments?: NativeVideoExportEditedTrackSegment[];
editedAudioData?: ArrayBuffer;
editedAudioMimeType?: string | null;
}
Expand Down Expand Up @@ -162,6 +172,81 @@ export function buildTrimmedSourceAudioFilter(
return filterParts.join(";");
}

export function buildEditedTrackSourceAudioFilter(
segments: NativeVideoExportEditedTrackSegment[],
sourceSampleRate: number,
): string | null {
if (segments.length === 0 || !Number.isFinite(sourceSampleRate) || sourceSampleRate <= 0) {
return null;
}

const normalizedSourceSampleRate = Math.round(sourceSampleRate);
if (normalizedSourceSampleRate < 1) {
return null;
}

const filterParts: string[] = [];
const segmentLabels: string[] = [];
let hasInvalidSegment = false;

segments.forEach((segment, index) => {
if (
!Number.isFinite(segment.startMs) ||
!Number.isFinite(segment.endMs) ||
segment.startMs < 0 ||
segment.endMs < 0
) {
hasInvalidSegment = true;
return;
}

if (segment.endMs - segment.startMs <= 0.5) {
hasInvalidSegment = true;
return;
}

const label = `edited_audio_${index}`;
const speed = segment.speed;
if (!Number.isFinite(speed) || speed <= 0) {
hasInvalidSegment = true;
return;
}

const segmentFilter = [
`[1:a]atrim=start=${formatFfmpegSeconds(segment.startMs)}:end=${formatFfmpegSeconds(segment.endMs)}`,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"asetpts=PTS-STARTPTS",
];

if (Math.abs(speed - 1) > 0.0001) {
const adjustedSampleRate = Math.round(normalizedSourceSampleRate * speed);
if (!Number.isSafeInteger(adjustedSampleRate) || adjustedSampleRate < 1) {
hasInvalidSegment = true;
return;
}

segmentFilter.push(
`asetrate=${adjustedSampleRate}`,
`aresample=${normalizedSourceSampleRate}`,
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

filterParts.push(`${segmentFilter.join(",")}[${label}]`);
segmentLabels.push(`[${label}]`);
});

if (hasInvalidSegment || segmentLabels.length === 0) {
return null;
}

if (segmentLabels.length === 1) {
filterParts.push(`${segmentLabels[0]}anull[aout]`);
} else {
filterParts.push(`${segmentLabels.join("")}concat=n=${segmentLabels.length}:v=0:a=1[aout]`);
}

return filterParts.join(";");
}

/**
* Builds FFmpeg arguments for a zero-copy H.264 stream export.
* FFmpeg receives a pre-encoded Annex B H.264 stream on stdin (produced by the
Expand Down
6 changes: 6 additions & 0 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ contextBridge.exposeInMainWorld("electronAPI", {
options?: {
audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
audioSourcePath?: string | null;
audioSourceSampleRate?: number;
trimSegments?: Array<{ startMs: number; endMs: number }>;
editedTrackStrategy?: "filtergraph-fast-path" | "offline-render-fallback";
editedTrackSegments?: Array<{ startMs: number; endMs: number; speed: number }>;
editedAudioData?: ArrayBuffer;
editedAudioMimeType?: string | null;
},
Expand Down Expand Up @@ -186,7 +189,10 @@ contextBridge.exposeInMainWorld("electronAPI", {
options?: {
audioMode?: "none" | "copy-source" | "trim-source" | "edited-track";
audioSourcePath?: string | null;
audioSourceSampleRate?: number;
trimSegments?: Array<{ startMs: number; endMs: number }>;
editedTrackStrategy?: "filtergraph-fast-path" | "offline-render-fallback";
editedTrackSegments?: Array<{ startMs: number; endMs: number; speed: number }>;
editedAudioData?: ArrayBuffer;
editedAudioMimeType?: string | null;
},
Expand Down
Loading