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
7 changes: 4 additions & 3 deletions apps/server/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ realizeEvents.waitUntilReady()
.then(() => {
realizeEvents.on("completed", async ({ jobId, returnvalue }) => {
const result = RealizeJobOutputsSchema.parse(typeof returnvalue === "object" ? returnvalue : JSON.parse(returnvalue));
const { videoKey, thumbnailKey, timelapseId } = result;
const { videoKey, thumbnailKey, timelapseId, realTimeDuration } = result;

logInfo(`Timelapse ${timelapseId} finished processing! job=${jobId}`, { videoKey, thumbnailKey });
logInfo(`Timelapse ${timelapseId} finished processing! job=${jobId}`, { videoKey, thumbnailKey, realTimeDuration });

const draft = await database().draftTimelapse.findFirst({
where: {
Expand Down Expand Up @@ -83,7 +83,8 @@ realizeEvents.waitUntilReady()
data: {
associatedJobId: null,
s3Key: videoKey,
thumbnailS3Key: thumbnailKey
thumbnailS3Key: thumbnailKey,
...(realTimeDuration != null && { duration: realTimeDuration })
},
include: { owner: true }
});
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/routers/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,16 @@ export default os.router({
.use(requiredAuth("ADMIN"))
.use(requiredScopes("elevated"))
.handler(async () => {
// Only recalculate durations for timelapses that haven't been realized yet.
// Realized timelapses have their duration set from the compiled video, which is the source of truth.
const PAGE_SIZE = 100;
let updated = 0;
let cursor: string | undefined;

while (true) {
const batch = await database().timelapse.findMany({
select: { id: true, snapshots: true },
where: { s3Key: null },
Comment on lines +519 to +528
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint now explicitly skips realized timelapses (where: { s3Key: null }), but the public admin contract/docs still describe it as “recalculates the duration of every timelapse from its snapshots”. Please update the API contract description (and/or rename/add params) so callers aren’t misled about what will be updated.

Copilot uses AI. Check for mistakes.
take: PAGE_SIZE,
orderBy: { id: "asc" },
...(cursor ? { skip: 1, cursor: { id: cursor } } : {})
Expand All @@ -549,7 +552,7 @@ export default os.router({
}
}

logInfo(`Recalculated durations for ${updated} timelapses.`);
logInfo(`Recalculated durations for ${updated} unrealized timelapses (skipped realized timelapses).`);

return apiOk({ updated });
}),
Expand Down
8 changes: 6 additions & 2 deletions apps/worker/src/workers/realize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ export const realizeJobWorker = new Worker<RealizeJobInputs, RealizeJobOutputs>(
]);

// Thumbnail generation - we opt for a simple approach where we just get the frame in the middle of the video.
const thumbnailTimestamp = (await measureVideoDuration(outputPath)) / 2;
const videoDuration = await measureVideoDuration(outputPath);
const realTimeDuration = videoDuration * TIMELAPSE_FACTOR;
log.info(`output video is ${videoDuration.toFixed(2)}s (real-time: ${realTimeDuration.toFixed(0)}s)`);
const thumbnailTimestamp = videoDuration / 2;

// Arguments to generate thumbnails regardless of output format
let thumbnailContentType = "image/avif";
Expand Down Expand Up @@ -344,7 +347,8 @@ export const realizeJobWorker = new Worker<RealizeJobInputs, RealizeJobOutputs>(
return {
timelapseId,
videoKey,
thumbnailKey
thumbnailKey,
realTimeDuration
};
}
finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/contracts/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const adminRouterContract = {
})),

recalculateDurations: contract("POST", "/admin/recalculateDurations")
.route({ description: "Recalculates the duration of every timelapse from its snapshots. Requires administrator permissions and an `elevated` grant." })
.route({ description: "Recalculates the duration of timelapses that have no compiled video (`s3Key` is null) from their snapshots. Timelapses with a compiled video are skipped because their duration is derived from the video. Requires administrator permissions and an `elevated` grant." })
.input(NO_INPUT)
.output(apiResult({
updated: z.number().int().nonnegative()
Expand Down
10 changes: 9 additions & 1 deletion packages/jobs/src/realize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ export const RealizeJobOutputsSchema = z.object({
/**
* The S3 key for the thumbnail, stored in the public S3 bucket, shared by both the server and the worker.
*/
thumbnailKey: z.string()
thumbnailKey: z.string(),

/**
* The real-time duration of the timelapse in seconds, derived from the compiled video duration
* (as measured by ffprobe) multiplied by `TIMELAPSE_FACTOR`. This value can be stored directly
* as the timelapse `duration` without further conversion.
* Optional for backwards compatibility with in-flight jobs that predate this field.
*/
realTimeDuration: z.number().gt(0).finite().optional()
});

/**
Expand Down
Loading