2026-07-19 12:32:08 +08:00
|
|
|
import type { WorkflowTask } from '@shumai/db'
|
2026-10-18 11:03:04 +08:00
|
|
|
import { ApplicationFailure } from '@shumai/workflow-core'
|
|
|
|
|
import { takeScreenshotsActivity } from '../activities/transcode'
|
|
|
|
|
import { startTask, fetchAssetWithKey, completeTask, failTask } from './common'
|
2026-07-19 12:32:08 +08:00
|
|
|
|
|
|
|
|
export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise<void> {
|
|
|
|
|
try {
|
2026-10-18 11:03:04 +08:00
|
|
|
await startTask(task)
|
2026-07-19 12:32:08 +08:00
|
|
|
|
|
|
|
|
const payload = task.payload
|
|
|
|
|
if (!payload?.screenshot) {
|
|
|
|
|
throw ApplicationFailure.create({
|
|
|
|
|
message: 'Invalid payload for screenshot task',
|
|
|
|
|
nonRetryable: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 11:03:04 +08:00
|
|
|
const { asset, key } = await fetchAssetWithKey(task.assetId)
|
2026-09-01 21:15:28 +08:00
|
|
|
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)
|
2026-09-24 14:32:47 +08:00
|
|
|
.sort((a, b) => {
|
|
|
|
|
// Prioritize SDR over HDR to avoid washed out screenshots
|
|
|
|
|
if (!a.hdr && b.hdr) return -1
|
|
|
|
|
if (a.hdr && !b.hdr) return 1
|
|
|
|
|
return (b.height || 0) - (a.height || 0)
|
|
|
|
|
})
|
2026-09-01 21:15:28 +08:00
|
|
|
if (sorted.length > 0 && sorted[0].key) {
|
|
|
|
|
targetKey = sorted[0].key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 11:03:04 +08:00
|
|
|
const screenshots = await takeScreenshotsActivity({
|
2026-09-19 10:40:49 +08:00
|
|
|
taskId: task.id,
|
2026-09-01 21:15:28 +08:00
|
|
|
assetKey: targetKey,
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
start: payload.screenshot.start,
|
|
|
|
|
end: payload.screenshot.end,
|
|
|
|
|
count: payload.screenshot.count,
|
|
|
|
|
commentTimestamp: payload.screenshot.commentTimestamp,
|
|
|
|
|
annotations: payload.screenshot.annotations,
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-18 11:03:04 +08:00
|
|
|
await completeTask(task.id, { screenshots })
|
2026-07-19 12:32:08 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`takeVideoScreenshotsWorkflow failed for task ${task.id}:`, err)
|
2026-10-18 11:03:04 +08:00
|
|
|
await failTask(task.id, err)
|
2026-07-19 12:32:08 +08:00
|
|
|
throw err
|
|
|
|
|
}
|
|
|
|
|
}
|