1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import {
Output,
Mp4OutputFormat,
WebMOutputFormat,
BufferTarget,
CanvasSource,
QUALITY_LOW,
QUALITY_MEDIUM,
QUALITY_HIGH,
QUALITY_VERY_HIGH,
} from "mediabunny";
import { renderTimelineFrame } from "./timeline-renderer";
import { useTimelineStore } from "@/stores/timeline-store";
import { useMediaStore } from "@/stores/media-store";
import { useProjectStore } from "@/stores/project-store";
import { DEFAULT_FPS } from "@/stores/project-store";
import { ExportOptions, ExportResult } from "@/types/export";
export const DEFAULT_EXPORT_OPTIONS: ExportOptions = {
format: "mp4",
quality: "high",
};
const qualityMap = {
low: QUALITY_LOW,
medium: QUALITY_MEDIUM,
high: QUALITY_HIGH,
very_high: QUALITY_VERY_HIGH,
};
export async function exportProject(
options: ExportOptions
): Promise<ExportResult> {
const { format, quality, fps, onProgress, onCancel } = options;
try {
const timelineStore = useTimelineStore.getState();
const mediaStore = useMediaStore.getState();
const projectStore = useProjectStore.getState();
const { tracks, getTotalDuration } = timelineStore;
const { mediaFiles } = mediaStore;
const { activeProject } = projectStore;
if (!activeProject) {
return { success: false, error: "No active project" };
}
const duration = getTotalDuration();
if (duration === 0) {
return { success: false, error: "Project is empty" };
}
const exportFps = fps || activeProject.fps || DEFAULT_FPS;
const canvasSize = activeProject.canvasSize;
const outputFormat =
format === "webm" ? new WebMOutputFormat() : new Mp4OutputFormat();
// BufferTarget for smaller files, StreamTarget for larger ones
// TODO: Implement StreamTarget
const output = new Output({
format: outputFormat,
target: new BufferTarget(),
});
// Canvas for rendering
const canvas = document.createElement("canvas");
canvas.width = canvasSize.width;
canvas.height = canvasSize.height;
const ctx = canvas.getContext("2d");
if (!ctx) {
return { success: false, error: "Failed to create canvas context" };
}
const videoSource = new CanvasSource(canvas, {
codec: format === "webm" ? "vp9" : "avc", // VP9 for WebM, H.264 for MP4
bitrate: qualityMap[quality],
});
output.addVideoTrack(videoSource, { frameRate: exportFps });
// Start the output
await output.start();
const totalFrames = Math.ceil(duration * exportFps);
let cancelled = false;
// Render each frame
for (let frameIndex = 0; frameIndex < totalFrames; frameIndex++) {
// Check for cancellation
if (onCancel?.()) {
cancelled = true;
break;
}
const time = frameIndex / exportFps;
await renderTimelineFrame({
ctx,
time,
canvasWidth: canvas.width,
canvasHeight: canvas.height,
tracks,
mediaFiles,
backgroundColor:
activeProject.backgroundType === "blur"
? "transparent"
: activeProject.backgroundColor || "#000000",
projectCanvasSize: canvasSize,
});
const frameDuration = 1 / exportFps;
await videoSource.add(time, frameDuration);
onProgress?.(frameIndex / totalFrames);
}
if (cancelled) {
await output.cancel();
return { success: false, cancelled: true };
}
videoSource.close();
await output.finalize();
onProgress?.(1);
return {
success: true,
buffer: output.target.buffer || undefined,
};
} catch (error) {
console.error("Export failed:", error);
return {
success: false,
error: error instanceof Error ? error.message : "Unknown export error",
};
}
}
export function getExportMimeType(format: "mp4" | "webm"): string {
return format === "webm" ? "video/webm" : "video/mp4";
}
export function getExportFileExtension(format: "mp4" | "webm"): string {
return `.${format}`;
}
|