mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 02:09:22 +00:00
feat(transcode): optimize video sprite generation with seek pool and compositing (#427)
* feat(transcode): optimize video sprite generation with seek pool and compositing * fix(transcode): handle abort signal during and after sharp compositing
This commit is contained in:
@@ -2222,7 +2222,7 @@ describe('TranscodeService', () => {
|
||||
expect(executedArgs).toContain('smpte2084')
|
||||
})
|
||||
|
||||
it('generateSprite uses split=2 filterComplex for HDR inputs', async () => {
|
||||
it('generateSprite uses split=2 filterComplex for small and short HDR inputs (single-pass)', async () => {
|
||||
let executedArgs: string[] = []
|
||||
;(
|
||||
child_process.execFile as unknown as {
|
||||
@@ -2244,9 +2244,11 @@ describe('TranscodeService', () => {
|
||||
return {}
|
||||
})
|
||||
|
||||
const inputPath = path.join(tempDir, 'short.mp4')
|
||||
fs.writeFileSync(inputPath, 'dummy short video content')
|
||||
const spritePath = path.join(tempDir, 'sprite.webp')
|
||||
const posterPath = path.join(tempDir, 'poster.webp')
|
||||
await transcodeService.generateSprite('input.mp4', spritePath, posterPath, 100, undefined, {
|
||||
await transcodeService.generateSprite(inputPath, spritePath, posterPath, 20, undefined, {
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
@@ -2260,7 +2262,7 @@ describe('TranscodeService', () => {
|
||||
expect(filterStr).toContain('[v_thumb]scale=')
|
||||
})
|
||||
|
||||
it('generateSprite uses split=2 filterComplex for SDR inputs', async () => {
|
||||
it('generateSprite uses split=2 filterComplex for small and short SDR inputs (single-pass)', async () => {
|
||||
let executedArgs: string[] = []
|
||||
;(
|
||||
child_process.execFile as unknown as {
|
||||
@@ -2278,9 +2280,11 @@ describe('TranscodeService', () => {
|
||||
return {}
|
||||
})
|
||||
|
||||
const inputPath = path.join(tempDir, 'short.mp4')
|
||||
fs.writeFileSync(inputPath, 'dummy short video content')
|
||||
const spritePath = path.join(tempDir, 'sprite.webp')
|
||||
const posterPath = path.join(tempDir, 'poster.webp')
|
||||
await transcodeService.generateSprite('input.mp4', spritePath, posterPath, 100)
|
||||
await transcodeService.generateSprite(inputPath, spritePath, posterPath, 20)
|
||||
|
||||
const filterIdx = executedArgs.indexOf('-filter_complex')
|
||||
expect(filterIdx).toBeGreaterThan(-1)
|
||||
@@ -2289,5 +2293,178 @@ describe('TranscodeService', () => {
|
||||
expect(filterStr).toContain('[v_sprite]fps=')
|
||||
expect(filterStr).toContain('[v_thumb]scale=')
|
||||
})
|
||||
|
||||
it('generateSprite uses seek-pool for long inputs (>30s) and generates sprite and poster', async () => {
|
||||
const dummyWebp = await sharp({
|
||||
create: { width: 300, height: 168, channels: 3, background: { r: 50, g: 50, b: 50 } },
|
||||
})
|
||||
.webp()
|
||||
.toBuffer()
|
||||
|
||||
const executedCommands: string[][] = []
|
||||
;(
|
||||
child_process.execFile as unknown as {
|
||||
mockImplementation: (
|
||||
fn: (
|
||||
file: string,
|
||||
args: string[],
|
||||
cb: (err: Error | null, res: { stdout: string; stderr: string }) => void,
|
||||
) => unknown,
|
||||
) => void
|
||||
}
|
||||
).mockImplementation((_file, args, cb) => {
|
||||
if (args.includes('-filters')) {
|
||||
cb(null, { stdout: ' ... zscale ... \n ... tonemap ... ', stderr: '' })
|
||||
return {}
|
||||
}
|
||||
executedCommands.push(args)
|
||||
const outPath = args[args.length - 1]
|
||||
if (outPath && outPath.endsWith('.webp')) {
|
||||
fs.writeFileSync(outPath, dummyWebp)
|
||||
}
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
return {}
|
||||
})
|
||||
|
||||
const inputPath = path.join(tempDir, 'long.mp4')
|
||||
fs.writeFileSync(inputPath, 'dummy long video content')
|
||||
const spritePath = path.join(tempDir, 'sprite_pool.webp')
|
||||
const posterPath = path.join(tempDir, 'poster_pool.webp')
|
||||
|
||||
await transcodeService.generateSprite(inputPath, spritePath, posterPath, 120, undefined, {
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
})
|
||||
|
||||
// Poster was extracted at t=0
|
||||
const posterCmd = executedCommands.find((cmd) => cmd[cmd.length - 1] === posterPath)
|
||||
expect(posterCmd).toBeDefined()
|
||||
expect(posterCmd).toContain('-ss')
|
||||
expect(posterCmd).toContain('0')
|
||||
expect(fs.existsSync(posterPath)).toBe(true)
|
||||
|
||||
// 100 seek calls were performed for frames
|
||||
const frameCmds = executedCommands.filter((cmd) => cmd[cmd.length - 1].includes('frame_'))
|
||||
expect(frameCmds.length).toBe(100)
|
||||
// Check pre-scaling before tonemap filter
|
||||
expect(frameCmds[0].some((arg) => arg.includes('scale=300:-2'))).toBe(true)
|
||||
|
||||
// Sprite file was generated by Sharp
|
||||
expect(fs.existsSync(spritePath)).toBe(true)
|
||||
expect(
|
||||
vi.mocked(sharp).mock.calls.some((call) => {
|
||||
const create = (call[0] as { create?: { width: number; height: number } })?.create
|
||||
return create?.width === 8000 && create?.height === 6000
|
||||
}),
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('generateSprite seek-pool resiliently handles frame failure and abort signal', async () => {
|
||||
const dummyWebp = await sharp({
|
||||
create: { width: 300, height: 168, channels: 3, background: { r: 10, g: 20, b: 30 } },
|
||||
})
|
||||
.webp()
|
||||
.toBuffer()
|
||||
|
||||
;(
|
||||
child_process.execFile as unknown as {
|
||||
mockImplementation: (
|
||||
fn: (
|
||||
file: string,
|
||||
args: string[],
|
||||
cb: (err: Error | null, res: { stdout: string; stderr: string }) => void,
|
||||
) => unknown,
|
||||
) => void
|
||||
}
|
||||
).mockImplementation((_file, args, cb) => {
|
||||
const outPath = args[args.length - 1]
|
||||
// Simulate failure on frame 50
|
||||
if (outPath && outPath.includes('frame_050')) {
|
||||
cb(new Error('Simulated frame decode error'), { stdout: '', stderr: 'error' })
|
||||
return {}
|
||||
}
|
||||
if (outPath && outPath.endsWith('.webp')) {
|
||||
fs.writeFileSync(outPath, dummyWebp)
|
||||
}
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
return {}
|
||||
})
|
||||
|
||||
const inputPath = path.join(tempDir, 'long_fail.mp4')
|
||||
fs.writeFileSync(inputPath, 'dummy video')
|
||||
const spritePath = path.join(tempDir, 'sprite_resilient.webp')
|
||||
const posterPath = path.join(tempDir, 'poster_resilient.webp')
|
||||
|
||||
// Should not throw even with frame_050 failure
|
||||
await transcodeService.generateSprite(inputPath, spritePath, posterPath, 60)
|
||||
expect(fs.existsSync(spritePath)).toBe(true)
|
||||
|
||||
// Test abort signal
|
||||
const controller = new AbortController()
|
||||
controller.abort()
|
||||
await expect(
|
||||
transcodeService.generateSprite(inputPath, spritePath, posterPath, 60, controller.signal),
|
||||
).rejects.toThrow('Sprite generation cancelled')
|
||||
})
|
||||
|
||||
it('generateSprite rejects with cancellation error if aborted while Sharp toFile is pending', async () => {
|
||||
const dummyWebp = await sharp({
|
||||
create: { width: 300, height: 168, channels: 3, background: { r: 10, g: 20, b: 30 } },
|
||||
})
|
||||
.webp()
|
||||
.toBuffer()
|
||||
|
||||
const controller = new AbortController()
|
||||
|
||||
;(
|
||||
child_process.execFile as unknown as {
|
||||
mockImplementation: (
|
||||
fn: (
|
||||
file: string,
|
||||
args: string[],
|
||||
optOrCb: unknown,
|
||||
maybeCb?: (err: Error | null, res: { stdout: string; stderr: string }) => void,
|
||||
) => unknown,
|
||||
) => void
|
||||
}
|
||||
).mockImplementation((_file, args, optOrCb, maybeCb) => {
|
||||
const cb = (typeof optOrCb === 'function' ? optOrCb : maybeCb) as (
|
||||
err: Error | null,
|
||||
res: { stdout: string; stderr: string },
|
||||
) => void
|
||||
const outPath = args[args.length - 1]
|
||||
if (outPath && outPath.endsWith('.webp')) {
|
||||
fs.writeFileSync(outPath, dummyWebp)
|
||||
}
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
return {}
|
||||
})
|
||||
|
||||
// Hook into Sharp toFile: trigger abort while toFile is pending
|
||||
const mockSharpInstance = vi.mocked(sharp())
|
||||
mockSharpInstance.toFile.mockImplementationOnce(async (filePath: string) => {
|
||||
controller.abort()
|
||||
fs.writeFileSync(filePath, 'fake-webp-data')
|
||||
return {
|
||||
format: 'webp',
|
||||
size: 14,
|
||||
width: 3000,
|
||||
height: 1680,
|
||||
channels: 4,
|
||||
premultiplied: false,
|
||||
hasAlpha: false,
|
||||
}
|
||||
})
|
||||
|
||||
const inputPath = path.join(tempDir, 'cancel_during_sharp.mp4')
|
||||
fs.writeFileSync(inputPath, 'dummy video')
|
||||
const spritePath = path.join(tempDir, 'sprite_cancel.webp')
|
||||
const posterPath = path.join(tempDir, 'poster_cancel.webp')
|
||||
|
||||
await expect(
|
||||
transcodeService.generateSprite(inputPath, spritePath, posterPath, 60, controller.signal),
|
||||
).rejects.toThrow('Sprite generation cancelled')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user