From 5a754bcb94d48c92f741a82d0a2247382067f0c9 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Tue, 1 Sep 2026 21:15:28 +0800 Subject: [PATCH] feat(agent): improve timestamp precision and proxy selection for video screenshots (#374) --- .../src/context/serialize-context.test.ts | 8 +-- packages/core/src/transcode/transcode.ts | 6 +- packages/dtos/src/context.ts | 4 +- .../workflows/take-video-screenshots.test.ts | 59 +++++++++++++++++++ .../src/workflows/take-video-screenshots.ts | 15 ++++- 5 files changed, 82 insertions(+), 10 deletions(-) diff --git a/packages/agent/src/context/serialize-context.test.ts b/packages/agent/src/context/serialize-context.test.ts index eccb16b1..2e6496a0 100644 --- a/packages/agent/src/context/serialize-context.test.ts +++ b/packages/agent/src/context/serialize-context.test.ts @@ -119,7 +119,7 @@ describe('serializeContextToXml', () => { - + @@ -315,7 +315,7 @@ describe('serializeContextToXml', () => { expect(refOrder).toEqual(['a_asset', 'm_asset', 'z_asset']) }) - it('normalizes float formatting with fixed .toFixed(2)', () => { + it('normalizes float formatting with fixed precision (toFixed(4) for position, toFixed(2) for duration)', () => { const context1: ShumaiMessageContext = { currentAsset: { id: '1', name: 'a', type: 'file', durationSeconds: 14 }, position: { type: 'time', seconds: 5.5 }, @@ -326,9 +326,9 @@ describe('serializeContextToXml', () => { } expect(serializeContextToXml(context1)).toContain('duration_seconds="14.00"') - expect(serializeContextToXml(context1)).toContain('seconds="5.50"') + expect(serializeContextToXml(context1)).toContain('seconds="5.5000"') expect(serializeContextToXml(context2)).toContain('duration_seconds="14.00"') - expect(serializeContextToXml(context2)).toContain('seconds="5.50"') + expect(serializeContextToXml(context2)).toContain('seconds="5.5000"') }) }) }) diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 7e4026f2..fdb59945 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -1225,12 +1225,12 @@ export class TranscodeService { // 4. Extract each screenshot for (const t of timestamps) { - const outName = `shot-${t.toFixed(3)}-${ulid()}.webp` + const outName = `shot-${t.toFixed(4)}-${ulid()}.webp` const localShotPath = path.join(tmpDir, outName) const args = [ '-ss', - t.toFixed(3), + t.toFixed(4), '-i', videoPath, '-vframes', @@ -1248,7 +1248,7 @@ export class TranscodeService { const isMatch = commentTimestamp !== undefined && commentTimestamp !== null && - Math.abs(t - commentTimestamp) < 1e-4 + Math.abs(t - commentTimestamp) <= 1e-3 // 5. Overlay annotations if timestamp matches commentTimestamp (within float tolerance) if (isMatch && params.annotations && params.annotations.length > 0) { diff --git a/packages/dtos/src/context.ts b/packages/dtos/src/context.ts index a37b3844..4bd95c82 100644 --- a/packages/dtos/src/context.ts +++ b/packages/dtos/src/context.ts @@ -26,7 +26,7 @@ function formatAttr(name: string, value: string | undefined): string { * 1. Tag sequence: , , , , , , * 2. Attribute sequence on every tag follows fixed, invariant order. * 3. Arrays (attachedFiles, referencedAssets) sorted by id ascending. - * 4. Floats formatted with .toFixed(2), integers rounded. + * 4. Time position floats formatted with .toFixed(4), duration with .toFixed(2), integers rounded. * 5. String values escaped via escapeXmlAttr. * 6. Exact 2-space indentation and LF newlines. */ @@ -101,7 +101,7 @@ export function serializeContextToXml(context?: ShumaiMessageContext | null): st // 3. if (context.position) { if (context.position.type === 'time') { - lines.push(` `) + lines.push(` `) } else if (context.position.type === 'page') { lines.push(` `) } diff --git a/packages/transcode/src/workflows/take-video-screenshots.test.ts b/packages/transcode/src/workflows/take-video-screenshots.test.ts index 3b099e9e..ee8bfbe6 100644 --- a/packages/transcode/src/workflows/take-video-screenshots.test.ts +++ b/packages/transcode/src/workflows/take-video-screenshots.test.ts @@ -107,4 +107,63 @@ describe('takeVideoScreenshotsWorkflow', () => { }, }) }) + + it('should prefer highest-resolution video transcode proxy over master storageKey', async () => { + const task: WorkflowTask = { + id: 'task-screenshot-proxy', + assetId: 'asset-video-proxy', + type: WorkflowTaskType.transcode_screenshot, + status: WorkflowTaskStatus.pending, + sessionId: null, + output: null, + payload: { + projectId: 'proj-1', + screenshot: { + start: 1.2345, + end: 1.2345, + count: 1, + commentTimestamp: 1.2345, + annotations: [], + }, + }, + createdAt: new Date(), + updatedAt: new Date(), + heartbeat: null, + teamId: 'team-1', + projectId: 'proj-1', + uid: 'task-uid', + model: null, + inputTokens: 0, + outputTokens: 0, + } + + mockActivities.getAssetActivity.mockResolvedValue({ + id: 'asset-video-proxy', + storageKey: { key: 'raw-master.mp4' }, + mediaType: 'video/mp4', + media: { + videoTranscodes: [ + { key: 'proxy-720p.mp4', height: 720, width: 1280 }, + { key: 'proxy-1080p.mp4', height: 1080, width: 1920 }, + { key: 'proxy-360p.mp4', height: 360, width: 640 }, + ], + }, + }) + + mockActivities.takeScreenshotsActivity.mockResolvedValue([ + { key: 'files/asset-video-proxy/screenshots/shot1.webp', timestamp: 1.2345 }, + ]) + + await takeVideoScreenshotsWorkflow(task) + + expect(mockActivities.takeScreenshotsActivity).toHaveBeenCalledWith({ + assetKey: 'proxy-1080p.mp4', + assetId: 'asset-video-proxy', + start: 1.2345, + end: 1.2345, + count: 1, + commentTimestamp: 1.2345, + annotations: [], + }) + }) }) diff --git a/packages/transcode/src/workflows/take-video-screenshots.ts b/packages/transcode/src/workflows/take-video-screenshots.ts index 185d7d58..3b4c3a59 100644 --- a/packages/transcode/src/workflows/take-video-screenshots.ts +++ b/packages/transcode/src/workflows/take-video-screenshots.ts @@ -17,10 +17,23 @@ export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise< } const { asset, key } = await fetchAssetWithKey(workerQueue, task.assetId) + let targetKey = key + if (asset?.media) { + const mediaInfo = asset.media as unknown as PrismaJson.MediaInfo + if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) { + const sorted = [...mediaInfo.videoTranscodes] + .filter((t) => t.key) + .sort((a, b) => (b.height || 0) - (a.height || 0)) + if (sorted.length > 0 && sorted[0].key) { + targetKey = sorted[0].key + } + } + } + const { takeScreenshotsActivity } = getActivities() const screenshots = await executeActivity(workerQueue, takeScreenshotsActivity, { - assetKey: key, + assetKey: targetKey, assetId: asset.id, start: payload.screenshot.start, end: payload.screenshot.end,