-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathremotion.ts
More file actions
424 lines (374 loc) · 12.5 KB
/
remotion.ts
File metadata and controls
424 lines (374 loc) · 12.5 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* Remotion Lambda rendering service — runtime only.
*
* Handles triggering and polling Remotion Lambda renders for video production.
* Produces both 16:9 (main) and 9:16 (short) video formats.
*
* Deploy functions (deploySite, deployFunction, getOrCreateBucket) live in
* `remotion-deploy.ts` to avoid pulling @rspack/binding into Vercel bundles.
*
* Requires env vars:
* - AWS_ACCESS_KEY_ID
* - AWS_SECRET_ACCESS_KEY
*
* Remotion-specific config (region, serveUrl, functionName) is read from
* Sanity singletons via getConfigValue().
*
* @module lib/services/remotion
*/
import {
renderMediaOnLambda,
getRenderProgress,
type AwsRegion,
} from "@remotion/lambda/client";
import { getConfigValue } from "@/lib/config";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface RemotionLambdaConfig {
awsAccessKeyId: string;
awsSecretAccessKey: string;
region: string;
serveUrl: string;
}
export interface RenderInput {
/** URL to the TTS audio file */
audioUrl: string;
/** Video script data */
script: {
hook: string;
scenes: Array<{
narration: string;
keywords?: string[];
visualDescription?: string;
bRollKeywords?: string[];
sceneNumber?: number;
durationEstimate?: number;
infographicUrl?: string;
}>;
cta: string;
};
/** B-roll video URLs mapped by scene index */
bRollUrls: Record<number, string>;
/** Optional sponsor data */
sponsor?: {
name: string;
logoUrl?: string;
message?: string;
};
/** Audio duration in seconds */
audioDurationSeconds: number;
}
export interface RenderResult {
/** URL to the rendered video file */
videoUrl: string;
/** Render duration in seconds */
renderDurationSeconds: number;
/** Output file size in bytes */
fileSizeBytes: number;
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Default Lambda function name if not configured in Sanity */
const DEFAULT_FUNCTION_NAME = "remotion-render-4-0-431";
/** Polling interval in ms when waiting for render completion */
const POLL_INTERVAL_MS = 2_000;
/** Maximum time to wait for a render before timing out (15 minutes) */
const RENDER_TIMEOUT_MS = 15 * 60 * 1_000;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function log(message: string, data?: Record<string, unknown>): void {
const ts = new Date().toISOString();
if (data) {
console.log(`[REMOTION] [${ts}] ${message}`, data);
} else {
console.log(`[REMOTION] [${ts}] ${message}`);
}
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Map a RenderInput into the inputProps shape expected by the Remotion composition.
*/
function mapInputProps(input: RenderInput): Record<string, unknown> {
return {
audioUrl: input.audioUrl,
audioDurationInSeconds: input.audioDurationSeconds,
hook: input.script.hook,
scenes: input.script.scenes.map((s, i) => ({
narration: s.narration,
visualDescription: s.visualDescription,
bRollKeywords: s.bRollKeywords,
sceneNumber: s.sceneNumber,
durationEstimate: s.durationEstimate,
bRollUrl: input.bRollUrls[i],
...(s.infographicUrl ? { infographicUrl: s.infographicUrl } : {}),
})),
cta: input.script.cta,
sponsor: input.sponsor,
};
}
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
/**
* Get Remotion Lambda configuration from Sanity config + environment variables.
* AWS credentials come from env vars; Remotion-specific config from Sanity.
* Throws if any required value is missing.
*/
export async function getRemotionConfig(): Promise<RemotionLambdaConfig> {
const awsAccessKeyId = process.env.AWS_ACCESS_KEY_ID;
const awsSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const region = await getConfigValue("remotion_config", "awsRegion", "us-east-1");
const serveUrl = await getConfigValue("remotion_config", "serveUrl", undefined);
const missing: string[] = [];
if (!awsAccessKeyId) missing.push("AWS_ACCESS_KEY_ID");
if (!awsSecretAccessKey) missing.push("AWS_SECRET_ACCESS_KEY");
if (!region) missing.push("remotion_config.awsRegion");
if (!serveUrl) missing.push("remotion_config.serveUrl");
if (missing.length > 0) {
throw new Error(
`[REMOTION] Missing required configuration: ${missing.join(", ")}. ` +
`AWS credentials come from env vars. Remotion config (region, serveUrl, functionName) ` +
`is managed in the Sanity Remotion Config singleton. ` +
`serveUrl is generated by running deployRemotionLambda().`
);
}
return {
awsAccessKeyId: awsAccessKeyId!,
awsSecretAccessKey: awsSecretAccessKey!,
region: region!,
serveUrl: serveUrl!,
};
}
/**
* Get the Lambda function name from Sanity config or use the default.
*/
async function getFunctionName(): Promise<string> {
return getConfigValue("remotion_config", "functionName", "remotion-render-4-0-431-mem2048mb-disk2048mb-240sec");
}
// ---------------------------------------------------------------------------
// Types – render start (no polling)
// ---------------------------------------------------------------------------
export interface RenderStartResult {
mainRenderId: string;
shortRenderId: string;
bucketName: string;
}
export interface RenderProgressResult {
done: boolean;
/** 0-100 */
progress: number;
outputUrl?: string;
outputSize?: number;
errors?: string;
}
export interface BothRendersResult {
allDone: boolean;
main: RenderProgressResult;
short: RenderProgressResult;
}
// ---------------------------------------------------------------------------
// Core – start a single render (no polling)
// ---------------------------------------------------------------------------
/**
* Trigger a render on Lambda and return immediately with the render ID.
* Does NOT poll for completion.
*/
async function startRender(
composition: string,
input: RenderInput
): Promise<{ renderId: string; bucketName: string }> {
const config = await getRemotionConfig();
const functionName = await getFunctionName();
const region = config.region as AwsRegion;
log(`Starting render for composition "${composition}"`, {
scenes: input.script.scenes.length,
audioDuration: input.audioDurationSeconds,
functionName,
region,
});
try {
const renderResponse = await renderMediaOnLambda({
composition,
serveUrl: config.serveUrl,
codec: "h264",
inputProps: mapInputProps(input),
region,
functionName,
});
log(`Render triggered successfully`, {
renderId: renderResponse.renderId,
bucketName: renderResponse.bucketName,
composition,
});
return {
renderId: renderResponse.renderId,
bucketName: renderResponse.bucketName,
};
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(
`[REMOTION] Failed to trigger Lambda render for "${composition}": ${message}. ` +
`Ensure the Lambda function "${functionName}" is deployed in ${region} ` +
`and the Remotion Config serveUrl points to a valid Remotion bundle.`
);
}
}
// ---------------------------------------------------------------------------
// Public API – start renders
// ---------------------------------------------------------------------------
/**
* Start both video renders (main 16:9 + short 9:16) in parallel on Lambda.
* Returns render IDs immediately — does NOT poll for completion.
*
* Use `checkBothRenders` to poll for progress separately.
*/
export async function startBothRenders(
input: RenderInput
): Promise<RenderStartResult> {
log(
`Starting parallel render of MainVideo + ShortVideo ` +
`(${input.script.scenes.length} scenes, ${input.audioDurationSeconds}s audio)`
);
const [mainResult, shortResult] = await Promise.all([
startRender("MainVideo", input),
startRender("ShortVideo", input),
]);
// Both should use the same bucket, but we take the main one as canonical
log(`Both renders started`, {
mainRenderId: mainResult.renderId,
shortRenderId: shortResult.renderId,
bucketName: mainResult.bucketName,
});
return {
mainRenderId: mainResult.renderId,
shortRenderId: shortResult.renderId,
bucketName: mainResult.bucketName,
};
}
// ---------------------------------------------------------------------------
// Public API – check render progress
// ---------------------------------------------------------------------------
/**
* Check the progress of a single Remotion Lambda render.
*/
export async function checkRenderProgress(
renderId: string,
bucketName: string
): Promise<RenderProgressResult> {
const config = await getRemotionConfig();
const functionName = await getFunctionName();
const region = config.region as AwsRegion;
const progress = await getRenderProgress({
renderId,
bucketName,
region,
functionName,
});
// Check for fatal errors
if (progress.fatalErrorEncountered) {
const errorMessages = (progress.errors ?? [])
.map((e) => {
if (typeof e === "string") return e;
if (e && typeof e === "object" && "message" in e)
return (e as { message: string }).message;
return JSON.stringify(e);
})
.join("; ");
return {
done: false,
progress: Math.round((progress.overallProgress ?? 0) * 100),
errors:
errorMessages ||
`Fatal error in render ${renderId}. Check CloudWatch logs for "${functionName}" in ${region}.`,
};
}
if (progress.done) {
const outputUrl = progress.outputFile ?? "";
const outputSize = progress.outputSizeInBytes ?? 0;
return {
done: true,
progress: 100,
outputUrl: outputUrl || undefined,
outputSize,
};
}
return {
done: false,
progress: Math.round((progress.overallProgress ?? 0) * 100),
};
}
/**
* Check progress of both main and short renders.
*/
export async function checkBothRenders(
mainRenderId: string,
shortRenderId: string,
bucketName: string
): Promise<BothRendersResult> {
const [main, short] = await Promise.all([
checkRenderProgress(mainRenderId, bucketName),
checkRenderProgress(shortRenderId, bucketName),
]);
return {
allDone: main.done && short.done,
main,
short,
};
}
// ---------------------------------------------------------------------------
// Legacy API (kept for backward compatibility)
// ---------------------------------------------------------------------------
/**
* @deprecated Use `startBothRenders` + `checkBothRenders` instead.
* Render both video formats (main + short) in parallel, polling until done.
*/
export async function renderBothFormats(
input: RenderInput
): Promise<{ main: RenderResult; short: RenderResult }> {
log(
`[DEPRECATED] renderBothFormats called — use startBothRenders + checkBothRenders instead`
);
const startResult = await startBothRenders(input);
// Poll until both are done
const pollSingle = async (
renderId: string,
bucketName: string,
label: string
): Promise<RenderResult> => {
const startTime = Date.now();
while (true) {
const elapsed = Date.now() - startTime;
if (elapsed > RENDER_TIMEOUT_MS) {
throw new Error(
`[REMOTION] Render timed out after ${Math.round(elapsed / 1000)}s (${label}, renderId: ${renderId})`
);
}
await sleep(POLL_INTERVAL_MS);
const result = await checkRenderProgress(renderId, bucketName);
if (result.errors) {
throw new Error(`[REMOTION] Render failed (${label}): ${result.errors}`);
}
if (result.done) {
if (!result.outputUrl) {
throw new Error(`[REMOTION] Render done but no output URL (${label})`);
}
return {
videoUrl: result.outputUrl,
renderDurationSeconds: Math.round(elapsed / 100) / 10,
fileSizeBytes: result.outputSize ?? 0,
};
}
log(`${label} progress: ${result.progress}%`);
}
};
const [main, short] = await Promise.all([
pollSingle(startResult.mainRenderId, startResult.bucketName, "MainVideo"),
pollSingle(startResult.shortRenderId, startResult.bucketName, "ShortVideo"),
]);
return { main, short };
}