mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 14:47:38 +00:00
feat(transcode): add HDR video proxy support and playback (#424)
* feat(transcode): add HDR output proxy support * fix(transcode): auto-detect MoltenVK Vulkan ICD on macOS for libplacebo filters * fix(transcode): split filter in generateSprite to ensure poster uses tone-mapped stream * docs(changelog): update docs/changelog.mdx and ignore in prettier * fix(transcode): respect video rotation metadata to preserve display aspect ratio * fix(webui): render native video with hardware EDR acceleration instead of 2D canvas * fix(webui): show HDR label on mobile video control bar * fix(share): preserve hdr flag on watermarked video transcodes * fix(webui): differentiate HDR download options in video player * fix(transcode): handle Dolby Vision Profile 5 via standard HDR pipeline without libplacebo * fix(transcode): prepend setparams filter to prevent zscale colorspace path error for DV P5 * refactor(transcode): simplify HDR proxies to always produce HDR without SDR setting
This commit is contained in:
@@ -75,6 +75,7 @@ vi.mock('@shumai/core/src/transcode/transcode', async (importOriginal) => {
|
||||
takeScreenshots: vi.fn(),
|
||||
overlayAnnotations: vi.fn(),
|
||||
renderPdfPages: vi.fn(),
|
||||
getAvailableFilters: vi.fn().mockResolvedValue(new Set(['zscale', 'tonemap'])),
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -184,6 +185,7 @@ describe('Transcode Activities', () => {
|
||||
audioChannels: 2,
|
||||
audioSampleRate: 48000,
|
||||
audioBitDepth: 16,
|
||||
rotation: -90,
|
||||
mimeType: 'video/mp4',
|
||||
})
|
||||
|
||||
@@ -197,6 +199,7 @@ describe('Transcode Activities', () => {
|
||||
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith('/tmp/v.mp4')
|
||||
expect(result.duration).toBe(10)
|
||||
expect(result.metadata?.originalWidth).toBe(1920)
|
||||
expect(result.metadata?.rotation).toBe(-90)
|
||||
expect(result.metadata?.videoBitRate).toBe(850000)
|
||||
expect(result.metadata?.videoCodec).toBe('Advanced Video Coding')
|
||||
expect(result.metadata?.audioCodec).toBe('MPEG-4 Audio')
|
||||
@@ -205,7 +208,10 @@ describe('Transcode Activities', () => {
|
||||
expect(result.metadata?.audioBitDepth).toBe(16)
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'video' }]),
|
||||
expect.arrayContaining([
|
||||
{ key: 'file_type', value: 'video' },
|
||||
{ key: 'rotation', value: -90 },
|
||||
]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
@@ -696,6 +702,37 @@ describe('Transcode Activities', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('transcodeVideoActivity appends -hdr.mp4 suffix and forwards HDR metadata', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
|
||||
const result = await transcodeVideoActivity({
|
||||
assetKey: 'files/asset-1/video.mp4',
|
||||
filePath: '/tmp/video.mp4',
|
||||
videoSpec: { resolution: '1080p', width: 1920, height: 1080, hdr: true },
|
||||
duration: 10,
|
||||
originalFps: 30,
|
||||
sourceIsHdr: true,
|
||||
sourceHdrType: 'pq',
|
||||
sourceColorTransfer: 'smpte2084',
|
||||
sourceColorPrimaries: 'bt2020',
|
||||
sourceColorSpace: 'bt2020nc',
|
||||
})
|
||||
|
||||
expect(result.key).toBe('files/asset-1/video-1080p-hdr.mp4')
|
||||
expect(result.hdr).toBe(true)
|
||||
expect(transcodeService.transcodeVideo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
inputFile: '/tmp/video.mp4',
|
||||
hdr: true,
|
||||
sourceIsHdr: true,
|
||||
sourceHdrType: 'pq',
|
||||
sourceColorTransfer: 'smpte2084',
|
||||
sourceColorPrimaries: 'bt2020',
|
||||
sourceColorSpace: 'bt2020nc',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should call transcodeService.takeScreenshots', async () => {
|
||||
vi.mocked(transcodeService.takeScreenshots).mockResolvedValue([
|
||||
{ key: 'screenshots/shot1.webp', timestamp: 1.0 },
|
||||
@@ -873,6 +910,44 @@ describe('Transcode Activities', () => {
|
||||
).rejects.toThrowError(/Sprite\/Poster generation failed/)
|
||||
})
|
||||
|
||||
it('generateSpriteActivity forwards HDR options to transcodeService.generateSprite', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
vi.mocked(transcodeService.generateSprite).mockImplementation(
|
||||
async (_in, spriteOut, posterOut) => {
|
||||
fs.writeFileSync(spriteOut, Buffer.from('fake-sprite'))
|
||||
fs.writeFileSync(posterOut, Buffer.from('fake-poster'))
|
||||
},
|
||||
)
|
||||
|
||||
await generateSpriteActivity({
|
||||
assetKey: 'files/asset-1/video.mp4',
|
||||
filePath: '/tmp/video.mp4',
|
||||
mediaInfo: {
|
||||
duration: 10,
|
||||
metadata: {
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
},
|
||||
} as unknown as PrismaJson.MediaInfo,
|
||||
spriteSpec: { key: 'files/asset-1/sprite.webp', frames: 100, tileX: 10, tileY: 10 },
|
||||
posterSpec: { key: 'files/asset-1/poster.webp' },
|
||||
})
|
||||
|
||||
expect(transcodeService.generateSprite).toHaveBeenCalledWith(
|
||||
'/tmp/video.mp4',
|
||||
expect.any(String),
|
||||
expect.any(String),
|
||||
10,
|
||||
undefined,
|
||||
{
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when takeScreenshotsActivity fails with error', async () => {
|
||||
vi.mocked(transcodeService.takeScreenshots).mockRejectedValue(new Error('FFmpeg failed'))
|
||||
|
||||
@@ -1100,6 +1175,41 @@ 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: '' })
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await extractPosterActivity({
|
||||
assetKey: 'video.mp4',
|
||||
posterSpec: { key: 'files/asset-1/poster.webp' },
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
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')
|
||||
})
|
||||
|
||||
it('should throw non-retryable ApplicationFailure when poster extraction is cancelled with aborted signal', async () => {
|
||||
vi.mocked(s3Service.headObject).mockRejectedValue(new Error('Not found'))
|
||||
const controller = new AbortController()
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { AssetStatus, prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||
import {
|
||||
transcodeService,
|
||||
buildSdrToneMapFilterChain,
|
||||
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'
|
||||
@@ -142,6 +146,13 @@ export async function getMediaInfoActivity(params: {
|
||||
audioChannels: info.audioChannels,
|
||||
audioSampleRate: info.audioSampleRate,
|
||||
audioBitDepth: info.audioBitDepth,
|
||||
colorTransfer: info.colorTransfer,
|
||||
colorPrimaries: info.colorPrimaries,
|
||||
colorSpace: info.colorSpace,
|
||||
isHdr: info.isHdr,
|
||||
hdrType: info.hdrType,
|
||||
dvProfile: info.dvProfile,
|
||||
rotation: info.rotation,
|
||||
format: {},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
@@ -151,6 +162,12 @@ export async function getMediaInfoActivity(params: {
|
||||
{ key: 'bitRate', value: info.bitRate / 1000 },
|
||||
{ key: 'frame_rate', value: info.frameRate },
|
||||
)
|
||||
if (info.rotation !== undefined)
|
||||
metadataUpdates.push({ key: 'rotation', value: info.rotation })
|
||||
if (info.isHdr !== undefined)
|
||||
metadataUpdates.push({ key: 'is_hdr', value: info.isHdr ? 1 : 0 })
|
||||
if (info.colorTransfer)
|
||||
metadataUpdates.push({ key: 'color_transfer', value: info.colorTransfer })
|
||||
if (info.videoCodec) metadataUpdates.push({ key: 'video_codec', value: info.videoCodec })
|
||||
if (info.audioCodec) metadataUpdates.push({ key: 'audio_codec', value: info.audioCodec })
|
||||
if (info.audioChannels !== undefined)
|
||||
@@ -253,6 +270,11 @@ export interface VideoActivityParams {
|
||||
hardwareAcceleration?: PrismaJson.HardwareAcceleration
|
||||
sourceVideoBitrate?: number
|
||||
threads?: number
|
||||
sourceIsHdr?: boolean
|
||||
sourceHdrType?: HdrType
|
||||
sourceColorTransfer?: string
|
||||
sourceColorPrimaries?: string
|
||||
sourceColorSpace?: string
|
||||
}
|
||||
|
||||
export async function transcodeVideoActivity(
|
||||
@@ -261,7 +283,8 @@ export async function transcodeVideoActivity(
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
const stem = stemFromKey(params.assetKey)
|
||||
const res = params.videoSpec.resolution || `${params.videoSpec.height}p`
|
||||
const key = path.posix.join(path.posix.dirname(params.assetKey), `${stem}-${res}.mp4`)
|
||||
const suffix = params.videoSpec.hdr ? `${res}-hdr` : res
|
||||
const key = path.posix.join(path.posix.dirname(params.assetKey), `${stem}-${suffix}.mp4`)
|
||||
|
||||
const signal = getActivityCancellationSignal(params.taskId)
|
||||
if (signal?.aborted) {
|
||||
@@ -279,7 +302,7 @@ export async function transcodeVideoActivity(
|
||||
}
|
||||
|
||||
const tmpDir = path.dirname(params.filePath)
|
||||
const outputFile = path.join(tmpDir, `${stem}-${res}.mp4`)
|
||||
const outputFile = path.join(tmpDir, `${stem}-${suffix}.mp4`)
|
||||
|
||||
try {
|
||||
let targetFps: number | string = params.originalFps || 30
|
||||
@@ -307,6 +330,12 @@ export async function transcodeVideoActivity(
|
||||
sourceVideoBitrate: params.sourceVideoBitrate,
|
||||
threads: params.threads,
|
||||
signal,
|
||||
hdr: params.videoSpec.hdr,
|
||||
sourceIsHdr: params.sourceIsHdr,
|
||||
sourceHdrType: params.sourceHdrType,
|
||||
sourceColorTransfer: params.sourceColorTransfer,
|
||||
sourceColorPrimaries: params.sourceColorPrimaries,
|
||||
sourceColorSpace: params.sourceColorSpace,
|
||||
})
|
||||
|
||||
const stat = fs.statSync(outputFile)
|
||||
@@ -339,7 +368,9 @@ export async function transcodeVideoActivity(
|
||||
lowerMsg.includes('ffprobe') ||
|
||||
lowerMsg.includes('spawn') ||
|
||||
lowerMsg.includes('format') ||
|
||||
lowerMsg.includes('no video stream found')
|
||||
lowerMsg.includes('no video stream found') ||
|
||||
lowerMsg.includes('zscale') ||
|
||||
lowerMsg.includes('tonemap')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Video transcoding failed: ${message}`,
|
||||
@@ -558,6 +589,11 @@ export async function generateSpriteActivity(params: GenerateSpriteActivityParam
|
||||
posterFile,
|
||||
params.mediaInfo.duration,
|
||||
signal,
|
||||
{
|
||||
isHdr: params.mediaInfo.metadata?.isHdr,
|
||||
hdrType: params.mediaInfo.metadata?.hdrType,
|
||||
colorTransfer: params.mediaInfo.metadata?.colorTransfer,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -604,7 +640,9 @@ export async function generateSpriteActivity(params: GenerateSpriteActivityParam
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('ffprobe') ||
|
||||
lowerMsg.includes('spawn') ||
|
||||
lowerMsg.includes('format')
|
||||
lowerMsg.includes('format') ||
|
||||
lowerMsg.includes('zscale') ||
|
||||
lowerMsg.includes('tonemap')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Sprite/Poster generation failed: ${message}`,
|
||||
@@ -857,6 +895,9 @@ export interface ExtractPosterActivityParams {
|
||||
posterSpec: PrismaJson.PosterInfo
|
||||
taskId?: string
|
||||
signal?: AbortSignal
|
||||
isHdr?: boolean
|
||||
hdrType?: HdrType
|
||||
colorTransfer?: string
|
||||
}
|
||||
|
||||
export async function extractPosterActivity(
|
||||
@@ -885,6 +926,17 @@ export async function extractPosterActivity(
|
||||
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']
|
||||
@@ -896,7 +948,7 @@ export async function extractPosterActivity(
|
||||
'-vframes',
|
||||
'1',
|
||||
'-vf',
|
||||
'scale=-2:300:force_original_aspect_ratio=decrease',
|
||||
vf,
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
'-q:v',
|
||||
@@ -938,7 +990,9 @@ export async function extractPosterActivity(
|
||||
lowerMsg.includes('enoent') ||
|
||||
lowerMsg.includes('nosuchkey') ||
|
||||
lowerMsg.includes('ffmpeg') ||
|
||||
lowerMsg.includes('sharp')
|
||||
lowerMsg.includes('sharp') ||
|
||||
lowerMsg.includes('zscale') ||
|
||||
lowerMsg.includes('tonemap')
|
||||
) {
|
||||
throw ApplicationFailure.create({
|
||||
message: `Poster extraction failed: ${message}`,
|
||||
|
||||
@@ -405,6 +405,125 @@ describe('Watermark Activities', () => {
|
||||
|
||||
fs.rmSync(outFilePath, { force: true })
|
||||
})
|
||||
|
||||
it('mirrors both SDR and HDR videoTranscodes preserving HDR flags and -hdr suffix', async () => {
|
||||
const assetKey = 'files/hdr-wm/video.mp4'
|
||||
const storageKey = await prisma.storageKey.create({ data: { key: assetKey } })
|
||||
const asset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'video.mp4',
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
storageKeyId: storageKey.id,
|
||||
mediaType: 'video/mp4',
|
||||
media: {
|
||||
duration: 10,
|
||||
filesize: 0,
|
||||
frames: 300,
|
||||
proxyType: 'video',
|
||||
imageTranscodes: [],
|
||||
videoPreview: { width: 1920, height: 1080 },
|
||||
finishedAt: new Date().toISOString(),
|
||||
original: {
|
||||
key: assetKey,
|
||||
filesizeInBytes: 0,
|
||||
codec: '',
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
bitRate: 1000,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
startTimecode: '00:00:00:00',
|
||||
hasAudio: true,
|
||||
format: {},
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
},
|
||||
videoTranscodes: [
|
||||
{
|
||||
key: 'files/hdr-wm/video-1080p.mp4',
|
||||
resolution: '1080p',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
hdr: false,
|
||||
},
|
||||
{
|
||||
key: 'files/hdr-wm/video-1080p-hdr.mp4',
|
||||
resolution: '1080p',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
hdr: true,
|
||||
},
|
||||
],
|
||||
} as PrismaJson.MediaInfo,
|
||||
},
|
||||
})
|
||||
const config = await seedConfig()
|
||||
|
||||
vi.mocked(transcodeService.getVideoInfo).mockResolvedValue({
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
bitRate: 1000,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
startTimecode: '00:00:00:00',
|
||||
hasAudio: true,
|
||||
mimeType: 'video/mp4',
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
})
|
||||
vi.mocked(s3Service.putObject).mockResolvedValue(undefined as never)
|
||||
|
||||
const sdrOut = path.join('/tmp', `video-watermark-${config.id}-1080p.mp4`)
|
||||
const hdrOut = path.join('/tmp', `video-watermark-${config.id}-1080p-hdr.mp4`)
|
||||
fs.writeFileSync(sdrOut, Buffer.from('fake sdr'))
|
||||
fs.writeFileSync(hdrOut, Buffer.from('fake hdr'))
|
||||
|
||||
const media = await transcodeWatermarkMediaActivity({
|
||||
assetId: asset.id,
|
||||
watermarkConfigId: config.id,
|
||||
})
|
||||
|
||||
expect(media.videoTranscodes).toHaveLength(2)
|
||||
expect(media.videoTranscodes?.[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolution: '1080p',
|
||||
hdr: false,
|
||||
key: expect.stringContaining(`video-watermark-${config.id}-1080p.mp4`),
|
||||
}),
|
||||
)
|
||||
expect(media.videoTranscodes?.[1]).toEqual(
|
||||
expect.objectContaining({
|
||||
resolution: '1080p',
|
||||
hdr: true,
|
||||
key: expect.stringContaining(`video-watermark-${config.id}-1080p-hdr.mp4`),
|
||||
}),
|
||||
)
|
||||
|
||||
expect(transcodeService.transcodeVideo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
outputFile: sdrOut,
|
||||
hdr: false,
|
||||
sourceIsHdr: true,
|
||||
}),
|
||||
)
|
||||
expect(transcodeService.transcodeVideo).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
outputFile: hdrOut,
|
||||
hdr: true,
|
||||
sourceIsHdr: true,
|
||||
}),
|
||||
)
|
||||
|
||||
fs.rmSync(sdrOut, { force: true })
|
||||
fs.rmSync(hdrOut, { force: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe('completeWatermarkFileActivity', () => {
|
||||
|
||||
@@ -181,6 +181,12 @@ export async function transcodeWatermarkMediaActivity(
|
||||
let startTimecode = '00:00:00:00'
|
||||
let hasAudio = false
|
||||
let sourceVideoBitrate: number | undefined
|
||||
let colorTransfer: string | undefined
|
||||
let colorPrimaries: string | undefined
|
||||
let colorSpace: string | undefined
|
||||
let isHdr: boolean | undefined
|
||||
let hdrType: PrismaJson.HdrType | undefined
|
||||
let dvProfile: number | undefined
|
||||
|
||||
if (isVideo) {
|
||||
const info = await transcodeService.getVideoInfo(rawFilePath)
|
||||
@@ -194,6 +200,12 @@ export async function transcodeWatermarkMediaActivity(
|
||||
startTimecode = info.startTimecode || '00:00:00:00'
|
||||
hasAudio = info.hasAudio
|
||||
sourceVideoBitrate = info.videoBitRate || info.bitRate
|
||||
colorTransfer = info.colorTransfer
|
||||
colorPrimaries = info.colorPrimaries
|
||||
colorSpace = info.colorSpace
|
||||
isHdr = info.isHdr
|
||||
hdrType = info.hdrType
|
||||
dvProfile = info.dvProfile
|
||||
} else if (isImage) {
|
||||
const info = await transcodeService.getImageInfo(rawFilePath)
|
||||
originalWidth = info.originalWidth
|
||||
@@ -258,6 +270,12 @@ export async function transcodeWatermarkMediaActivity(
|
||||
totalFrames,
|
||||
startTimecode,
|
||||
hasAudio,
|
||||
colorTransfer: colorTransfer ?? originalMedia?.metadata?.colorTransfer,
|
||||
colorPrimaries: colorPrimaries ?? originalMedia?.metadata?.colorPrimaries,
|
||||
colorSpace: colorSpace ?? originalMedia?.metadata?.colorSpace,
|
||||
isHdr: isHdr ?? originalMedia?.metadata?.isHdr,
|
||||
hdrType: hdrType ?? originalMedia?.metadata?.hdrType,
|
||||
dvProfile: dvProfile ?? originalMedia?.metadata?.dvProfile,
|
||||
format: {},
|
||||
},
|
||||
original: {
|
||||
@@ -320,6 +338,8 @@ export async function transcodeWatermarkMediaActivity(
|
||||
const targetWidth = vt.width || originalWidth
|
||||
const targetHeight = vt.height || originalHeight
|
||||
const resolution = vt.resolution || `${targetHeight}p`
|
||||
const isVtHdr = Boolean(vt.hdr)
|
||||
const suffix = isVtHdr ? `${resolution}-hdr` : resolution
|
||||
|
||||
const svgString = generateWatermarkSvg(config, targetWidth, targetHeight, blockImagesMap)
|
||||
const overlayPngBuffer = await transcodeService.renderSvgToPng(svgString)
|
||||
@@ -327,7 +347,7 @@ export async function transcodeWatermarkMediaActivity(
|
||||
const overlayPngPath = path.join(tmpDir, `watermarkOverlay-${i}.png`)
|
||||
fs.writeFileSync(overlayPngPath, overlayPngBuffer)
|
||||
|
||||
const outFileName = `${stem}-watermark-${params.watermarkConfigId}-${resolution}.mp4`
|
||||
const outFileName = `${stem}-watermark-${params.watermarkConfigId}-${suffix}.mp4`
|
||||
const outFilePath = path.join(tmpDir, outFileName)
|
||||
|
||||
await transcodeService.transcodeVideo({
|
||||
@@ -340,6 +360,12 @@ export async function transcodeWatermarkMediaActivity(
|
||||
hardwareAcceleration,
|
||||
sourceVideoBitrate,
|
||||
threads,
|
||||
hdr: isVtHdr,
|
||||
sourceIsHdr: isHdr ?? originalMedia?.metadata?.isHdr,
|
||||
sourceHdrType: hdrType ?? originalMedia?.metadata?.hdrType,
|
||||
sourceColorTransfer: colorTransfer ?? originalMedia?.metadata?.colorTransfer,
|
||||
sourceColorPrimaries: colorPrimaries ?? originalMedia?.metadata?.colorPrimaries,
|
||||
sourceColorSpace: colorSpace ?? originalMedia?.metadata?.colorSpace,
|
||||
})
|
||||
|
||||
const stat = fs.statSync(outFilePath)
|
||||
@@ -359,6 +385,7 @@ export async function transcodeWatermarkMediaActivity(
|
||||
width: targetWidth,
|
||||
height: targetHeight,
|
||||
resolution,
|
||||
hdr: isVtHdr,
|
||||
})
|
||||
}
|
||||
mediaInfo.filesize = totalSize
|
||||
|
||||
@@ -168,4 +168,58 @@ describe('takeVideoScreenshotsWorkflow', () => {
|
||||
annotations: [],
|
||||
})
|
||||
})
|
||||
|
||||
it('should prioritize SDR proxy over higher-resolution HDR proxy to avoid washed-out annotations', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-screenshot-sdr-prioritized',
|
||||
assetId: 'asset-video-hdr',
|
||||
type: WorkflowTaskType.transcode_screenshot,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
sessionId: null,
|
||||
output: null,
|
||||
payload: {
|
||||
projectId: 'proj-1',
|
||||
screenshot: {
|
||||
start: 2.0,
|
||||
end: 2.0,
|
||||
count: 1,
|
||||
commentTimestamp: 2.0,
|
||||
annotations: [],
|
||||
},
|
||||
},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
heartbeat: null,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
uid: 'task-uid',
|
||||
model: null,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'asset-video-hdr',
|
||||
storageKey: { key: 'raw-hdr.mp4' },
|
||||
mediaType: 'video/mp4',
|
||||
media: {
|
||||
videoTranscodes: [
|
||||
{ key: 'proxy-1080p-hdr.mp4', height: 1080, width: 1920, hdr: true },
|
||||
{ key: 'proxy-720p.mp4', height: 720, width: 1280, hdr: false },
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
mockActivities.takeScreenshotsActivity.mockResolvedValue([
|
||||
{ key: 'files/asset-video-hdr/screenshots/shot1.webp', timestamp: 2.0 },
|
||||
])
|
||||
|
||||
await takeVideoScreenshotsWorkflow(task)
|
||||
|
||||
expect(mockActivities.takeScreenshotsActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
assetKey: 'proxy-720p.mp4',
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -23,7 +23,12 @@ export async function takeVideoScreenshotsWorkflow(task: WorkflowTask): Promise<
|
||||
if (mediaInfo.videoTranscodes && mediaInfo.videoTranscodes.length > 0) {
|
||||
const sorted = [...mediaInfo.videoTranscodes]
|
||||
.filter((t) => t.key)
|
||||
.sort((a, b) => (b.height || 0) - (a.height || 0))
|
||||
.sort((a, b) => {
|
||||
// Prioritize SDR over HDR to avoid washed out screenshots
|
||||
if (!a.hdr && b.hdr) return -1
|
||||
if (a.hdr && !b.hdr) return 1
|
||||
return (b.height || 0) - (a.height || 0)
|
||||
})
|
||||
if (sorted.length > 0 && sorted[0].key) {
|
||||
targetKey = sorted[0].key
|
||||
}
|
||||
|
||||
@@ -497,4 +497,209 @@ describe('transcodeVideoWorkflow', () => {
|
||||
mockActivities.updateAssetMediaActivity.mock.invocationCallOrder[spriteCommitIndex],
|
||||
).toBeLessThan(firstProxyOrder)
|
||||
})
|
||||
|
||||
it('should transcode only HDR proxies and HDR 180p preview when source is HDR', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-hdr',
|
||||
assetId: 'asset-hdr',
|
||||
type: WorkflowTaskType.transcode_video,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
sessionId: null,
|
||||
output: null,
|
||||
payload: {
|
||||
projectId: 'proj-1',
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
heartbeat: null,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
uid: 'task-uid-hdr',
|
||||
model: null,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'asset-hdr',
|
||||
storageKey: { key: 'hdr-video.mp4' },
|
||||
mediaType: 'video/mp4',
|
||||
})
|
||||
|
||||
mockActivities.getMediaInfoActivity.mockResolvedValue({
|
||||
proxyType: 'video',
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
startTimecode: '00:00:00:00',
|
||||
bitRate: 1000,
|
||||
videoBitRate: 850000,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
colorPrimaries: 'bt2020',
|
||||
colorSpace: 'bt2020nc',
|
||||
},
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
})
|
||||
|
||||
mockActivities.transcodeVideoActivity.mockImplementation(async (params) => {
|
||||
return {
|
||||
key: `files/asset-hdr/video-${params.videoSpec.resolution}${params.videoSpec.hdr ? '-hdr' : ''}.mp4`,
|
||||
width: params.videoSpec.width,
|
||||
height: params.videoSpec.height,
|
||||
resolution: params.videoSpec.resolution,
|
||||
hdr: params.videoSpec.hdr,
|
||||
}
|
||||
})
|
||||
|
||||
await transcodeVideoWorkflow(task)
|
||||
|
||||
// Poster extraction must use SDR tone mapping
|
||||
expect(mockActivities.extractPosterActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
isHdr: true,
|
||||
hdrType: 'pq',
|
||||
colorTransfer: 'smpte2084',
|
||||
}),
|
||||
)
|
||||
|
||||
// 180p preview must be generated as HDR (hdr: true)
|
||||
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
videoSpec: expect.objectContaining({
|
||||
resolution: '180p',
|
||||
hdr: true,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
|
||||
// Main resolution (1080p) must be called as HDR (hdr: true)
|
||||
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
videoSpec: expect.objectContaining({
|
||||
resolution: '1080p',
|
||||
hdr: true,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
|
||||
// No calls should have been made with hdr: false for HDR source
|
||||
const calls = mockActivities.transcodeVideoActivity.mock.calls
|
||||
const sdrCalls = calls.filter(
|
||||
(c: unknown[]) => (c[0] as { videoSpec: { hdr?: boolean } }).videoSpec.hdr === false,
|
||||
)
|
||||
expect(sdrCalls).toHaveLength(0)
|
||||
|
||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||
taskId: 'task-hdr',
|
||||
status: WorkflowTaskStatus.completed,
|
||||
})
|
||||
})
|
||||
|
||||
it('should transcode only SDR proxy and SDR 180p preview when source is SDR', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-sdr-source',
|
||||
assetId: 'asset-sdr-source',
|
||||
type: WorkflowTaskType.transcode_video,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
sessionId: null,
|
||||
output: null,
|
||||
payload: {
|
||||
projectId: 'proj-1',
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
heartbeat: null,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
uid: 'task-uid-sdr-source',
|
||||
model: null,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'asset-sdr-source',
|
||||
storageKey: { key: 'sdr-video.mp4' },
|
||||
mediaType: 'video/mp4',
|
||||
})
|
||||
|
||||
mockActivities.getMediaInfoActivity.mockResolvedValue({
|
||||
proxyType: 'video',
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
startTimecode: '00:00:00:00',
|
||||
bitRate: 1000,
|
||||
videoBitRate: 850000,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
isHdr: false,
|
||||
},
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
})
|
||||
|
||||
mockActivities.transcodeVideoActivity.mockImplementation(async (params) => {
|
||||
return {
|
||||
key: `files/asset-sdr-source/video-${params.videoSpec.resolution}.mp4`,
|
||||
width: params.videoSpec.width,
|
||||
height: params.videoSpec.height,
|
||||
resolution: params.videoSpec.resolution,
|
||||
hdr: params.videoSpec.hdr,
|
||||
}
|
||||
})
|
||||
|
||||
await transcodeVideoWorkflow(task)
|
||||
|
||||
// 180p preview must be generated as SDR (hdr: false)
|
||||
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
videoSpec: expect.objectContaining({
|
||||
resolution: '180p',
|
||||
hdr: false,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
|
||||
// Main resolution (1080p) must be generated as SDR (hdr: false)
|
||||
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
videoSpec: expect.objectContaining({
|
||||
resolution: '1080p',
|
||||
hdr: false,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
|
||||
// No calls should have been made with hdr: true for SDR source
|
||||
const calls = mockActivities.transcodeVideoActivity.mock.calls
|
||||
const hdrCalls = calls.filter(
|
||||
(c: unknown[]) => (c[0] as { videoSpec: { hdr?: boolean } }).videoSpec.hdr === true,
|
||||
)
|
||||
expect(hdrCalls).toHaveLength(0)
|
||||
|
||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||
taskId: 'task-sdr-source',
|
||||
status: WorkflowTaskStatus.completed,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -69,6 +69,9 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
assetKey: key,
|
||||
posterSpec,
|
||||
taskId: task.id,
|
||||
isHdr: mediaInfo.metadata?.isHdr,
|
||||
hdrType: mediaInfo.metadata?.hdrType,
|
||||
colorTransfer: mediaInfo.metadata?.colorTransfer,
|
||||
})
|
||||
mediaInfo.poster = posterResult.poster
|
||||
await executeActivity(workerQueue, updateAssetMediaActivity, {
|
||||
@@ -129,10 +132,42 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
metadata.originalHeight,
|
||||
)
|
||||
|
||||
const isHdr = Boolean(metadata.isHdr)
|
||||
|
||||
if (res === '180p') {
|
||||
const videoSpec: PrismaJson.VideoTranscode = {
|
||||
resolution: res,
|
||||
width,
|
||||
height,
|
||||
hdr: isHdr,
|
||||
}
|
||||
|
||||
const videoTranscode = await executeActivity(workerQueue, transcodeVideoActivity, {
|
||||
taskId: task.id,
|
||||
assetKey: key,
|
||||
filePath,
|
||||
videoSpec,
|
||||
duration: metadata.duration,
|
||||
originalFps: metadata.frameRate,
|
||||
hardwareAcceleration: spec.hardwareAcceleration,
|
||||
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
||||
threads: spec.threads,
|
||||
sourceIsHdr: metadata.isHdr,
|
||||
sourceHdrType: metadata.hdrType,
|
||||
sourceColorTransfer: metadata.colorTransfer,
|
||||
sourceColorPrimaries: metadata.colorPrimaries,
|
||||
sourceColorSpace: metadata.colorSpace,
|
||||
})
|
||||
|
||||
mediaInfo.videoPreview = videoTranscode
|
||||
continue
|
||||
}
|
||||
|
||||
const videoSpec: PrismaJson.VideoTranscode = {
|
||||
resolution: res,
|
||||
width,
|
||||
height,
|
||||
hdr: isHdr,
|
||||
}
|
||||
|
||||
const videoTranscode = await executeActivity(workerQueue, transcodeVideoActivity, {
|
||||
@@ -145,13 +180,14 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
hardwareAcceleration: spec.hardwareAcceleration,
|
||||
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
||||
threads: spec.threads,
|
||||
sourceIsHdr: metadata.isHdr,
|
||||
sourceHdrType: metadata.hdrType,
|
||||
sourceColorTransfer: metadata.colorTransfer,
|
||||
sourceColorPrimaries: metadata.colorPrimaries,
|
||||
sourceColorSpace: metadata.colorSpace,
|
||||
})
|
||||
|
||||
if (res === '180p') {
|
||||
mediaInfo.videoPreview = videoTranscode
|
||||
} else {
|
||||
mediaInfo.videoTranscodes.push(videoTranscode)
|
||||
}
|
||||
mediaInfo.videoTranscodes.push(videoTranscode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user