mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
feat(settings): add FFmpeg threads setting for media processing (#415)
* feat(settings): add FFmpeg threads setting for media processing * style(settings): add bottom padding to media processing page
This commit is contained in:
@@ -2,6 +2,7 @@ import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { teamService } from '@shumai/core/src/team/team'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { TeamSettingsResponse } from '@shumai/dtos'
|
||||
|
||||
describe('TeamService', () => {
|
||||
setupTestDbHooks()
|
||||
@@ -10,6 +11,7 @@ describe('TeamService', () => {
|
||||
const team = await teamService.ensureDefaultTeam()
|
||||
expect(team).toBeDefined()
|
||||
expect(team.name).toBe('Default Team')
|
||||
expect((team.settings as TeamSettingsResponse | null)?.transcode?.threads).toBe(0)
|
||||
})
|
||||
|
||||
it('should create team', async () => {
|
||||
@@ -376,12 +378,13 @@ describe('TeamService', () => {
|
||||
|
||||
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'all')
|
||||
await teamService.updateSettings(team.id, 'transcode.hardwareAcceleration', 'auto')
|
||||
await teamService.updateSettings(team.id, 'transcode.threads', 4)
|
||||
await teamService.updateSettings(team.id, 'appearance.hideAgent', true)
|
||||
|
||||
const finalSettings = await teamService.getSettings(team.id)
|
||||
expect(finalSettings).toEqual({
|
||||
theme: 'dark',
|
||||
transcode: { videoStrategy: 'all', hardwareAcceleration: 'auto' },
|
||||
transcode: { videoStrategy: 'all', hardwareAcceleration: 'auto', threads: 4 },
|
||||
appearance: { hideAgent: true },
|
||||
semanticSearchEnabled: false,
|
||||
})
|
||||
|
||||
@@ -33,7 +33,7 @@ export class TeamService {
|
||||
data: {
|
||||
name: 'Default Team',
|
||||
settings: {
|
||||
transcode: { videoStrategy: 'best_match', hardwareAcceleration: 'off' },
|
||||
transcode: { videoStrategy: 'best_match', hardwareAcceleration: 'off', threads: 0 },
|
||||
},
|
||||
sandbox: { create: {} },
|
||||
},
|
||||
@@ -332,6 +332,12 @@ export class TeamService {
|
||||
TeamSettingsResponse['transcode']
|
||||
>['hardwareAcceleration']
|
||||
delete settings['transcode.hardwareAcceleration']
|
||||
} else if (key === 'transcode.threads') {
|
||||
if (!settings.transcode) {
|
||||
settings.transcode = {}
|
||||
}
|
||||
settings.transcode.threads = Number(value)
|
||||
delete settings['transcode.threads']
|
||||
} else if (key === 'appearance.hideAgent') {
|
||||
if (!settings.appearance) {
|
||||
settings.appearance = {}
|
||||
|
||||
@@ -614,6 +614,76 @@ describe('TranscodeService', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('transcodeAudio should pass -threads when threads > 0', async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
})
|
||||
|
||||
await transcodeService.transcodeAudio({
|
||||
inputFile: 'input.wav',
|
||||
outputFile: 'output.mp4',
|
||||
bitrate: '128k',
|
||||
threads: 4,
|
||||
})
|
||||
|
||||
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
[
|
||||
'-y',
|
||||
'-loglevel',
|
||||
'warning',
|
||||
'-i',
|
||||
'input.wav',
|
||||
'-vn',
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-b:a',
|
||||
'128k',
|
||||
'-ac',
|
||||
'2',
|
||||
'-threads',
|
||||
'4',
|
||||
'output.mp4',
|
||||
],
|
||||
expect.any(Function),
|
||||
)
|
||||
})
|
||||
|
||||
it('transcodeAudio should omit -threads when threads is 0', async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
})
|
||||
|
||||
await transcodeService.transcodeAudio({
|
||||
inputFile: 'input.wav',
|
||||
outputFile: 'output.mp4',
|
||||
bitrate: '128k',
|
||||
threads: 0,
|
||||
})
|
||||
|
||||
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
[
|
||||
'-y',
|
||||
'-loglevel',
|
||||
'warning',
|
||||
'-i',
|
||||
'input.wav',
|
||||
'-vn',
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-b:a',
|
||||
'128k',
|
||||
'-ac',
|
||||
'2',
|
||||
'output.mp4',
|
||||
],
|
||||
expect.any(Function),
|
||||
)
|
||||
})
|
||||
|
||||
it('should generate PDF sprite sheet and poster using pdftoppm, ffmpeg, and sharp', async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
@@ -1638,5 +1708,73 @@ describe('TranscodeService', () => {
|
||||
expect.any(Function),
|
||||
)
|
||||
})
|
||||
|
||||
it('transcodeVideo with threads > 0 should pass -threads to ffmpeg', async () => {
|
||||
vi.mocked(execFile).mockImplementation(
|
||||
(
|
||||
_cmd: unknown,
|
||||
_args: unknown,
|
||||
callback: unknown,
|
||||
): ReturnType<typeof child_process.execFile> => {
|
||||
const cb = callback as (
|
||||
err: Error | null,
|
||||
result: { stdout: string; stderr: string },
|
||||
) => void
|
||||
if (typeof cb === 'function') {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
return {} as ReturnType<typeof child_process.execFile>
|
||||
},
|
||||
)
|
||||
|
||||
const outputFile = path.join(tempDir, 'out_threads.mp4')
|
||||
await transcodeService.transcodeVideo({
|
||||
inputFile: 'input.mp4',
|
||||
outputFile,
|
||||
width: 1280,
|
||||
height: 720,
|
||||
threads: 6,
|
||||
})
|
||||
|
||||
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
expect.arrayContaining(['-threads', '6']),
|
||||
expect.any(Function),
|
||||
)
|
||||
})
|
||||
|
||||
it('transcodeVideo with threads 0 or undefined should omit -threads', async () => {
|
||||
vi.mocked(execFile).mockImplementation(
|
||||
(
|
||||
_cmd: unknown,
|
||||
_args: unknown,
|
||||
callback: unknown,
|
||||
): ReturnType<typeof child_process.execFile> => {
|
||||
const cb = callback as (
|
||||
err: Error | null,
|
||||
result: { stdout: string; stderr: string },
|
||||
) => void
|
||||
if (typeof cb === 'function') {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
return {} as ReturnType<typeof child_process.execFile>
|
||||
},
|
||||
)
|
||||
|
||||
const outputFile = path.join(tempDir, 'out_threads_0.mp4')
|
||||
await transcodeService.transcodeVideo({
|
||||
inputFile: 'input.mp4',
|
||||
outputFile,
|
||||
width: 1280,
|
||||
height: 720,
|
||||
threads: 0,
|
||||
})
|
||||
|
||||
expect(child_process.execFile).not.toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
expect.arrayContaining(['-threads']),
|
||||
expect.any(Function),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -116,6 +116,7 @@ export interface TranscodeVideoParams {
|
||||
hardwareAcceleration?: 'off' | 'auto'
|
||||
videoBitrate?: string
|
||||
sourceVideoBitrate?: number
|
||||
threads?: number
|
||||
}
|
||||
|
||||
export interface EncoderConfig {
|
||||
@@ -627,6 +628,10 @@ export class TranscodeService {
|
||||
args.push('-c:a', 'aac', '-b:a', '128k')
|
||||
}
|
||||
|
||||
if (params.threads && params.threads > 0) {
|
||||
args.push('-threads', params.threads.toString())
|
||||
}
|
||||
|
||||
args.push('-movflags', '+faststart', '-max_muxing_queue_size', '1024', params.outputFile)
|
||||
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
@@ -1084,20 +1089,14 @@ export class TranscodeService {
|
||||
inputFile: string
|
||||
outputFile: string
|
||||
bitrate?: string
|
||||
threads?: number
|
||||
}): Promise<void> {
|
||||
const bitrate = params.bitrate || '128k'
|
||||
const args = [
|
||||
'-i',
|
||||
params.inputFile,
|
||||
'-vn',
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-b:a',
|
||||
bitrate,
|
||||
'-ac',
|
||||
'2',
|
||||
params.outputFile,
|
||||
]
|
||||
const args = ['-i', params.inputFile, '-vn', '-c:a', 'aac', '-b:a', bitrate, '-ac', '2']
|
||||
if (params.threads && params.threads > 0) {
|
||||
args.push('-threads', params.threads.toString())
|
||||
}
|
||||
args.push(params.outputFile)
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
}
|
||||
|
||||
|
||||
@@ -195,6 +195,58 @@ describe('UploadService', () => {
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
hardwareAcceleration: 'off',
|
||||
threads: 0,
|
||||
sprite: true,
|
||||
poster: true,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('should pass custom transcode.threads from team settings to transcode task', async () => {
|
||||
await prisma.team.update({
|
||||
where: { id: teamId },
|
||||
data: {
|
||||
settings: {
|
||||
transcode: {
|
||||
videoStrategy: 'all',
|
||||
hardwareAcceleration: 'auto',
|
||||
threads: 12,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const task = await prisma.task.create({
|
||||
data: { creatorId: userId, total: 1, uploaded: 0, type: 'upload' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video-custom-threads.mp4',
|
||||
type: AssetType.file,
|
||||
project: { connect: { id: projectId } },
|
||||
parent: { connect: { id: parentId } },
|
||||
status: AssetStatus.uploading,
|
||||
storageKey: {
|
||||
connectOrCreate: {
|
||||
where: { key: 'test-key-threads' },
|
||||
create: { key: 'test-key-threads' },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await uploadService.confirmFileUpload(userId, task.id, { fileId: asset.id })
|
||||
|
||||
const workflowTask = await prisma.workflowTask.findFirst({
|
||||
where: { assetId: asset.id, type: WorkflowTaskType.transcode_video },
|
||||
})
|
||||
expect(workflowTask).toBeDefined()
|
||||
expect(workflowTask?.payload).toEqual({
|
||||
projectId: projectId,
|
||||
transcode: {
|
||||
videoStrategy: 'all',
|
||||
hardwareAcceleration: 'auto',
|
||||
threads: 12,
|
||||
sprite: true,
|
||||
poster: true,
|
||||
},
|
||||
|
||||
@@ -331,6 +331,7 @@ export class UploadService {
|
||||
})
|
||||
} else {
|
||||
const settings = team.settings as PrismaJson.Settings | null
|
||||
const threads = settings?.transcode?.threads ?? 0
|
||||
if (isVideo) {
|
||||
const strategy = settings?.transcode?.videoStrategy || 'best_match'
|
||||
const hardwareAcceleration = settings?.transcode?.hardwareAcceleration || 'off'
|
||||
@@ -338,12 +339,15 @@ export class UploadService {
|
||||
await new VideoTranscoder(tx as any, asset.id, team.id, projectId)
|
||||
.setStrategy(strategy)
|
||||
.setHardwareAcceleration(hardwareAcceleration)
|
||||
.setThreads(threads)
|
||||
.withSprite()
|
||||
.withPoster()
|
||||
.submit()
|
||||
} else if (isAudio) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await new VideoTranscoder(tx as any, asset.id, team.id, projectId).submit()
|
||||
await new VideoTranscoder(tx as any, asset.id, team.id, projectId)
|
||||
.setThreads(threads)
|
||||
.submit()
|
||||
} else if (isImage) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await new ImageTranscoder(tx as any, asset.id, team.id, projectId).withThumbnail().submit()
|
||||
|
||||
Reference in New Issue
Block a user