mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
fix(transcode): avoid selecting attached picture streams for video transcoding (#443)
* fix(transcode): avoid selecting attached picture streams for video transcoding Filter out attached picture streams (disposition.attached_pic) during video probing and stream selection, matching Immich's default and bitrate selection strategy. Forward explicit primary video and audio stream indices to FFmpeg pipelines to prevent picking embedded cover art or thumbnail streams. * fix(transcode): skip poster and sprite generation for audio-only assets and add multi-stream e2e tests * test(workflow): scope pixel format to h264 stream and use yuvj420p for mjpeg cover art
This commit is contained in:
@@ -8,11 +8,56 @@ import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import { fileURLToPath } from 'url'
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
import { execFile } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
const currentDir = path.dirname(fileURLToPath(import.meta.url))
|
||||
const transcodeWorkflowsPath = path.resolve(currentDir, '../../../apps/transcode/src/workflows.ts')
|
||||
const fixturesDir = path.resolve(currentDir, '../fixtures')
|
||||
|
||||
async function sampleFirstFrameRgb(
|
||||
buffer: Buffer,
|
||||
ext: string = 'mp4',
|
||||
): Promise<{ r: number; g: number; b: number }> {
|
||||
const tmpFile = path.join(
|
||||
os.tmpdir(),
|
||||
`sample-${Date.now()}-${Math.random().toString(36).slice(2)}.${ext}`,
|
||||
)
|
||||
try {
|
||||
fs.writeFileSync(tmpFile, buffer)
|
||||
const proc = await execFileAsync(
|
||||
'ffmpeg',
|
||||
[
|
||||
'-y',
|
||||
'-i',
|
||||
tmpFile,
|
||||
'-vframes',
|
||||
'1',
|
||||
'-s',
|
||||
'1x1',
|
||||
'-f',
|
||||
'rawvideo',
|
||||
'-pix_fmt',
|
||||
'rgb24',
|
||||
'pipe:1',
|
||||
],
|
||||
{ encoding: 'buffer' },
|
||||
)
|
||||
return {
|
||||
r: proc.stdout[0],
|
||||
g: proc.stdout[1],
|
||||
b: proc.stdout[2],
|
||||
}
|
||||
} finally {
|
||||
if (fs.existsSync(tmpFile)) {
|
||||
fs.unlinkSync(tmpFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe.each(['local', 'temporal'] as const)(
|
||||
'Workflow E2E - transcodeVideoWorkflow (executor: %s)',
|
||||
(mode) => {
|
||||
@@ -371,5 +416,700 @@ describe.each(['local', 'temporal'] as const)(
|
||||
})
|
||||
expect(symlinkVariant).toContain('#EXTM3U')
|
||||
}, 60000)
|
||||
|
||||
it('Case 1: should ignore attached picture cover art (Red) and select primary video stream (Green)', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'E2E Cover Art Video Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'E2E Cover Art Video Project', teamId: team.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'projects/e2e/case1/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
mediaType: 'video/mp4',
|
||||
projectId: project.id,
|
||||
storageKeyId: storageKey.id,
|
||||
},
|
||||
})
|
||||
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'e2e-case1-'))
|
||||
const videoPath = path.join(tmpDir, 'case1.mp4')
|
||||
try {
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=green:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=red:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'sine=f=1000:d=2',
|
||||
'-map',
|
||||
'0:v',
|
||||
'-map',
|
||||
'1:v',
|
||||
'-map',
|
||||
'2:a',
|
||||
'-c:v:0',
|
||||
'libx264',
|
||||
'-pix_fmt:v:0',
|
||||
'yuv420p',
|
||||
'-c:v:1',
|
||||
'mjpeg',
|
||||
'-pix_fmt:v:1',
|
||||
'yuvj420p',
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-disposition:v:1',
|
||||
'attached_pic',
|
||||
videoPath,
|
||||
])
|
||||
const mp4Buffer = fs.readFileSync(videoPath)
|
||||
await s3Service.putObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
'projects/e2e/case1/video.mp4',
|
||||
mp4Buffer,
|
||||
mp4Buffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'transcode_video',
|
||||
status: 'pending',
|
||||
assetId: asset.id,
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
sprite: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const completedTask = await workflowService.executeWait(task, 45000)
|
||||
expect(completedTask.status).toBe('completed')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||
|
||||
const mediaInfo = updatedAsset?.media as unknown as {
|
||||
proxyType: string
|
||||
metadata: { videoStreamIndex: number }
|
||||
poster: { key: string }
|
||||
sprite: { key: string }
|
||||
videoTranscodes: { key: string }[]
|
||||
}
|
||||
expect(mediaInfo.proxyType).toBe('video')
|
||||
expect(mediaInfo.metadata.videoStreamIndex).toBe(0)
|
||||
|
||||
// Verify poster is Green (not Red attached pic)
|
||||
expect(mediaInfo.poster?.key).toBeDefined()
|
||||
const posterObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.poster.key,
|
||||
)
|
||||
const posterColor = await sampleFirstFrameRgb(posterObj.buffer, 'webp')
|
||||
expect(posterColor.g).toBeGreaterThan(100)
|
||||
expect(posterColor.r).toBeLessThan(50)
|
||||
expect(posterColor.b).toBeLessThan(50)
|
||||
|
||||
// Verify MP4 proxy is Green
|
||||
expect(mediaInfo.videoTranscodes.length).toBeGreaterThan(0)
|
||||
const videoObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.videoTranscodes[0].key,
|
||||
)
|
||||
const videoColor = await sampleFirstFrameRgb(videoObj.buffer, 'mp4')
|
||||
expect(videoColor.g).toBeGreaterThan(100)
|
||||
expect(videoColor.r).toBeLessThan(50)
|
||||
expect(videoColor.b).toBeLessThan(50)
|
||||
|
||||
// Verify sprite sheet exists
|
||||
expect(mediaInfo.sprite?.key).toBeDefined()
|
||||
await s3Service.headObject('shumai-e2e-test-bucket-transcode', mediaInfo.sprite.key)
|
||||
}, 60000)
|
||||
|
||||
it('Case 2: should select higher bitrate stream (Blue) when both streams have default flag', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'E2E Bitrate Comparison Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'E2E Bitrate Comparison Project', teamId: team.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'projects/e2e/case2/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
mediaType: 'video/mp4',
|
||||
projectId: project.id,
|
||||
storageKeyId: storageKey.id,
|
||||
},
|
||||
})
|
||||
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'e2e-case2-'))
|
||||
const videoPath = path.join(tmpDir, 'case2.mp4')
|
||||
try {
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=red:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=blue:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'sine=f=1000:d=2',
|
||||
'-map',
|
||||
'0:v',
|
||||
'-map',
|
||||
'1:v',
|
||||
'-map',
|
||||
'2:a',
|
||||
'-c:v:0',
|
||||
'libx264',
|
||||
'-b:v:0',
|
||||
'200k',
|
||||
'-minrate:v:0',
|
||||
'200k',
|
||||
'-maxrate:v:0',
|
||||
'200k',
|
||||
'-bufsize:v:0',
|
||||
'200k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:0',
|
||||
'default',
|
||||
'-c:v:1',
|
||||
'libx264',
|
||||
'-b:v:1',
|
||||
'2000k',
|
||||
'-minrate:v:1',
|
||||
'2000k',
|
||||
'-maxrate:v:1',
|
||||
'2000k',
|
||||
'-bufsize:v:1',
|
||||
'2000k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:1',
|
||||
'default',
|
||||
'-c:a',
|
||||
'aac',
|
||||
videoPath,
|
||||
])
|
||||
const mp4Buffer = fs.readFileSync(videoPath)
|
||||
await s3Service.putObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
'projects/e2e/case2/video.mp4',
|
||||
mp4Buffer,
|
||||
mp4Buffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'transcode_video',
|
||||
status: 'pending',
|
||||
assetId: asset.id,
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const completedTask = await workflowService.executeWait(task, 45000)
|
||||
expect(completedTask.status).toBe('completed')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||
|
||||
const mediaInfo = updatedAsset?.media as unknown as {
|
||||
proxyType: string
|
||||
metadata: { videoStreamIndex: number }
|
||||
poster: { key: string }
|
||||
videoTranscodes: { key: string }[]
|
||||
}
|
||||
expect(mediaInfo.proxyType).toBe('video')
|
||||
// Stream 1 (Blue, 2000k) should be selected over Stream 0 (Red, 200k)
|
||||
expect(mediaInfo.metadata.videoStreamIndex).toBe(1)
|
||||
|
||||
const posterObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.poster.key,
|
||||
)
|
||||
const posterColor = await sampleFirstFrameRgb(posterObj.buffer, 'webp')
|
||||
expect(posterColor.b).toBeGreaterThan(100)
|
||||
expect(posterColor.r).toBeLessThan(50)
|
||||
expect(posterColor.g).toBeLessThan(50)
|
||||
|
||||
const videoObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.videoTranscodes[0].key,
|
||||
)
|
||||
const videoColor = await sampleFirstFrameRgb(videoObj.buffer, 'mp4')
|
||||
expect(videoColor.b).toBeGreaterThan(100)
|
||||
expect(videoColor.r).toBeLessThan(50)
|
||||
expect(videoColor.g).toBeLessThan(50)
|
||||
}, 60000)
|
||||
|
||||
it('Case 3: should select stream with default flag (Yellow) over higher bitrate stream without default flag (Blue)', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'E2E Default Flag Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'E2E Default Flag Project', teamId: team.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'projects/e2e/case3/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
mediaType: 'video/mp4',
|
||||
projectId: project.id,
|
||||
storageKeyId: storageKey.id,
|
||||
},
|
||||
})
|
||||
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'e2e-case3-'))
|
||||
const videoPath = path.join(tmpDir, 'case3.mp4')
|
||||
try {
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=yellow:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=blue:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'sine=f=1000:d=2',
|
||||
'-map',
|
||||
'0:v',
|
||||
'-map',
|
||||
'1:v',
|
||||
'-map',
|
||||
'2:a',
|
||||
'-c:v:0',
|
||||
'libx264',
|
||||
'-b:v:0',
|
||||
'200k',
|
||||
'-minrate:v:0',
|
||||
'200k',
|
||||
'-maxrate:v:0',
|
||||
'200k',
|
||||
'-bufsize:v:0',
|
||||
'200k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:0',
|
||||
'default',
|
||||
'-c:v:1',
|
||||
'libx264',
|
||||
'-b:v:1',
|
||||
'2000k',
|
||||
'-minrate:v:1',
|
||||
'2000k',
|
||||
'-maxrate:v:1',
|
||||
'2000k',
|
||||
'-bufsize:v:1',
|
||||
'2000k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:1',
|
||||
'0',
|
||||
'-c:a',
|
||||
'aac',
|
||||
videoPath,
|
||||
])
|
||||
const mp4Buffer = fs.readFileSync(videoPath)
|
||||
await s3Service.putObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
'projects/e2e/case3/video.mp4',
|
||||
mp4Buffer,
|
||||
mp4Buffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'transcode_video',
|
||||
status: 'pending',
|
||||
assetId: asset.id,
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const completedTask = await workflowService.executeWait(task, 45000)
|
||||
expect(completedTask.status).toBe('completed')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||
|
||||
const mediaInfo = updatedAsset?.media as unknown as {
|
||||
proxyType: string
|
||||
metadata: { videoStreamIndex: number }
|
||||
poster: { key: string }
|
||||
videoTranscodes: { key: string }[]
|
||||
}
|
||||
expect(mediaInfo.proxyType).toBe('video')
|
||||
// Stream 0 (Yellow, default: 1) should win over Stream 1 (Blue, 10x higher bitrate but default: 0)
|
||||
expect(mediaInfo.metadata.videoStreamIndex).toBe(0)
|
||||
|
||||
const posterObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.poster.key,
|
||||
)
|
||||
const posterColor = await sampleFirstFrameRgb(posterObj.buffer, 'webp')
|
||||
expect(posterColor.r).toBeGreaterThan(100)
|
||||
expect(posterColor.g).toBeGreaterThan(100)
|
||||
expect(posterColor.b).toBeLessThan(50)
|
||||
|
||||
const videoObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.videoTranscodes[0].key,
|
||||
)
|
||||
const videoColor = await sampleFirstFrameRgb(videoObj.buffer, 'mp4')
|
||||
expect(videoColor.r).toBeGreaterThan(100)
|
||||
expect(videoColor.g).toBeGreaterThan(100)
|
||||
expect(videoColor.b).toBeLessThan(50)
|
||||
}, 60000)
|
||||
|
||||
it('Case 4: should handle audio-only MP4 with cover art by downgrading to audio proxy (catches audio bug)', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: { name: 'E2E Audio Cover Art Team' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'E2E Audio Cover Art Project', teamId: team.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'projects/e2e/case4/audio-cover.mp4' },
|
||||
})
|
||||
// Uploaded as video/mp4 because of .mp4 container, even though it only has audio + cover art
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'audio-cover.mp4',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
mediaType: 'video/mp4',
|
||||
projectId: project.id,
|
||||
storageKeyId: storageKey.id,
|
||||
},
|
||||
})
|
||||
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'e2e-case4-'))
|
||||
const videoPath = path.join(tmpDir, 'case4.mp4')
|
||||
try {
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'sine=f=1000:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=magenta:s=320x240:d=2',
|
||||
'-map',
|
||||
'0:a',
|
||||
'-map',
|
||||
'1:v',
|
||||
'-c:a',
|
||||
'aac',
|
||||
'-c:v',
|
||||
'mjpeg',
|
||||
'-pix_fmt:v:0',
|
||||
'yuvj420p',
|
||||
'-disposition:v:0',
|
||||
'attached_pic',
|
||||
videoPath,
|
||||
])
|
||||
const mp4Buffer = fs.readFileSync(videoPath)
|
||||
await s3Service.putObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
'projects/e2e/case4/audio-cover.mp4',
|
||||
mp4Buffer,
|
||||
mp4Buffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
// Spec requested poster, sprite, videoStrategy as video upload would
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'transcode_video',
|
||||
status: 'pending',
|
||||
assetId: asset.id,
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
sprite: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const completedTask = await workflowService.executeWait(task, 45000)
|
||||
// Must complete without error (previously failed trying to generate poster/sprite for audio)
|
||||
expect(completedTask.status).toBe('completed')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||
|
||||
const mediaInfo = updatedAsset?.media as unknown as {
|
||||
proxyType: string
|
||||
poster?: unknown
|
||||
sprite?: unknown
|
||||
videoTranscodes: { key: string }[]
|
||||
}
|
||||
expect(mediaInfo.proxyType).toBe('audio')
|
||||
expect(mediaInfo.poster).toBeUndefined()
|
||||
expect(mediaInfo.sprite).toBeUndefined()
|
||||
expect(mediaInfo.videoTranscodes.length).toBeGreaterThan(0)
|
||||
expect(mediaInfo.videoTranscodes[0].key).toContain('-audio-proxy.mp4')
|
||||
await s3Service.headObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.videoTranscodes[0].key,
|
||||
)
|
||||
}, 60000)
|
||||
|
||||
it('Case 5: should rank 3 video streams with HLS enabled and select highest bitrate stream (Blue)', async () => {
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
name: 'E2E Multi Stream HLS Team',
|
||||
settings: {
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
hlsEnabled: true,
|
||||
hlsResolutions: ['480p'],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'E2E Multi Stream HLS Project', teamId: team.id },
|
||||
})
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: { key: 'projects/e2e/case5/video.mp4' },
|
||||
})
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
mediaType: 'video/mp4',
|
||||
projectId: project.id,
|
||||
storageKeyId: storageKey.id,
|
||||
},
|
||||
})
|
||||
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'e2e-case5-'))
|
||||
const videoPath = path.join(tmpDir, 'case5.mp4')
|
||||
try {
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=red:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=green:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'color=c=blue:s=320x240:d=2',
|
||||
'-f',
|
||||
'lavfi',
|
||||
'-i',
|
||||
'sine=f=1000:d=2',
|
||||
'-map',
|
||||
'0:v',
|
||||
'-map',
|
||||
'1:v',
|
||||
'-map',
|
||||
'2:v',
|
||||
'-map',
|
||||
'3:a',
|
||||
'-c:v:0',
|
||||
'libx264',
|
||||
'-b:v:0',
|
||||
'100k',
|
||||
'-minrate:v:0',
|
||||
'100k',
|
||||
'-maxrate:v:0',
|
||||
'100k',
|
||||
'-bufsize:v:0',
|
||||
'100k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:0',
|
||||
'default',
|
||||
'-c:v:1',
|
||||
'libx264',
|
||||
'-b:v:1',
|
||||
'500k',
|
||||
'-minrate:v:1',
|
||||
'500k',
|
||||
'-maxrate:v:1',
|
||||
'500k',
|
||||
'-bufsize:v:1',
|
||||
'500k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:1',
|
||||
'default',
|
||||
'-c:v:2',
|
||||
'libx264',
|
||||
'-b:v:2',
|
||||
'1500k',
|
||||
'-minrate:v:2',
|
||||
'1500k',
|
||||
'-maxrate:v:2',
|
||||
'1500k',
|
||||
'-bufsize:v:2',
|
||||
'1500k',
|
||||
'-nal-hrd',
|
||||
'cbr',
|
||||
'-disposition:v:2',
|
||||
'default',
|
||||
'-c:a',
|
||||
'aac',
|
||||
videoPath,
|
||||
])
|
||||
const mp4Buffer = fs.readFileSync(videoPath)
|
||||
await s3Service.putObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
'projects/e2e/case5/video.mp4',
|
||||
mp4Buffer,
|
||||
mp4Buffer.length,
|
||||
'video/mp4',
|
||||
)
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
}
|
||||
|
||||
const task = await prisma.workflowTask.create({
|
||||
data: {
|
||||
type: 'transcode_video',
|
||||
status: 'pending',
|
||||
assetId: asset.id,
|
||||
projectId: project.id,
|
||||
teamId: team.id,
|
||||
payload: {
|
||||
projectId: project.id,
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
hlsEnabled: true,
|
||||
hlsResolutions: ['480p'],
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const completedTask = await workflowService.executeWait(task, 45000)
|
||||
expect(completedTask.status).toBe('completed')
|
||||
|
||||
const updatedAsset = await prisma.asset.findUnique({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
expect(updatedAsset?.status).toBe(AssetStatus.processed)
|
||||
|
||||
const mediaInfo = updatedAsset?.media as unknown as {
|
||||
proxyType: string
|
||||
metadata: { videoStreamIndex: number }
|
||||
poster: { key: string }
|
||||
isHls: boolean
|
||||
hls: { key: string; resolutions: { resolution: string }[] }
|
||||
}
|
||||
expect(mediaInfo.proxyType).toBe('video')
|
||||
// Stream 2 (Blue, 1500k) should be selected
|
||||
expect(mediaInfo.metadata.videoStreamIndex).toBe(2)
|
||||
|
||||
const posterObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.poster.key,
|
||||
)
|
||||
const posterColor = await sampleFirstFrameRgb(posterObj.buffer, 'webp')
|
||||
expect(posterColor.b).toBeGreaterThan(100)
|
||||
expect(posterColor.r).toBeLessThan(50)
|
||||
expect(posterColor.g).toBeLessThan(50)
|
||||
|
||||
expect(mediaInfo.isHls).toBe(true)
|
||||
const masterObj = await s3Service.getObject(
|
||||
'shumai-e2e-test-bucket-transcode',
|
||||
mediaInfo.hls.key,
|
||||
)
|
||||
expect(masterObj.buffer.toString('utf-8')).toContain('480p/index.m3u8')
|
||||
}, 60000)
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user