mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
feat(deploy): implement KEDA ScaledJob and scale-to-zero On-Demand transcoding
This commit is contained in:
@@ -218,6 +218,40 @@ describe('Transcode Activities', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should resolve presigned URL when assetKey is provided without filePath for video', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'remote-video.mp4', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
|
||||
vi.spyOn(s3Service, 'resolveInput').mockResolvedValueOnce(
|
||||
'http://s3.local/shumai/remote-video.mp4?presigned=true',
|
||||
)
|
||||
vi.mocked(transcodeService.getVideoInfo).mockResolvedValueOnce({
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 15,
|
||||
bitRate: 1000000,
|
||||
videoBitRate: 800000,
|
||||
frameRate: 30,
|
||||
totalFrames: 450,
|
||||
hasAudio: false,
|
||||
mimeType: 'video/mp4',
|
||||
})
|
||||
|
||||
const result = await getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
assetKey: 'remote-video.mp4',
|
||||
proxyType: 'video',
|
||||
mediaType: 'video/mp4',
|
||||
})
|
||||
|
||||
expect(s3Service.resolveInput).toHaveBeenCalledWith(expect.any(String), 'remote-video.mp4')
|
||||
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith(
|
||||
'http://s3.local/shumai/remote-video.mp4?presigned=true',
|
||||
)
|
||||
expect(result.duration).toBe(15)
|
||||
})
|
||||
|
||||
it('should downgrade proxyType to audio when getVideoInfo throws No video stream found but audio exists', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'audio-with-art.mp4', type: 'file', status: 'uploaded' },
|
||||
|
||||
@@ -94,19 +94,43 @@ async function ensureAssetNotPurging(assetKey: string): Promise<void> {
|
||||
}
|
||||
|
||||
export const getMediaInfoActivity = defineStep(async function getMediaInfoActivity(params: {
|
||||
filePath: string
|
||||
filePath?: string
|
||||
assetKey?: string
|
||||
assetId: string
|
||||
proxyType?: 'image' | 'video' | 'audio' | 'pdf' | null
|
||||
mediaType?: string
|
||||
}): Promise<PrismaJson.MediaInfo> {
|
||||
let tempLocalDir: string | undefined
|
||||
try {
|
||||
const proxyType =
|
||||
params.proxyType || getProxyType(params.mediaType, params.filePath) || undefined
|
||||
params.proxyType ||
|
||||
getProxyType(params.mediaType, params.filePath || params.assetKey) ||
|
||||
undefined
|
||||
const isVideo = proxyType === 'video'
|
||||
const isImage = proxyType === 'image'
|
||||
const isAudio = proxyType === 'audio'
|
||||
const isPdf = proxyType === 'pdf'
|
||||
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
let mediaSource = params.filePath
|
||||
if (!mediaSource) {
|
||||
if (params.assetKey) {
|
||||
if (isVideo || isAudio) {
|
||||
mediaSource = await s3Service.resolveInput(bucket, params.assetKey)
|
||||
} else {
|
||||
tempLocalDir = transcodeService.createTempDir('mediainfo-')
|
||||
const ext = path.extname(params.assetKey) || (isImage ? '.jpg' : isPdf ? '.pdf' : '')
|
||||
const localTarget = path.join(tempLocalDir, `input${ext}`)
|
||||
mediaSource = await ensureLocalMediaFile(params.assetKey, localTarget)
|
||||
}
|
||||
} else {
|
||||
throw ApplicationFailure.create({
|
||||
message: 'Neither filePath nor assetKey provided to getMediaInfoActivity',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const fileType = isVideo
|
||||
? 'video'
|
||||
: isAudio
|
||||
@@ -145,11 +169,11 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
||||
let isDowngradedToAudio = false
|
||||
let videoInfo: Awaited<ReturnType<typeof transcodeService.getVideoInfo>> | null = null
|
||||
try {
|
||||
videoInfo = await transcodeService.getVideoInfo(params.filePath)
|
||||
videoInfo = await transcodeService.getVideoInfo(mediaSource)
|
||||
} catch (err) {
|
||||
if (err instanceof Error && err.message.includes('No video stream found')) {
|
||||
try {
|
||||
const audioInfo = await transcodeService.getAudioInfo(params.filePath)
|
||||
const audioInfo = await transcodeService.getAudioInfo(mediaSource)
|
||||
isDowngradedToAudio = true
|
||||
mediaInfo.proxyType = 'audio'
|
||||
mediaInfo.duration = audioInfo.duration
|
||||
@@ -247,7 +271,7 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
||||
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
|
||||
}
|
||||
} else if (isAudio) {
|
||||
const info = await transcodeService.getAudioInfo(params.filePath)
|
||||
const info = await transcodeService.getAudioInfo(mediaSource)
|
||||
mediaInfo.duration = info.duration
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: 0,
|
||||
@@ -278,7 +302,7 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
||||
if (info.audioBitDepth !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
|
||||
} else if (isImage) {
|
||||
const info = await transcodeService.getImageInfo(params.filePath)
|
||||
const info = await transcodeService.getImageInfo(mediaSource)
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
@@ -295,7 +319,7 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
)
|
||||
} else if (isPdf) {
|
||||
const info = await transcodeService.getPdfInfo(params.filePath)
|
||||
const info = await transcodeService.getPdfInfo(mediaSource)
|
||||
mediaInfo.frames = info.totalFrames
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
@@ -328,6 +352,14 @@ export const getMediaInfoActivity = defineStep(async function getMediaInfoActivi
|
||||
nonRetryable: true,
|
||||
cause: err instanceof Error ? err : undefined,
|
||||
})
|
||||
} finally {
|
||||
if (tempLocalDir) {
|
||||
try {
|
||||
fs.rmSync(tempLocalDir, { recursive: true, force: true })
|
||||
} catch {
|
||||
// ignore cleanup error
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -156,6 +156,16 @@ describe('transcodeVideoWorkflow', () => {
|
||||
}),
|
||||
}),
|
||||
})
|
||||
expect(mockActivities.getMediaInfoActivity).toHaveBeenCalledWith({
|
||||
assetKey: 'video.mp4',
|
||||
assetId: 'asset-1',
|
||||
proxyType: 'video',
|
||||
mediaType: 'video/mp4',
|
||||
})
|
||||
const getMediaInfoOrder = mockActivities.getMediaInfoActivity.mock.invocationCallOrder[0]
|
||||
const downloadOrder = mockActivities.downloadMediaToTmpActivity.mock.invocationCallOrder[0]
|
||||
expect(getMediaInfoOrder).toBeLessThan(downloadOrder)
|
||||
|
||||
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-1',
|
||||
status: AssetStatus.processed,
|
||||
|
||||
@@ -35,16 +35,10 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
|
||||
const { asset, key } = await fetchAssetWithKey(task.assetId)
|
||||
|
||||
const download = await 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 getMediaInfoActivity({
|
||||
filePath,
|
||||
assetKey: key,
|
||||
assetId: asset.id,
|
||||
proxyType,
|
||||
mediaType: asset.mediaType || '',
|
||||
@@ -80,6 +74,12 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
})
|
||||
}
|
||||
|
||||
const download = await downloadMediaToTmpActivity({
|
||||
assetKey: key,
|
||||
})
|
||||
const { filePath } = download
|
||||
tmpDir = download.tmpDir
|
||||
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user