mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 20:28:42 +00:00
feat(transcode): smart poster frame selection and decoupled generation (#428)
* feat(transcode): smart poster frame selection and decoupled generation * fix(transcode): detect MPEG-TS extension from URL pathname for signed remote sources
This commit is contained in:
@@ -63,6 +63,7 @@ vi.mock('@shumai/core/src/transcode/transcode', async (importOriginal) => {
|
||||
transcodeAudio: vi.fn(),
|
||||
transcodeImage: vi.fn(),
|
||||
generateSprite: vi.fn(),
|
||||
generatePoster: vi.fn(),
|
||||
generatePdfSprite: vi.fn().mockResolvedValue({
|
||||
pageCount: 10,
|
||||
originalWidth: 800,
|
||||
@@ -1127,22 +1128,12 @@ describe('Transcode Activities', () => {
|
||||
expect(child_process.execFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should extract poster upfront at t=0 and upload to S3', async () => {
|
||||
it('should extract poster upfront and upload to S3', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(s3Service.resolveInput).mockResolvedValue('https://mock-r2.com/video.mp4')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(file: string, args: string[], optionsOrCb: unknown, maybeCb?: unknown) => {
|
||||
const cb = typeof optionsOrCb === 'function' ? optionsOrCb : maybeCb
|
||||
if (file === 'ffmpeg') {
|
||||
const outPath = args[args.length - 1]
|
||||
fs.writeFileSync(outPath, 'fake-poster-bytes')
|
||||
}
|
||||
if (typeof cb === 'function') {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
},
|
||||
)
|
||||
vi.mocked(transcodeService.generatePoster).mockImplementation(async (_in, posterOut) => {
|
||||
fs.writeFileSync(posterOut, Buffer.from('fake-poster-bytes'))
|
||||
})
|
||||
|
||||
const res = await extractPosterActivity({
|
||||
assetKey: 'video.mp4',
|
||||
@@ -1151,20 +1142,15 @@ describe('Transcode Activities', () => {
|
||||
|
||||
expect(res.poster.key).toBe('files/asset-1/poster.webp')
|
||||
expect(s3Service.resolveInput).toHaveBeenCalledWith('shumai', 'video.mp4')
|
||||
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
expect.arrayContaining([
|
||||
'-ss',
|
||||
'0',
|
||||
'-i',
|
||||
'https://mock-r2.com/video.mp4',
|
||||
'-vframes',
|
||||
'1',
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
]),
|
||||
expect.objectContaining({ signal: undefined }),
|
||||
expect.any(Function),
|
||||
expect(transcodeService.generatePoster).toHaveBeenCalledWith(
|
||||
'https://mock-r2.com/video.mp4',
|
||||
expect.stringContaining('poster.webp'),
|
||||
{
|
||||
isHdr: undefined,
|
||||
hdrType: undefined,
|
||||
colorTransfer: undefined,
|
||||
signal: undefined,
|
||||
},
|
||||
)
|
||||
expect(s3Service.putObject).toHaveBeenCalledWith(
|
||||
'shumai',
|
||||
@@ -1178,21 +1164,9 @@ describe('Transcode Activities', () => {
|
||||
it('extractPosterActivity applies tone mapping filter when isHdr is true', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(s3Service.resolveInput).mockResolvedValue('https://mock-r2.com/video.mp4')
|
||||
let ffmpegArgs: string[] = []
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(file: string, args: string[], optionsOrCb: unknown, maybeCb?: unknown) => {
|
||||
const cb = typeof optionsOrCb === 'function' ? optionsOrCb : maybeCb
|
||||
if (file === 'ffmpeg') {
|
||||
ffmpegArgs = args
|
||||
const outPath = args[args.length - 1]
|
||||
fs.writeFileSync(outPath, 'fake-poster-bytes')
|
||||
}
|
||||
if (typeof cb === 'function') {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
},
|
||||
)
|
||||
vi.mocked(transcodeService.generatePoster).mockImplementation(async (_in, posterOut) => {
|
||||
fs.writeFileSync(posterOut, Buffer.from('fake-poster-bytes'))
|
||||
})
|
||||
|
||||
await extractPosterActivity({
|
||||
assetKey: 'video.mp4',
|
||||
@@ -1202,12 +1176,16 @@ describe('Transcode Activities', () => {
|
||||
colorTransfer: 'smpte2084',
|
||||
})
|
||||
|
||||
const vfIndex = ffmpegArgs.indexOf('-vf')
|
||||
expect(vfIndex).toBeGreaterThan(-1)
|
||||
const vfArg = ffmpegArgs[vfIndex + 1]
|
||||
expect(vfArg).toContain('zscale=tin=smpte2084')
|
||||
expect(vfArg).toContain('tonemap=tonemap=hable')
|
||||
expect(vfArg).toContain('scale=-2:300')
|
||||
expect(transcodeService.generatePoster).toHaveBeenCalledWith(
|
||||
'https://mock-r2.com/video.mp4',
|
||||
expect.stringContaining('poster.webp'),
|
||||
{
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
signal: undefined,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when poster extraction is cancelled with aborted signal', async () => {
|
||||
@@ -1232,19 +1210,10 @@ describe('Transcode Activities', () => {
|
||||
vi.mocked(s3Service.resolveInput).mockResolvedValue('https://mock-r2.com/video.mp4')
|
||||
const controller = new AbortController()
|
||||
|
||||
const abortErr = new Error('The operation was aborted')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(_file: string, _args: string[], optionsOrCb: unknown, maybeCb?: unknown) => {
|
||||
const cb = typeof optionsOrCb === 'function' ? optionsOrCb : maybeCb
|
||||
controller.abort()
|
||||
const abortErr = new Error('The operation was aborted')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(abortErr as any).code = 'ABORT_ERR'
|
||||
if (typeof cb === 'function') {
|
||||
cb(abortErr, { stdout: '', stderr: '' })
|
||||
}
|
||||
},
|
||||
)
|
||||
;(abortErr as any).code = 'ABORT_ERR'
|
||||
vi.mocked(transcodeService.generatePoster).mockRejectedValue(abortErr)
|
||||
|
||||
await expect(
|
||||
extractPosterActivity({
|
||||
@@ -1273,19 +1242,9 @@ describe('Transcode Activities', () => {
|
||||
},
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(file: string, args: string[], optionsOrCb: unknown, maybeCb?: unknown) => {
|
||||
const cb = typeof optionsOrCb === 'function' ? optionsOrCb : maybeCb
|
||||
if (file === 'ffmpeg') {
|
||||
const outPath = args[args.length - 1]
|
||||
fs.writeFileSync(outPath, 'fake-poster-bytes')
|
||||
}
|
||||
if (typeof cb === 'function') {
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
}
|
||||
},
|
||||
)
|
||||
vi.mocked(transcodeService.generatePoster).mockImplementation(async (_in, posterOut) => {
|
||||
fs.writeFileSync(posterOut, Buffer.from('fake-poster-bytes'))
|
||||
})
|
||||
|
||||
await expect(
|
||||
extractPosterActivity({
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import { AssetStatus, prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import {
|
||||
transcodeService,
|
||||
buildSdrToneMapFilterChain,
|
||||
type HdrType,
|
||||
} from '@shumai/core/src/transcode/transcode'
|
||||
import { transcodeService, type HdrType } from '@shumai/core/src/transcode/transcode'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { getDerivedArtifactDirectory, stemFromKey } from '@shumai/core/src/utils/filename'
|
||||
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
|
||||
@@ -924,38 +920,12 @@ export async function extractPosterActivity(
|
||||
|
||||
try {
|
||||
const inputSource = await s3Service.resolveInput(bucket, params.assetKey)
|
||||
const isRemote = inputSource.startsWith('http://') || inputSource.startsWith('https://')
|
||||
|
||||
let vf = 'scale=-2:300:force_original_aspect_ratio=decrease'
|
||||
if (params.isHdr) {
|
||||
const availableFilters = await transcodeService.getAvailableFilters()
|
||||
const tonemap = buildSdrToneMapFilterChain({
|
||||
hdrType: params.hdrType,
|
||||
colorTransfer: params.colorTransfer,
|
||||
availableFilters,
|
||||
})
|
||||
vf = `${tonemap},scale=-2:300:force_original_aspect_ratio=decrease`
|
||||
}
|
||||
|
||||
const args = [
|
||||
...(isRemote
|
||||
? ['-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5']
|
||||
: []),
|
||||
'-ss',
|
||||
'0',
|
||||
'-i',
|
||||
inputSource,
|
||||
'-vframes',
|
||||
'1',
|
||||
'-vf',
|
||||
vf,
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
'-q:v',
|
||||
'75',
|
||||
posterFile,
|
||||
]
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args], { signal })
|
||||
await transcodeService.generatePoster(inputSource, posterFile, {
|
||||
isHdr: params.isHdr,
|
||||
hdrType: params.hdrType,
|
||||
colorTransfer: params.colorTransfer,
|
||||
signal,
|
||||
})
|
||||
|
||||
const posterBuffer = fs.readFileSync(posterFile)
|
||||
await ensureAssetNotPurging(params.assetKey)
|
||||
|
||||
Reference in New Issue
Block a user