Files
shumai/packages/transcode/src/workflows/transcode-video.ts
T
Cameron HarrisandClaude Opus 5.5 dd57fb9ed2 feat(transcode): decode and scale on the GPU for hardware transcodes (VA-API) (#449)
* feat(transcode): decode and scale on the GPU for VA-API transcodes

When the selected encoder is h264_vaapi, decode with -hwaccel vaapi and
scale with scale_vaapi so frames stay in GPU memory, instead of decoding
and scaling on the CPU and hwupload-ing before the encoder. On a Haswell
iGPU this makes 4K proxy/HLS renditions 3-5x faster.

Fallback order: GPU decode + VA-API encode -> software decode + VA-API
encode (previous behavior) -> libx264. Watermark overlays, HDR sources
and rotated sources skip GPU decode: ffmpeg cannot auto-insert its
rotation filter on hardware frames, so rotated input would otherwise come
out sideways instead of failing. SHUMAI_VAAPI_HW_DECODE=false disables it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(transcode): document VA-API GPU decoding and SHUMAI_VAAPI_HW_DECODE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(transcode): generic hardware decode config, stall watchdog, reuse probed rotation

Follow-ups from comparing with Immich and Jellyfin:

- Replace the VA-API-specific helpers with a per-encoder HW_DECODE_CONFIGS
  table (input args, output args, GPU scale filter) and build the MP4 and
  HLS input/filter graph in one shared method so the paths cannot drift.
  Only h264_vaapi has an entry; other backends can be added once tested on
  real hardware. The opt-out is now SHUMAI_HW_DECODE=false.
- Kill a GPU-decode attempt with SIGKILL when its output stops growing for
  SHUMAI_HW_DECODE_STALL_TIMEOUT (default 120s) and fall back, since some
  VA-API driver failures hang ffmpeg instead of exiting (immich#31933).
- Add -noautoscale on the GPU-decode path so a mid-stream decoder reinit
  (e.g. 1080 -> 1088) cannot auto-insert a software scaler into the GPU
  graph (jellyfin#17133).
- Pass the rotation already probed into MediaMetadata through the workflow
  and activities as sourceRotation; ffprobe only runs when it is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 12:00:01 +08:00

296 lines
9.5 KiB
TypeScript

import type { WorkflowTask } from '@shumai/db'
import '@shumai/db/src/prisma-json-types'
import { executeActivity, getActivities } from '@shumai/workflow-core'
import {
getWorkerQueueAndStartTask,
fetchAssetWithKey,
completeTask,
failTask,
cleanupTmpDir,
} from './common'
import {
getTargetVideoResolutions,
getTargetHlsResolutions,
resolutionToDimensions,
} from './transcode-utils'
export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void> {
let tmpDir: string | undefined
let workerQueue = ''
let mediaProcessed = false
try {
workerQueue = await getWorkerQueueAndStartTask(task)
const {
updateAssetStatusActivity,
getMediaInfoActivity,
transcodeVideoActivity,
transcodeHlsActivity,
transcodeAudioActivity,
transcodeImageActivity,
extractPosterActivity,
generateSpriteActivity,
updateAssetMediaActivity,
downloadMediaToTmpActivity,
createEmbeddingTaskIfEnabledActivity,
createAutofillTaskIfEnabledActivity,
} = getActivities()
await executeActivity(workerQueue, updateAssetStatusActivity, {
assetId: task.assetId,
status: 'processing',
})
const { asset, key } = await fetchAssetWithKey(workerQueue, task.assetId)
const download = await executeActivity(workerQueue, downloadMediaToTmpActivity, {
assetKey: key,
})
const { filePath } = download
tmpDir = download.tmpDir
const spec = task.payload?.transcode || {}
const proxyType = asset.mediaType?.startsWith('audio/') ? 'audio' : 'video'
const mediaInfo = await executeActivity(workerQueue, getMediaInfoActivity, {
filePath,
assetId: asset.id,
proxyType,
mediaType: asset.mediaType || '',
})
mediaInfo.original = {
key,
filesizeInBytes: 0,
codec: '',
}
const isVideo = mediaInfo.proxyType === 'video'
if (spec.poster && isVideo) {
const lastSlashIndex = key.lastIndexOf('/')
const assetDir = lastSlashIndex === -1 ? '' : key.substring(0, lastSlashIndex)
const posterSpec: PrismaJson.PosterInfo = {
key: assetDir ? `${assetDir}/poster.webp` : 'poster.webp',
}
const posterResult = await executeActivity(workerQueue, extractPosterActivity, {
assetKey: key,
posterSpec,
taskId: task.id,
isHdr: mediaInfo.metadata?.isHdr,
hdrType: mediaInfo.metadata?.hdrType,
colorTransfer: mediaInfo.metadata?.colorTransfer,
streamIndex: mediaInfo.metadata?.videoStreamIndex,
})
mediaInfo.poster = posterResult.poster
await executeActivity(workerQueue, updateAssetMediaActivity, {
assetId: asset.id,
mediaInfo,
})
}
// 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.
if ((spec.sprite || (spec.poster && !mediaInfo.poster)) && isVideo) {
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',
}
const spriteResult = await executeActivity(workerQueue, generateSpriteActivity, {
taskId: task.id,
assetKey: key,
filePath,
spriteSpec,
posterSpec,
mediaInfo,
})
if (spec.sprite) {
mediaInfo.sprite = spriteResult.sprite
}
if (!mediaInfo.poster) {
mediaInfo.poster = spriteResult.poster
}
await executeActivity(workerQueue, updateAssetMediaActivity, {
assetId: asset.id,
mediaInfo,
})
}
const metadata = mediaInfo.metadata
if (isVideo && metadata) {
const videoResolutions = getTargetVideoResolutions(
spec.videoStrategy || 'best_match',
metadata.originalWidth,
metadata.originalHeight,
spec.videoResolutions,
)
for (const res of videoResolutions) {
const [width, height] = resolutionToDimensions(
res,
metadata.originalWidth,
metadata.originalHeight,
)
const isHdr = Boolean(metadata.isHdr)
if (res === '180p') {
const videoSpec: PrismaJson.VideoTranscode = {
resolution: res,
width,
height,
hdr: isHdr,
}
const videoTranscode = await executeActivity(workerQueue, transcodeVideoActivity, {
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,
sourceRotation: metadata.rotation ?? 0,
streamIndex: metadata.videoStreamIndex,
audioStreamIndex: metadata.audioStreamIndex,
})
mediaInfo.videoPreview = videoTranscode
continue
}
const videoSpec: PrismaJson.VideoTranscode = {
resolution: res,
width,
height,
hdr: isHdr,
}
const videoTranscode = await executeActivity(workerQueue, transcodeVideoActivity, {
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,
// rotation is undefined when the probed source has none
sourceRotation: metadata.rotation ?? 0,
streamIndex: metadata.videoStreamIndex,
audioStreamIndex: metadata.audioStreamIndex,
})
mediaInfo.videoTranscodes.push(videoTranscode)
}
if (spec.hlsEnabled) {
const hlsLadders = getTargetHlsResolutions(
spec.hlsResolutions,
metadata.originalWidth,
metadata.originalHeight,
)
if (hlsLadders.length > 0) {
const hlsResult = await executeActivity(workerQueue, transcodeHlsActivity, {
taskId: task.id,
assetKey: key,
filePath,
ladders: hlsLadders,
originalWidth: metadata.originalWidth,
originalHeight: metadata.originalHeight,
originalFps: metadata.frameRate,
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,
sourceRotation: metadata.rotation ?? 0,
streamIndex: metadata.videoStreamIndex,
audioStreamIndex: metadata.audioStreamIndex,
})
mediaInfo.isHls = true
mediaInfo.hls = hlsResult
}
}
}
const isAudio = mediaInfo.proxyType === 'audio'
if (isAudio && metadata) {
const audioTranscode = await executeActivity(workerQueue, transcodeAudioActivity, {
taskId: task.id,
assetKey: key,
filePath,
threads: spec.threads,
audioStreamIndex: metadata.audioStreamIndex,
})
mediaInfo.videoTranscodes.push(audioTranscode)
}
if (spec.thumbnail && isVideo) {
const thumbTranscode = await executeActivity(workerQueue, transcodeImageActivity, {
assetKey: key,
filePath,
imageSpec: { width: 300, height: 300, quality: 80, format: 'webp', isPreview: true },
})
mediaInfo.thumbnail = thumbTranscode
}
await executeActivity(workerQueue, updateAssetMediaActivity, {
assetId: asset.id,
mediaInfo,
})
await executeActivity(workerQueue, updateAssetStatusActivity, {
assetId: asset.id,
status: 'processed',
})
mediaProcessed = true
await executeActivity(workerQueue, createEmbeddingTaskIfEnabledActivity, {
assetId: asset.id,
teamId: task.teamId,
projectId: task.projectId,
})
await executeActivity(workerQueue, createAutofillTaskIfEnabledActivity, {
assetId: asset.id,
teamId: task.teamId,
projectId: task.projectId,
})
await completeTask(workerQueue, task.id)
} catch (err) {
console.error(`transcodeVideoWorkflow failed for task ${task.id}:`, err)
await failTask(workerQueue, task.id, err, mediaProcessed ? undefined : task.assetId)
throw err
} finally {
await cleanupTmpDir(workerQueue, tmpDir)
}
}