summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/export-utils.ts
blob: 1fc0505b888465504405dce9e38abc9d7e46f1f1 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { initFFmpeg } from "./ffmpeg-utils";
import { TimelineTrack, TimelineElement } from "@/types/timeline";
import { MediaItem } from "@/stores/media-store";
import { TProject } from "@/types/project";

export type ExportOptions = {
  format: "mp4" | "webm" | "mov";
  quality: "low" | "medium" | "high";
  resolution: "720p" | "1080p" | "4k";
  fps: number;
};

export type ExportProgress = {
  phase: "preparing" | "processing" | "finalizing" | "complete";
  progress: number; // 0-100
  message: string;
};

export class VideoExporter {
  private ffmpeg: FFmpeg | null = null;
  private onProgress?: (progress: ExportProgress) => void;

  constructor(onProgress?: (progress: ExportProgress) => void) {
    this.onProgress = onProgress;
  }

  private updateProgress(
    phase: ExportProgress["phase"],
    progress: number,
    message: string
  ) {
    if (this.onProgress) {
      this.onProgress({ phase, progress, message });
    }
  }

  async exportProject(
    project: TProject,
    tracks: TimelineTrack[],
    mediaItems: MediaItem[],
    options: ExportOptions
  ): Promise<Blob> {
    this.updateProgress("preparing", 0, "Initializing FFmpeg...");

    this.ffmpeg = await initFFmpeg();

    // Set up progress callback for FFmpeg
    this.ffmpeg.on("progress", ({ progress }) => {
      this.updateProgress("processing", progress * 100, "Rendering video...");
    });

    try {
      this.updateProgress("preparing", 10, "Analyzing timeline...");

      // Get timeline duration
      const duration = this.calculateTimelineDuration(tracks);
      if (duration === 0) {
        throw new Error("Timeline is empty - nothing to export");
      }

      this.updateProgress("preparing", 20, "Preparing media files...");

      // Process media tracks (video/image/audio)
      const mediaTracks = tracks.filter(
        (track) => track.type === "media" || track.type === "audio"
      );

      if (mediaTracks.length === 0) {
        throw new Error("No media tracks found to export");
      }

      this.updateProgress("preparing", 40, "Setting up export parameters...");

      // Get resolution settings
      const { width, height } = this.getResolutionDimensions(
        options.resolution
      );

      // Create a simple concatenation for now
      // In a full implementation, this would handle overlays, transitions, etc.
      const outputBlob = await this.renderSimpleTimeline(
        mediaTracks,
        mediaItems,
        duration,
        options,
        width,
        height
      );

      this.updateProgress("complete", 100, "Export complete!");
      return outputBlob;
    } catch (error) {
      console.error("Export failed:", error);
      throw error;
    }
  }

  private calculateTimelineDuration(tracks: TimelineTrack[]): number {
    let maxDuration = 0;

    for (const track of tracks) {
      for (const element of track.elements) {
        const elementEnd =
          element.startTime +
          element.duration -
          element.trimStart -
          element.trimEnd;
        maxDuration = Math.max(maxDuration, elementEnd);
      }
    }

    return maxDuration;
  }

  private getResolutionDimensions(resolution: string): {
    width: number;
    height: number;
  } {
    switch (resolution) {
      case "720p":
        return { width: 1280, height: 720 };
      case "1080p":
        return { width: 1920, height: 1080 };
      case "4k":
        return { width: 3840, height: 2160 };
      default:
        return { width: 1920, height: 1080 };
    }
  }

  private async renderSimpleTimeline(
    tracks: TimelineTrack[],
    mediaItems: MediaItem[],
    duration: number,
    options: ExportOptions,
    width: number,
    height: number
  ): Promise<Blob> {
    if (!this.ffmpeg) throw new Error("FFmpeg not initialized");

    // For now, let's implement a simple case: export the first video/image element
    // A full implementation would handle complex timeline rendering

    const firstMediaTrack = tracks.find((track) => track.type === "media");
    if (!firstMediaTrack || firstMediaTrack.elements.length === 0) {
      throw new Error("No media elements found to export");
    }

    const firstElement = firstMediaTrack.elements[0];

    // Type guard to ensure we have a media element
    if (firstElement.type !== "media") {
      throw new Error("First element is not a media element");
    }

    const mediaItem = mediaItems.find(
      (item) => item.id === firstElement.mediaId
    );

    if (!mediaItem || !mediaItem.file) {
      throw new Error("Media file not found for export");
    }

    this.updateProgress("processing", 10, "Processing media file...");

    const inputName = `input.${this.getFileExtension(mediaItem.file.name)}`;
    const outputName = `output.${options.format}`;

    // Write input file
    await this.ffmpeg.writeFile(
      inputName,
      new Uint8Array(await mediaItem.file.arrayBuffer())
    );

    // Build FFmpeg command based on media type and options
    const ffmpegArgs = this.buildFFmpegCommand(
      inputName,
      outputName,
      firstElement,
      options,
      width,
      height,
      duration
    );

    this.updateProgress("processing", 30, "Rendering video...");

    // Execute FFmpeg command
    await this.ffmpeg.exec(ffmpegArgs);

    this.updateProgress("finalizing", 90, "Finalizing export...");

    // Read output file
    const data = await this.ffmpeg.readFile(outputName);
    const blob = new Blob([data], { type: `video/${options.format}` });

    // Cleanup
    await this.ffmpeg.deleteFile(inputName);
    await this.ffmpeg.deleteFile(outputName);

    return blob;
  }

  private buildFFmpegCommand(
    inputName: string,
    outputName: string,
    element: TimelineElement,
    options: ExportOptions,
    width: number,
    height: number,
    totalDuration: number
  ): string[] {
    const args = ["-i", inputName];

    // Handle trimming
    if (element.trimStart > 0) {
      args.push("-ss", element.trimStart.toString());
    }

    // Set duration
    const elementDuration =
      element.duration - element.trimStart - element.trimEnd;
    args.push("-t", Math.min(elementDuration, totalDuration).toString());

    // Set resolution and scaling
    args.push(
      "-vf",
      `scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2`
    );

    // Set frame rate
    args.push("-r", options.fps.toString());

    // Set quality/bitrate based on options
    const { videoBitrate, audioBitrate } = this.getQualitySettings(
      options.quality
    );
    args.push("-b:v", videoBitrate, "-b:a", audioBitrate);

    // Set codec based on format
    if (options.format === "mp4") {
      args.push("-c:v", "libx264", "-c:a", "aac");
    } else if (options.format === "webm") {
      args.push("-c:v", "libvpx-vp9", "-c:a", "libopus");
    }

    args.push(outputName);
    return args;
  }

  private getQualitySettings(quality: string): {
    videoBitrate: string;
    audioBitrate: string;
  } {
    switch (quality) {
      case "low":
        return { videoBitrate: "1M", audioBitrate: "128k" };
      case "medium":
        return { videoBitrate: "3M", audioBitrate: "192k" };
      case "high":
        return { videoBitrate: "8M", audioBitrate: "320k" };
      default:
        return { videoBitrate: "3M", audioBitrate: "192k" };
    }
  }

  private getFileExtension(filename: string): string {
    return filename.split(".").pop() || "mp4";
  }

  cleanup() {
    if (this.ffmpeg) {
      // Remove all progress listeners
      this.ffmpeg.off("progress", () => {});
    }
  }
}

// Utility function to download blob as file
export function downloadBlob(blob: Blob, filename: string) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}