2026-07-19 12:32:08 +08:00
|
|
|
import type { WorkflowTask } from '@shumai/db'
|
|
|
|
|
import '@shumai/db/src/prisma-json-types'
|
2026-10-19 15:35:08 +08:00
|
|
|
import { updateAssetStatusActivity } from '@shumai/workflow-core'
|
2026-07-19 12:32:08 +08:00
|
|
|
import {
|
2026-10-19 15:35:08 +08:00
|
|
|
downloadMediaToTmpActivity,
|
|
|
|
|
getMediaInfoActivity,
|
|
|
|
|
transcodeVideoActivity,
|
|
|
|
|
transcodeHlsActivity,
|
|
|
|
|
transcodeAudioActivity,
|
|
|
|
|
transcodeImageActivity,
|
|
|
|
|
extractPosterActivity,
|
|
|
|
|
generateSpriteActivity,
|
|
|
|
|
updateAssetMediaActivity,
|
|
|
|
|
createEmbeddingTaskIfEnabledActivity,
|
|
|
|
|
createAutofillTaskIfEnabledActivity,
|
|
|
|
|
} from '../activities/transcode'
|
|
|
|
|
import { startTask, fetchAssetWithKey, completeTask, failTask, cleanupTmpDir } from './common'
|
2026-09-30 16:31:40 +08:00
|
|
|
import {
|
|
|
|
|
getTargetVideoResolutions,
|
|
|
|
|
getTargetHlsResolutions,
|
|
|
|
|
resolutionToDimensions,
|
|
|
|
|
} from './transcode-utils'
|
2026-07-19 12:32:08 +08:00
|
|
|
|
|
|
|
|
export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void> {
|
|
|
|
|
let tmpDir: string | undefined
|
2026-10-02 20:09:32 +08:00
|
|
|
let mediaProcessed = false
|
2026-07-19 12:32:08 +08:00
|
|
|
|
|
|
|
|
try {
|
2026-10-19 15:35:08 +08:00
|
|
|
await startTask(task)
|
2026-07-19 12:32:08 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetStatusActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: task.assetId,
|
|
|
|
|
status: 'processing',
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const { asset, key } = await fetchAssetWithKey(task.assetId)
|
2026-07-19 12:32:08 +08:00
|
|
|
|
|
|
|
|
const spec = task.payload?.transcode || {}
|
2026-07-20 10:22:36 +08:00
|
|
|
const proxyType = asset.mediaType?.startsWith('audio/') ? 'audio' : 'video'
|
2026-10-19 15:35:08 +08:00
|
|
|
const mediaInfo = await getMediaInfoActivity({
|
|
|
|
|
assetKey: key,
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: asset.id,
|
2026-07-20 10:22:36 +08:00
|
|
|
proxyType,
|
2026-07-19 12:32:08 +08:00
|
|
|
mediaType: asset.mediaType || '',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mediaInfo.original = {
|
|
|
|
|
key,
|
|
|
|
|
filesizeInBytes: 0,
|
|
|
|
|
codec: '',
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 13:51:17 +08:00
|
|
|
const isVideo = mediaInfo.proxyType === 'video'
|
|
|
|
|
|
|
|
|
|
if (spec.poster && isVideo) {
|
2026-09-08 10:17:15 +08:00
|
|
|
const lastSlashIndex = key.lastIndexOf('/')
|
|
|
|
|
const assetDir = lastSlashIndex === -1 ? '' : key.substring(0, lastSlashIndex)
|
|
|
|
|
const posterSpec: PrismaJson.PosterInfo = {
|
|
|
|
|
key: assetDir ? `${assetDir}/poster.webp` : 'poster.webp',
|
|
|
|
|
}
|
2026-10-19 15:35:08 +08:00
|
|
|
const posterResult = await extractPosterActivity({
|
2026-09-08 10:17:15 +08:00
|
|
|
assetKey: key,
|
|
|
|
|
posterSpec,
|
2026-09-19 10:40:49 +08:00
|
|
|
taskId: task.id,
|
2026-09-24 14:32:47 +08:00
|
|
|
isHdr: mediaInfo.metadata?.isHdr,
|
|
|
|
|
hdrType: mediaInfo.metadata?.hdrType,
|
|
|
|
|
colorTransfer: mediaInfo.metadata?.colorTransfer,
|
2026-10-02 13:51:17 +08:00
|
|
|
streamIndex: mediaInfo.metadata?.videoStreamIndex,
|
2026-09-08 10:17:15 +08:00
|
|
|
})
|
|
|
|
|
mediaInfo.poster = posterResult.poster
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetMediaActivity({
|
2026-09-08 10:17:15 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
mediaInfo,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const download = await downloadMediaToTmpActivity({
|
|
|
|
|
assetKey: key,
|
|
|
|
|
})
|
|
|
|
|
const { filePath } = download
|
|
|
|
|
tmpDir = download.tmpDir
|
|
|
|
|
|
2026-09-19 15:25:41 +08:00
|
|
|
// Generate the sprite (and its poster fallback) before the expensive proxy transcodes so the
|
|
|
|
|
// file list can show a poster/sprite preview while the asset is still processing.
|
2026-10-02 13:51:17 +08:00
|
|
|
if ((spec.sprite || (spec.poster && !mediaInfo.poster)) && isVideo) {
|
2026-09-19 15:25:41 +08:00
|
|
|
const lastSlashIndex = key.lastIndexOf('/')
|
|
|
|
|
const assetDir = lastSlashIndex === -1 ? '' : key.substring(0, lastSlashIndex)
|
|
|
|
|
|
|
|
|
|
const spriteSpec: PrismaJson.SpriteInfo = {
|
|
|
|
|
key: assetDir ? `${assetDir}/sprite.webp` : 'sprite.webp',
|
|
|
|
|
frames: 100,
|
|
|
|
|
tileX: 10,
|
|
|
|
|
tileY: 10,
|
|
|
|
|
}
|
|
|
|
|
const posterSpec: PrismaJson.PosterInfo = mediaInfo.poster || {
|
|
|
|
|
key: assetDir ? `${assetDir}/poster.webp` : 'poster.webp',
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const spriteResult = await generateSpriteActivity({
|
2026-09-19 15:25:41 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
|
|
|
|
spriteSpec,
|
|
|
|
|
posterSpec,
|
|
|
|
|
mediaInfo,
|
|
|
|
|
})
|
|
|
|
|
if (spec.sprite) {
|
|
|
|
|
mediaInfo.sprite = spriteResult.sprite
|
|
|
|
|
}
|
|
|
|
|
if (!mediaInfo.poster) {
|
|
|
|
|
mediaInfo.poster = spriteResult.poster
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetMediaActivity({
|
2026-09-19 15:25:41 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
mediaInfo,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 12:32:08 +08:00
|
|
|
const metadata = mediaInfo.metadata
|
|
|
|
|
|
2026-10-02 13:51:17 +08:00
|
|
|
if (isVideo && metadata) {
|
2026-07-19 12:32:08 +08:00
|
|
|
const videoResolutions = getTargetVideoResolutions(
|
|
|
|
|
spec.videoStrategy || 'best_match',
|
|
|
|
|
metadata.originalWidth,
|
|
|
|
|
metadata.originalHeight,
|
2026-10-01 13:27:50 +08:00
|
|
|
spec.videoResolutions,
|
2026-07-19 12:32:08 +08:00
|
|
|
)
|
|
|
|
|
for (const res of videoResolutions) {
|
|
|
|
|
const [width, height] = resolutionToDimensions(
|
|
|
|
|
res,
|
|
|
|
|
metadata.originalWidth,
|
|
|
|
|
metadata.originalHeight,
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-24 14:32:47 +08:00
|
|
|
const isHdr = Boolean(metadata.isHdr)
|
|
|
|
|
|
|
|
|
|
if (res === '180p') {
|
|
|
|
|
const videoSpec: PrismaJson.VideoTranscode = {
|
|
|
|
|
resolution: res,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
hdr: isHdr,
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const videoTranscode = await transcodeVideoActivity({
|
2026-09-24 14:32:47 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
|
|
|
|
videoSpec,
|
|
|
|
|
duration: metadata.duration,
|
|
|
|
|
originalFps: metadata.frameRate,
|
|
|
|
|
hardwareAcceleration: spec.hardwareAcceleration,
|
|
|
|
|
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
|
|
|
|
threads: spec.threads,
|
|
|
|
|
sourceIsHdr: metadata.isHdr,
|
|
|
|
|
sourceHdrType: metadata.hdrType,
|
|
|
|
|
sourceColorTransfer: metadata.colorTransfer,
|
|
|
|
|
sourceColorPrimaries: metadata.colorPrimaries,
|
|
|
|
|
sourceColorSpace: metadata.colorSpace,
|
2026-10-05 00:00:01 -04:00
|
|
|
sourceRotation: metadata.rotation ?? 0,
|
2026-10-02 13:51:17 +08:00
|
|
|
streamIndex: metadata.videoStreamIndex,
|
|
|
|
|
audioStreamIndex: metadata.audioStreamIndex,
|
2026-09-24 14:32:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
mediaInfo.videoPreview = videoTranscode
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 12:32:08 +08:00
|
|
|
const videoSpec: PrismaJson.VideoTranscode = {
|
|
|
|
|
resolution: res,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
2026-09-24 14:32:47 +08:00
|
|
|
hdr: isHdr,
|
2026-07-19 12:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
const videoTranscode = await transcodeVideoActivity({
|
2026-09-19 10:40:49 +08:00
|
|
|
taskId: task.id,
|
2026-07-19 12:32:08 +08:00
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
|
|
|
|
videoSpec,
|
|
|
|
|
duration: metadata.duration,
|
|
|
|
|
originalFps: metadata.frameRate,
|
2026-08-26 16:28:14 +08:00
|
|
|
hardwareAcceleration: spec.hardwareAcceleration,
|
|
|
|
|
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
2026-09-18 13:30:33 +08:00
|
|
|
threads: spec.threads,
|
2026-09-24 14:32:47 +08:00
|
|
|
sourceIsHdr: metadata.isHdr,
|
|
|
|
|
sourceHdrType: metadata.hdrType,
|
|
|
|
|
sourceColorTransfer: metadata.colorTransfer,
|
|
|
|
|
sourceColorPrimaries: metadata.colorPrimaries,
|
|
|
|
|
sourceColorSpace: metadata.colorSpace,
|
2026-10-05 00:00:01 -04:00
|
|
|
// rotation is undefined when the probed source has none
|
|
|
|
|
sourceRotation: metadata.rotation ?? 0,
|
2026-10-02 13:51:17 +08:00
|
|
|
streamIndex: metadata.videoStreamIndex,
|
|
|
|
|
audioStreamIndex: metadata.audioStreamIndex,
|
2026-07-19 12:32:08 +08:00
|
|
|
})
|
|
|
|
|
|
2026-09-24 14:32:47 +08:00
|
|
|
mediaInfo.videoTranscodes.push(videoTranscode)
|
2026-07-19 12:32:08 +08:00
|
|
|
}
|
2026-09-30 16:31:40 +08:00
|
|
|
|
|
|
|
|
if (spec.hlsEnabled) {
|
|
|
|
|
const hlsLadders = getTargetHlsResolutions(
|
|
|
|
|
spec.hlsResolutions,
|
|
|
|
|
metadata.originalWidth,
|
|
|
|
|
metadata.originalHeight,
|
|
|
|
|
)
|
|
|
|
|
if (hlsLadders.length > 0) {
|
2026-10-19 15:35:08 +08:00
|
|
|
const hlsResult = await transcodeHlsActivity({
|
2026-09-30 16:31:40 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
|
|
|
|
ladders: hlsLadders,
|
|
|
|
|
originalWidth: metadata.originalWidth,
|
|
|
|
|
originalHeight: metadata.originalHeight,
|
|
|
|
|
originalFps: metadata.frameRate,
|
2026-10-19 15:35:08 +08:00
|
|
|
duration: metadata.duration,
|
2026-09-30 16:31:40 +08:00
|
|
|
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
|
|
|
|
hardwareAcceleration: spec.hardwareAcceleration,
|
|
|
|
|
threads: spec.threads,
|
|
|
|
|
sourceIsHdr: metadata.isHdr,
|
|
|
|
|
sourceHdrType: metadata.hdrType,
|
|
|
|
|
sourceColorTransfer: metadata.colorTransfer,
|
|
|
|
|
sourceColorPrimaries: metadata.colorPrimaries,
|
|
|
|
|
sourceColorSpace: metadata.colorSpace,
|
2026-10-05 00:00:01 -04:00
|
|
|
sourceRotation: metadata.rotation ?? 0,
|
2026-10-02 13:51:17 +08:00
|
|
|
streamIndex: metadata.videoStreamIndex,
|
|
|
|
|
audioStreamIndex: metadata.audioStreamIndex,
|
2026-09-30 16:31:40 +08:00
|
|
|
})
|
|
|
|
|
mediaInfo.isHls = true
|
|
|
|
|
mediaInfo.hls = hlsResult
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-19 12:32:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-20 14:37:17 +08:00
|
|
|
const isAudio = mediaInfo.proxyType === 'audio'
|
2026-07-19 12:32:08 +08:00
|
|
|
if (isAudio && metadata) {
|
2026-10-19 15:35:08 +08:00
|
|
|
const audioTranscode = await transcodeAudioActivity({
|
2026-09-19 10:40:49 +08:00
|
|
|
taskId: task.id,
|
2026-07-19 12:32:08 +08:00
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
2026-09-18 13:30:33 +08:00
|
|
|
threads: spec.threads,
|
2026-10-02 13:51:17 +08:00
|
|
|
audioStreamIndex: metadata.audioStreamIndex,
|
2026-07-19 12:32:08 +08:00
|
|
|
})
|
|
|
|
|
mediaInfo.videoTranscodes.push(audioTranscode)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 13:51:17 +08:00
|
|
|
if (spec.thumbnail && isVideo) {
|
2026-10-19 15:35:08 +08:00
|
|
|
const thumbTranscode = await transcodeImageActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
assetKey: key,
|
|
|
|
|
filePath,
|
2026-08-08 15:17:51 +08:00
|
|
|
imageSpec: { width: 300, height: 300, quality: 80, format: 'webp', isPreview: true },
|
2026-07-19 12:32:08 +08:00
|
|
|
})
|
|
|
|
|
mediaInfo.thumbnail = thumbTranscode
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetMediaActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
mediaInfo,
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await updateAssetStatusActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
status: 'processed',
|
|
|
|
|
})
|
2026-10-02 20:09:32 +08:00
|
|
|
mediaProcessed = true
|
2026-07-19 12:32:08 +08:00
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await createEmbeddingTaskIfEnabledActivity({
|
2026-07-19 12:32:08 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
teamId: task.teamId,
|
|
|
|
|
projectId: task.projectId,
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await createAutofillTaskIfEnabledActivity({
|
2026-07-28 20:19:10 +08:00
|
|
|
assetId: asset.id,
|
|
|
|
|
teamId: task.teamId,
|
|
|
|
|
projectId: task.projectId,
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-19 15:35:08 +08:00
|
|
|
await completeTask(task.id)
|
2026-07-19 12:32:08 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`transcodeVideoWorkflow failed for task ${task.id}:`, err)
|
2026-10-19 15:35:08 +08:00
|
|
|
await failTask(task.id, err, mediaProcessed ? undefined : task.assetId)
|
2026-07-19 12:32:08 +08:00
|
|
|
throw err
|
|
|
|
|
} finally {
|
2026-10-19 15:35:08 +08:00
|
|
|
await cleanupTmpDir(tmpDir)
|
2026-07-19 12:32:08 +08:00
|
|
|
}
|
|
|
|
|
}
|