feat(transcode): add hardware-accelerated video transcoding support (#352)

* feat(transcode): add hardware-accelerated video transcoding support

* feat(transcode): use encoder quality modes and source-capped maxrate for video transcode

* fix(transcode): scale max bitrate for downsampled preview framerates

* fix(transcode): propagate videoBitRate in getMediaInfoActivity and update auto description
This commit is contained in:
Yiling-J
2026-08-26 16:28:14 +08:00
committed by GitHub
parent 7b1428bf51
commit 8f55ba1d37
29 changed files with 1272 additions and 11 deletions
@@ -172,6 +172,7 @@ describe('Transcode Activities', () => {
originalHeight: 1080,
duration: 10,
bitRate: 1000,
videoBitRate: 850000,
frameRate: 30,
totalFrames: 300,
hasAudio: true,
@@ -193,6 +194,7 @@ describe('Transcode Activities', () => {
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith('/tmp/v.mp4')
expect(result.duration).toBe(10)
expect(result.metadata?.originalWidth).toBe(1920)
expect(result.metadata?.videoBitRate).toBe(850000)
expect(result.metadata?.videoCodec).toBe('Advanced Video Coding')
expect(result.metadata?.audioCodec).toBe('MPEG-4 Audio')
expect(result.metadata?.audioChannels).toBe(2)
@@ -590,9 +592,17 @@ describe('Transcode Activities', () => {
videoSpec: { resolution: '720p', width: 1280, height: 720 },
duration: 10,
originalFps: 30,
hardwareAcceleration: 'auto',
sourceVideoBitrate: 600_000,
})
expect(transcodeService.transcodeVideo).toHaveBeenCalled()
expect(transcodeService.transcodeVideo).toHaveBeenCalledWith(
expect.objectContaining({
inputFile: '/tmp/v.mp4',
hardwareAcceleration: 'auto',
sourceVideoBitrate: 600_000,
}),
)
})
it('should call transcodeService.takeScreenshots', async () => {
@@ -104,6 +104,7 @@ export async function getMediaInfoActivity(params: {
originalHeight: info.originalHeight,
duration: info.duration,
bitRate: info.bitRate,
videoBitRate: info.videoBitRate,
frameRate: info.frameRate,
totalFrames: info.totalFrames,
startTimecode: info.startTimecode || '00:00:00:00',
@@ -220,6 +221,8 @@ export interface VideoActivityParams {
videoSpec: PrismaJson.VideoTranscode
duration: number
originalFps: number
hardwareAcceleration?: PrismaJson.HardwareAcceleration
sourceVideoBitrate?: number
}
export async function transcodeVideoActivity(
@@ -262,6 +265,8 @@ export async function transcodeVideoActivity(
height: params.videoSpec.height,
frameRate: targetFps || undefined,
disableAudio,
hardwareAcceleration: params.hardwareAcceleration,
sourceVideoBitrate: params.sourceVideoBitrate,
})
const stat = fs.statSync(outputFile)
+7
View File
@@ -22,6 +22,13 @@ export class VideoTranscoder {
return this
}
setHardwareAcceleration(hardwareAcceleration?: PrismaJson.HardwareAcceleration): this {
if (hardwareAcceleration) {
this.spec.hardwareAcceleration = hardwareAcceleration
}
return this
}
withSprite(): this {
this.spec.sprite = true
return this
@@ -154,11 +154,13 @@ describe('transcodeVideoWorkflow', () => {
projectId: 'proj-1',
})
expect(mockActivities.createAutofillTaskIfEnabledActivity).toHaveBeenCalledWith({
assetId: 'asset-1',
teamId: 'team-1',
projectId: 'proj-1',
})
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
expect.objectContaining({
assetKey: 'video.mp4',
filePath: '/tmp/video.mp4',
hardwareAcceleration: undefined,
}),
)
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-1',
@@ -166,6 +168,74 @@ describe('transcodeVideoWorkflow', () => {
})
})
it('should forward hardwareAcceleration to transcodeVideoActivity when specified', async () => {
const task: WorkflowTask = {
id: 'task-hw',
assetId: 'asset-hw',
type: WorkflowTaskType.transcode_video,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
transcode: {
videoStrategy: 'best_match',
hardwareAcceleration: 'auto',
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid-hw',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-hw',
storageKey: { key: 'video.mp4' },
mediaType: 'video/mp4',
})
mockActivities.getMediaInfoActivity.mockResolvedValue({
proxyType: 'video',
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
startTimecode: '00:00:00:00',
bitRate: 1000,
videoBitRate: 850000,
hasAudio: false,
format: {},
},
videoTranscodes: [],
imageTranscodes: [],
})
mockActivities.transcodeVideoActivity.mockResolvedValue({
key: 'v.mp4',
width: 1920,
height: 1080,
})
await transcodeVideoWorkflow(task)
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
expect.objectContaining({
assetKey: 'video.mp4',
filePath: '/tmp/video.mp4',
hardwareAcceleration: 'auto',
sourceVideoBitrate: 850000,
}),
)
})
it('should run audio transcoding workflow successfully', async () => {
const task: WorkflowTask = {
id: 'task-audio',
@@ -85,6 +85,8 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
videoSpec,
duration: metadata.duration,
originalFps: metadata.frameRate,
hardwareAcceleration: spec.hardwareAcceleration,
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
})
if (res === '180p') {