feat(transcode): add HLS live streaming support (#436)

* feat(transcode): add HLS live streaming support

* fix(hls): resolve version stacks for public HLS and add workflow E2E test
This commit is contained in:
Yiling-J
2026-09-30 16:31:40 +08:00
committed by GitHub
parent 5fe22ac52c
commit 123a8cb5f4
35 changed files with 2210 additions and 83 deletions
+43
View File
@@ -774,4 +774,47 @@ describe('file api', () => {
mockGetProjectIds.mockRestore()
})
describe('GET /files/:fileId/m3u8 routes', () => {
it('returns master playlist with correct content-type', async () => {
const mockMaster = vi
.spyOn(assetService, 'getHlsMasterPlaylist')
.mockResolvedValue('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n')
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/asset-1/m3u8/master.m3u8')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
const text = await res.text()
expect(text).toContain('#EXTM3U')
expect(text).toContain('1080p/index.m3u8')
expect(mockMaster).toHaveBeenCalledWith({ assetId: 'asset-1' })
expect(authzService.hasPermission).toHaveBeenCalledWith({
user: { id: 'user1', name: 'Test User' },
permission: Permission.Read,
type: 'asset',
id: 'asset-1',
})
mockMaster.mockRestore()
})
it('returns variant playlist with rewritten URLs', async () => {
const mockVariant = vi
.spyOn(assetService, 'getHlsVariantPlaylist')
.mockResolvedValue(
'#EXTM3U\n#EXT-X-MAP:URI="http://presigned/init.mp4"\nhttp://presigned/seg0.m4s\n',
)
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
const res = await app.request('/files/asset-1/m3u8/1080p/index.m3u8')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
const text = await res.text()
expect(text).toContain('http://presigned/init.mp4')
expect(mockVariant).toHaveBeenCalledWith({ assetId: 'asset-1', resolution: '1080p' })
mockVariant.mockRestore()
})
})
})
+65
View File
@@ -480,5 +480,70 @@ const route = new Hono<{ Variables: { user: User } }>()
return c.json({ url })
},
)
.get('/files/:fileId/m3u8', async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: fileId,
})
const playlist = await assetService.getHlsMasterPlaylist({ assetId: fileId })
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
})
.get('/files/:fileId/m3u8/master.m3u8', async (c) => {
const fileId = c.req.param('fileId')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: fileId,
})
const playlist = await assetService.getHlsMasterPlaylist({ assetId: fileId })
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
})
.get('/files/:fileId/m3u8/:resolution', async (c) => {
const fileId = c.req.param('fileId')
let resolution = c.req.param('resolution')
if (resolution.endsWith('.m3u8')) {
resolution = resolution.replace(/\.m3u8$/, '')
}
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: fileId,
})
const playlist = await assetService.getHlsVariantPlaylist({ assetId: fileId, resolution })
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
})
.get('/files/:fileId/m3u8/:resolution/index.m3u8', async (c) => {
const fileId = c.req.param('fileId')
const resolution = c.req.param('resolution')
const user = c.get('user')
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: fileId,
})
const playlist = await assetService.getHlsVariantPlaylist({ assetId: fileId, resolution })
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
})
export default route
+129 -6
View File
@@ -38,6 +38,8 @@ import type { AssetInfo } from '@shumai/dtos'
async function applyWatermarkToAssetMedia(
asset: AssetInfo,
watermarkMedia: PrismaJson.MediaInfo | null,
shareId?: string,
password?: string,
): Promise<AssetInfo> {
if (!asset.media) return asset
if (asset.proxyType !== 'video' && asset.proxyType !== 'image') return asset
@@ -74,6 +76,19 @@ async function applyWatermarkToAssetMedia(
)
: []
if (watermarkMedia?.isHls && watermarkMedia.hls?.key && shareId) {
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
updatedMedia.isHls = true
updatedMedia.hls = {
key: watermarkMedia.hls.key,
url: `/api/shares/${shareId}/files/${asset.id}/m3u8/master.m3u8${pwQuery}`,
resolutions: watermarkMedia.hls.resolutions,
}
} else if (watermarkMedia) {
updatedMedia.isHls = false
delete updatedMedia.hls
}
return {
...asset,
media: updatedMedia,
@@ -170,7 +185,8 @@ const route = app
zValidator('query', sharedChildrenRequestSchema),
async (c) => {
const folderId = c.req.param('folderId')
const password = c.req.header('x-share-password')
const password =
c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
const req = c.req.valid('query')
try {
@@ -197,7 +213,7 @@ const route = app
// Resolve symlink targets once and reuse for the media lookup, so each
// item costs a single query instead of two.
const targetIds = await Promise.all(
res.data.map((item) => assetService.resolveTargetAssetId(item.id)),
res.data.map((item) => assetService.resolveLatestVersionId(item.id)),
)
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
targetIds,
@@ -206,9 +222,21 @@ const route = app
res.data = await Promise.all(
res.data.map(async (item, index) =>
applyWatermarkToAssetMedia(item, wfMap.get(targetIds[index]) ?? null),
applyWatermarkToAssetMedia(
item,
wfMap.get(targetIds[index]) ?? null,
shareLink.id,
password,
),
),
)
} else if (res.data.length > 0) {
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
for (const item of res.data) {
if (item.media?.isHls && item.media.hls?.key) {
item.media.hls.url = `/api/shares/${shareLink.id}/files/${item.id}/m3u8/master.m3u8${pwQuery}`
}
}
}
return c.json(res)
@@ -219,7 +247,7 @@ const route = app
)
.get('/shares/:shareId/files/:fileId', async (c) => {
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password')
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
try {
const shareLink = await shareService.verifyPublicAccess(fileId, password)
@@ -232,12 +260,20 @@ const route = app
shareLink.watermarkConfigId &&
(asset.proxyType === 'video' || asset.proxyType === 'image')
) {
const targetAssetId = await assetService.resolveTargetAssetId(fileId)
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
const wfMap = await watermarkService.getCompletedWatermarkMediaMap(
[targetAssetId],
shareLink.watermarkConfigId,
)
asset = await applyWatermarkToAssetMedia(asset, wfMap.get(targetAssetId) ?? null)
asset = await applyWatermarkToAssetMedia(
asset,
wfMap.get(targetAssetId) ?? null,
shareLink.id,
password,
)
} else if (asset.media?.isHls && asset.media.hls?.key) {
const pwQuery = password ? `?p=${encodeURIComponent(password)}` : ''
asset.media.hls.url = `/api/shares/${shareLink.id}/files/${asset.id}/m3u8/master.m3u8${pwQuery}`
}
return c.json(asset)
@@ -360,5 +396,92 @@ const route = app
}
},
)
.get('/shares/:shareId/files/:fileId/m3u8', async (c) => {
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
try {
const shareLink = await shareService.verifyPublicAccess(fileId, password)
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
let playlist = await assetService.getHlsMasterPlaylist({
assetId: targetAssetId,
watermarkConfigId: shareLink.watermarkConfigId,
})
if (password) {
playlist = playlist.replace(
/([0-9]+p\/index\.m3u8)/g,
`$1?p=${encodeURIComponent(password)}`,
)
}
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
} catch (err) {
return handlePublicShareError(c, err)
}
})
.get('/shares/:shareId/files/:fileId/m3u8/master.m3u8', async (c) => {
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
try {
const shareLink = await shareService.verifyPublicAccess(fileId, password)
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
let playlist = await assetService.getHlsMasterPlaylist({
assetId: targetAssetId,
watermarkConfigId: shareLink.watermarkConfigId,
})
if (password) {
playlist = playlist.replace(
/([0-9]+p\/index\.m3u8)/g,
`$1?p=${encodeURIComponent(password)}`,
)
}
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
} catch (err) {
return handlePublicShareError(c, err)
}
})
.get('/shares/:shareId/files/:fileId/m3u8/:resolution', async (c) => {
const fileId = c.req.param('fileId')
let resolution = c.req.param('resolution')
if (resolution.endsWith('.m3u8')) {
resolution = resolution.replace(/\.m3u8$/, '')
}
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
try {
const shareLink = await shareService.verifyPublicAccess(fileId, password)
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
const playlist = await assetService.getHlsVariantPlaylist({
assetId: targetAssetId,
resolution,
watermarkConfigId: shareLink.watermarkConfigId,
})
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
} catch (err) {
return handlePublicShareError(c, err)
}
})
.get('/shares/:shareId/files/:fileId/m3u8/:resolution/index.m3u8', async (c) => {
const fileId = c.req.param('fileId')
const resolution = c.req.param('resolution')
const password = c.req.header('x-share-password') || c.req.query('password') || c.req.query('p')
try {
const shareLink = await shareService.verifyPublicAccess(fileId, password)
const targetAssetId = await assetService.resolveLatestVersionId(fileId)
const playlist = await assetService.getHlsVariantPlaylist({
assetId: targetAssetId,
resolution,
watermarkConfigId: shareLink.watermarkConfigId,
})
c.header('Content-Type', 'application/vnd.apple.mpegurl')
return c.body(playlist)
} catch (err) {
return handlePublicShareError(c, err)
}
})
export default route
+77
View File
@@ -866,4 +866,81 @@ describe('Share API', () => {
expect(getDownloadUrlSpy).not.toHaveBeenCalled()
})
})
describe('GET /shares/:shareId/files/:fileId/m3u8 routes', () => {
test('returns master playlist with query password preserved in variant URLs', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
id: 'share1',
watermarkConfigId: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.spyOn(assetService, 'getHlsMasterPlaylist').mockResolvedValue(
'#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n',
)
const res = await app.request('/shares/share1/files/file1/m3u8/master.m3u8?p=secret123')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
const text = await res.text()
expect(text).toContain('1080p/index.m3u8?p=secret123')
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('file1', 'secret123')
expect(assetService.getHlsMasterPlaylist).toHaveBeenCalledWith({
assetId: 'file1',
watermarkConfigId: null,
})
})
test('returns variant playlist with rewritten segment URLs', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
id: 'share1',
watermarkConfigId: 'wm1',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.spyOn(assetService, 'getHlsVariantPlaylist').mockResolvedValue(
'#EXTM3U\n#EXT-X-MAP:URI="http://presigned/init.mp4"\nhttp://presigned/seg0.m4s\n',
)
const res = await app.request('/shares/share1/files/file1/m3u8/1080p/index.m3u8')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('application/vnd.apple.mpegurl')
const text = await res.text()
expect(text).toContain('http://presigned/init.mp4')
expect(assetService.getHlsVariantPlaylist).toHaveBeenCalledWith({
assetId: 'file1',
resolution: '1080p',
watermarkConfigId: 'wm1',
})
})
test('returns 401 when password is invalid', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockRejectedValue(
new ShareLinkPasswordInvalidError('Invalid password'),
)
const res = await app.request('/shares/share1/files/file1/m3u8/master.m3u8?p=wrong')
expect(res.status).toBe(401)
const body = await res.json()
expect(body.error).toBe('Unauthorized')
})
test('resolves latest version id when fileId is a symlink to a version stack', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
id: 'share1',
watermarkConfigId: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.spyOn(assetService, 'resolveLatestVersionId').mockResolvedValue('child-version-file-id')
vi.spyOn(assetService, 'getHlsMasterPlaylist').mockResolvedValue(
'#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n',
)
const res = await app.request('/shares/share1/files/symlink1/m3u8/master.m3u8')
expect(res.status).toBe(200)
expect(assetService.resolveLatestVersionId).toHaveBeenCalledWith('symlink1')
expect(assetService.getHlsMasterPlaylist).toHaveBeenCalledWith({
assetId: 'child-version-file-id',
watermarkConfigId: null,
})
})
})
})
+108
View File
@@ -16,6 +16,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
s3Service: {
presign: vi.fn().mockResolvedValue('http://mock-s3-url'),
putObject: vi.fn().mockResolvedValue(undefined),
getObject: vi.fn().mockResolvedValue({ buffer: Buffer.from('') }),
deleteObject: vi.fn().mockResolvedValue(1),
deletePrefix: vi.fn().mockResolvedValue(1),
copyObject: vi.fn().mockResolvedValue(undefined),
@@ -5251,4 +5252,111 @@ describe('AssetService — natural sort by name', () => {
expect(restored?.status).toBe(AssetStatus.processed)
})
})
describe('HLS playlists', () => {
const setupHlsAssets = async () => {
const team = await prisma.team.create({ data: { name: 'hls-team-' + Date.now() } })
const project = await prisma.project.create({
data: { name: 'hls-proj-' + Date.now(), teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'root',
type: AssetType.folder,
projectId: project.id,
status: AssetStatus.processed,
},
})
return { team, project, rootFolder }
}
it('getHlsMasterPlaylist throws 404 if asset is not HLS', async () => {
const { project, rootFolder } = await setupHlsAssets()
const file = await prisma.asset.create({
data: {
name: 'regular.mp4',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.processed,
media: {
isHls: false,
} as PrismaJson.MediaInfo,
},
})
await expect(assetService.getHlsMasterPlaylist({ assetId: file.id })).rejects.toThrow(
'HLS playlist not found',
)
})
it('getHlsMasterPlaylist returns playlist string when asset is HLS', async () => {
const { project, rootFolder } = await setupHlsAssets()
const file = await prisma.asset.create({
data: {
name: 'hls-video.mp4',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.processed,
media: {
isHls: true,
hls: {
key: 'files/asset/hls/master.m3u8',
resolutions: [
{ resolution: '1080p', width: 1920, height: 1080 },
{ resolution: '720p', width: 1280, height: 720 },
],
},
} as PrismaJson.MediaInfo,
},
})
const masterContent = '#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=4628000\n1080p/index.m3u8\n'
vi.mocked(s3Service.getObject).mockResolvedValueOnce({
buffer: Buffer.from(masterContent),
} as unknown as Awaited<ReturnType<typeof s3Service.getObject>>)
const res = await assetService.getHlsMasterPlaylist({ assetId: file.id })
expect(res).toBe(masterContent)
expect(s3Service.getObject).toHaveBeenCalledWith(
expect.any(String),
'files/asset/hls/master.m3u8',
)
})
it('getHlsVariantPlaylist fetches and rewrites segment URLs', async () => {
const { project, rootFolder } = await setupHlsAssets()
const file = await prisma.asset.create({
data: {
name: 'hls-video.mp4',
type: AssetType.file,
projectId: project.id,
parentId: rootFolder.id,
status: AssetStatus.processed,
media: {
isHls: true,
hls: {
key: 'files/asset/hls/master.m3u8',
resolutions: [{ resolution: '1080p', width: 1920, height: 1080 }],
},
} as PrismaJson.MediaInfo,
},
})
const sampleVariant = '#EXTM3U\n#EXT-X-MAP:URI="init.mp4"\nsegment_000.m4s\n'
vi.mocked(s3Service.getObject).mockResolvedValueOnce({
buffer: Buffer.from(sampleVariant),
} as unknown as Awaited<ReturnType<typeof s3Service.getObject>>)
vi.mocked(s3Service.presign).mockResolvedValue('http://mock-s3-url')
const variant = await assetService.getHlsVariantPlaylist({
assetId: file.id,
resolution: '1080p',
})
expect(variant).toBeDefined()
expect(variant).toContain('#EXT-X-MAP:URI="http://mock-s3-url"')
expect(variant).toContain('http://mock-s3-url')
})
})
})
+93
View File
@@ -57,6 +57,7 @@ import { metadataService } from '@shumai/core/src/metadata/metadata'
import { uploadService } from '@shumai/core/src/upload/upload'
import path from 'path'
import { stemFromKey } from '@shumai/core/src/utils/filename'
import { rewriteM3u8WithPresignedUrls } from '@shumai/core/src/transcode/transcode'
export const assetInclude = {
creator: true,
@@ -2386,6 +2387,9 @@ export class AssetService {
'GET',
)
}
if (media && media.isHls && media.hls?.key) {
media.hls.url = `/api/files/${latestVersion.id}/m3u8/master.m3u8`
}
const key = latestVersion.storageKey?.key
if (key) {
@@ -3196,6 +3200,95 @@ export class AssetService {
req,
)
}
async getHlsMasterPlaylist(params: {
assetId: string
watermarkConfigId?: string | null
}): Promise<string> {
const bucket = process.env.S3_BUCKET || 'shumai'
const targetAssetId = await this.resolveLatestVersionId(params.assetId)
if (params.watermarkConfigId) {
const wf = await this.prismaClient.watermarkFile.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_watermarkConfigId: {
assetId: targetAssetId,
watermarkConfigId: params.watermarkConfigId,
},
},
})
const media = wf?.media as PrismaJson.MediaInfo | null
if (!media?.isHls || !media.hls?.key) {
throw new HTTPException(404, { message: 'HLS playlist not found' })
}
const obj = await s3Service.getObject(bucket, media.hls.key)
return obj.buffer.toString('utf-8')
}
const asset = await this.prismaClient.asset.findUnique({
where: { id: targetAssetId },
})
if (!asset) {
throw new HTTPException(404, { message: 'Asset not found' })
}
const media = asset.media as PrismaJson.MediaInfo | null
if (!media?.isHls || !media.hls?.key) {
throw new HTTPException(404, { message: 'HLS playlist not found' })
}
const obj = await s3Service.getObject(bucket, media.hls.key)
return obj.buffer.toString('utf-8')
}
async getHlsVariantPlaylist(params: {
assetId: string
resolution: string
watermarkConfigId?: string | null
}): Promise<string> {
const bucket = process.env.S3_BUCKET || 'shumai'
if (!/^\d+p$/.test(params.resolution)) {
throw new HTTPException(400, { message: 'Invalid resolution format' })
}
const targetAssetId = await this.resolveLatestVersionId(params.assetId)
let media: PrismaJson.MediaInfo | null
if (params.watermarkConfigId) {
const wf = await this.prismaClient.watermarkFile.findUnique({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
assetId_watermarkConfigId: {
assetId: targetAssetId,
watermarkConfigId: params.watermarkConfigId,
},
},
})
media = (wf?.media as PrismaJson.MediaInfo | null) ?? null
} else {
const asset = await this.prismaClient.asset.findUnique({
where: { id: targetAssetId },
})
if (!asset) {
throw new HTTPException(404, { message: 'Asset not found' })
}
media = (asset.media as PrismaJson.MediaInfo | null) ?? null
}
if (!media?.isHls || !media.hls?.key) {
throw new HTTPException(404, { message: 'HLS playlist not found' })
}
const matchedRes = media.hls.resolutions?.find((r) => r.resolution === params.resolution)
const variantKey =
matchedRes?.key ||
path.posix.join(path.posix.dirname(media.hls.key), params.resolution, 'index.m3u8')
const obj = await s3Service.getObject(bucket, variantKey)
const rawM3u8 = obj.buffer.toString('utf-8')
const s3Prefix = path.posix.dirname(variantKey)
return await rewriteM3u8WithPresignedUrls(rawM3u8, s3Prefix, bucket)
}
}
export const assetService = new AssetService()
+12
View File
@@ -371,6 +371,8 @@ describe('TeamService', () => {
videoStrategy: 'best_match',
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
hlsResolutions: ['480p', '720p', '1080p'],
},
appearance: { hideAgent: false },
})
@@ -385,6 +387,8 @@ describe('TeamService', () => {
videoStrategy: 'best_match',
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
hlsResolutions: ['480p', '720p', '1080p'],
},
appearance: { hideAgent: false },
})
@@ -392,6 +396,12 @@ describe('TeamService', () => {
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'all')
await teamService.updateSettings(team.id, 'transcode.hardwareAcceleration', 'auto')
await teamService.updateSettings(team.id, 'transcode.threads', 4)
await teamService.updateSettings(team.id, 'transcode.hlsEnabled', true)
await teamService.updateSettings(team.id, 'transcode.hlsResolutions', [
'720p',
'1080p',
'2160p',
])
await teamService.updateSettings(team.id, 'appearance.hideAgent', true)
const finalSettings = await teamService.getSettings(team.id)
@@ -401,6 +411,8 @@ describe('TeamService', () => {
videoStrategy: 'all',
hardwareAcceleration: 'auto',
threads: 4,
hlsEnabled: true,
hlsResolutions: ['720p', '1080p', '2160p'],
},
appearance: { hideAgent: true },
semanticSearchEnabled: false,
+23
View File
@@ -314,6 +314,15 @@ export class TeamService {
videoStrategy: 'best_match',
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
hlsResolutions: ['480p', '720p', '1080p'],
}
} else {
if (settings.transcode.hlsEnabled === undefined) {
settings.transcode.hlsEnabled = false
}
if (!settings.transcode.hlsResolutions) {
settings.transcode.hlsResolutions = ['480p', '720p', '1080p']
}
}
@@ -354,6 +363,20 @@ export class TeamService {
}
settings.transcode.threads = Number(value)
delete settings['transcode.threads']
} else if (key === 'transcode.hlsEnabled') {
if (!settings.transcode) {
settings.transcode = {}
}
settings.transcode.hlsEnabled = Boolean(value)
delete settings['transcode.hlsEnabled']
} else if (key === 'transcode.hlsResolutions') {
if (!settings.transcode) {
settings.transcode = {}
}
settings.transcode.hlsResolutions = value as NonNullable<
TeamSettingsResponse['transcode']
>['hlsResolutions']
delete settings['transcode.hlsResolutions']
} else if (key === 'appearance.hideAgent') {
if (!settings.appearance) {
settings.appearance = {}
@@ -10,6 +10,8 @@ import {
buildSdrToneMapFilterChain,
getVaapiDevice,
parseBitrateKbps,
buildHlsMasterPlaylist,
rewriteM3u8WithPresignedUrls,
} from './transcode'
import { logger } from '@shumai/core/src/logger'
import { s3Service } from '@shumai/core/src/s3/s3'
@@ -27,6 +29,9 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
downloadToFile: vi.fn(),
putObject: vi.fn(),
resolveInput: vi.fn().mockImplementation(async (_bucket, key) => `http://mock-storage/${key}`),
presign: vi
.fn()
.mockImplementation(async (_bucket, key) => `https://presigned.example.com/${key}`),
},
}))
@@ -3376,4 +3381,106 @@ describe('TranscodeService', () => {
).rejects.toThrow('Poster generation cancelled')
})
})
describe('HLS utilities', () => {
it('builds master playlist correctly with bandwidth and codecs', () => {
const playlist = buildHlsMasterPlaylist([
{ resolution: '1080p', width: 1920, height: 1080, bitrateBps: 4_500_000, isHdr: false },
{ resolution: '720p', width: 1280, height: 720, bitrateBps: 2_500_000, isHdr: false },
{ resolution: '480p', width: 854, height: 480, bitrateBps: 1_200_000, isHdr: false },
])
expect(playlist).toContain('#EXTM3U')
expect(playlist).toContain('#EXT-X-VERSION:7')
expect(playlist).toContain('#EXT-X-INDEPENDENT-SEGMENTS')
expect(playlist).toContain('RESOLUTION=1920x1080')
expect(playlist).toContain('BANDWIDTH=4628000')
expect(playlist).toContain('1080p/index.m3u8')
expect(playlist).toContain('RESOLUTION=1280x720')
expect(playlist).toContain('720p/index.m3u8')
expect(playlist).toContain('RESOLUTION=854x480')
expect(playlist).toContain('480p/index.m3u8')
})
it('builds master playlist with HDR codec when isHdr is true', () => {
const playlist = buildHlsMasterPlaylist([
{ resolution: '1080p', width: 1920, height: 1080, bitrateBps: 4_500_000, isHdr: true },
])
expect(playlist).toContain('CODECS="avc1.640028,mp4a.40.2"')
})
it('rewrites variant m3u8 playlist with presigned S3 URLs', async () => {
const sampleM3u8 = [
'#EXTM3U',
'#EXT-X-VERSION:7',
'#EXT-X-TARGETDURATION:4',
'#EXT-X-MEDIA-SEQUENCE:0',
'#EXT-X-PLAYLIST-TYPE:VOD',
'#EXT-X-MAP:URI="init.mp4"',
'#EXTINF:4.000000,',
'segment_000.m4s',
'#EXTINF:4.000000,',
'segment_001.m4s',
'#EXT-X-ENDLIST',
].join('\n')
const rewritten = await rewriteM3u8WithPresignedUrls(
sampleM3u8,
'files/asset1/hls/1080p',
'shumai',
)
expect(rewritten).toContain(
'#EXT-X-MAP:URI="https://presigned.example.com/files/asset1/hls/1080p/init.mp4"',
)
expect(rewritten).toContain(
'https://presigned.example.com/files/asset1/hls/1080p/segment_000.m4s',
)
expect(rewritten).toContain(
'https://presigned.example.com/files/asset1/hls/1080p/segment_001.m4s',
)
expect(rewritten).toContain('#EXTINF:4.000000,')
expect(rewritten).toContain('#EXT-X-ENDLIST')
})
it('executes transcodeHlsRendition with correct fMP4 CMAF parameters', async () => {
let executedArgs: string[] = []
// child_process.execFile mock signature
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(child_process.execFile as any).mockImplementation(
(
_file: string,
args: string[],
cb: (err: unknown, result: { stdout: string; stderr: string }) => void,
) => {
executedArgs = args
cb(null, { stdout: '', stderr: '' })
},
)
const outputDir = path.join(tempDir, 'hls_output')
fs.mkdirSync(outputDir, { recursive: true })
await transcodeService.transcodeHlsRendition({
inputFile: 'input.mp4',
outputDir,
width: 1920,
height: 1080,
frameRate: 30,
hardwareAcceleration: 'off',
threads: 4,
})
expect(executedArgs).toContain('-f')
expect(executedArgs).toContain('hls')
expect(executedArgs).toContain('-hls_segment_type')
expect(executedArgs).toContain('fmp4')
expect(executedArgs).toContain('-hls_fmp4_init_filename')
expect(executedArgs).toContain('init.mp4')
expect(executedArgs).toContain('-flags')
expect(executedArgs).toContain('+cgop')
expect(executedArgs).toContain('-threads')
expect(executedArgs).toContain('4')
})
})
})
+376
View File
@@ -146,6 +146,28 @@ export interface TranscodeVideoParams {
sourceColorSpace?: string
}
export interface TranscodeHlsRenditionParams {
inputFile: string
outputDir: string
width: number
height: number
frameRate?: number | string
disableAudio?: boolean
overlayFile?: string
hardwareAcceleration?: 'off' | 'auto'
videoBitrate?: string
sourceVideoBitrate?: number
threads?: number
signal?: AbortSignal
hdr?: boolean
sourceIsHdr?: boolean
sourceHdrType?: HdrType
sourceColorTransfer?: string
sourceColorPrimaries?: string
sourceColorSpace?: string
segmentDuration?: number
}
export interface EncoderConfig {
name: string
presetArgs: string[]
@@ -241,9 +263,11 @@ export function getDefaultBitrateBps(height: number, width?: number): number {
const effectiveHeight = shortSide > 0 ? shortSide : height
if (effectiveHeight >= 2160 || longSide >= 3840) return 12_000_000
if (effectiveHeight >= 1440 || longSide >= 2560) return 8_000_000
if (effectiveHeight >= 1080 || longSide >= 1920) return 4_500_000
if (effectiveHeight >= 720 || longSide >= 1280) return 2_500_000
if (effectiveHeight >= 540 || longSide >= 960) return 1_200_000
if (effectiveHeight >= 480 || longSide >= 854) return 1_400_000
if (effectiveHeight >= 360 || longSide >= 640) return 800_000
return 100_000
}
@@ -1131,6 +1155,301 @@ export class TranscodeService {
}
}
async transcodeHlsRendition(params: TranscodeHlsRenditionParams): Promise<void> {
const encoder = await this.selectH264Encoder(params.hardwareAcceleration)
if (encoder.name !== 'libx264') {
try {
await this.executeFfmpegHlsTranscode(params, encoder)
return
} catch (err) {
if (this.isAbortError(err, params.signal)) {
throw err
}
logger.warn(
{
err,
encoder: encoder.name,
inputFile: params.inputFile,
outputDir: params.outputDir,
width: params.width,
height: params.height,
},
'Hardware HLS transcoding failed; falling back to software transcode (libx264)',
)
if (fs.existsSync(params.outputDir)) {
try {
fs.rmSync(params.outputDir, { recursive: true, force: true })
} catch {
// ignore unlink errors
}
}
await this.executeFfmpegHlsTranscode(params, H264_ENCODER_CONFIGS.libx264)
return
}
}
await this.executeFfmpegHlsTranscode(params, encoder)
}
private async executeFfmpegHlsTranscode(
params: TranscodeHlsRenditionParams,
encoder: EncoderConfig,
): Promise<void> {
fs.mkdirSync(params.outputDir, { recursive: true })
const isSourceHdr =
params.sourceIsHdr ||
params.sourceHdrType === 'pq' ||
params.sourceHdrType === 'hlg' ||
params.sourceHdrType === 'dovi_p5'
const isHdrOutput = Boolean(params.hdr)
const isVaapi = encoder.name === 'h264_vaapi'
const vaapiDevice = isVaapi ? (this.getVaapiDevice() ?? '/dev/dri/renderD128') : undefined
const segmentDuration = params.segmentDuration || 4
let calculatedFps = 30
if (params.frameRate) {
if (typeof params.frameRate === 'number') {
calculatedFps = params.frameRate
} else {
const parts = params.frameRate.split('/')
if (parts.length === 2) {
calculatedFps = parseFloat(parts[0]) / parseFloat(parts[1])
} else {
calculatedFps = parseFloat(params.frameRate)
}
}
}
if (!Number.isFinite(calculatedFps) || calculatedFps <= 0) {
calculatedFps = 30
}
const gopSize = Math.max(1, Math.round(calculatedFps * segmentDuration))
logger.info(
{
encoder: encoder.name,
hardwareAcceleration: params.hardwareAcceleration ?? 'off',
inputFile: params.inputFile,
outputDir: params.outputDir,
width: params.width,
height: params.height,
segmentDuration,
gopSize,
},
'Starting HLS rendition transcoding',
)
let filterComplex: string
const args: string[] = []
if (isVaapi && vaapiDevice) {
args.push('-init_hw_device', `vaapi=accel:${vaapiDevice}`, '-filter_hw_device', 'accel')
}
args.push('-i', params.inputFile)
const baseScale = `scale=w=${params.width}:h=${params.height}:force_original_aspect_ratio=decrease,scale=w='trunc(iw/2)*2':h='trunc(ih/2)*2'`
if (params.overlayFile) {
args.push('-i', params.overlayFile)
filterComplex = `[0:v]scale=${params.width}:${params.height}[vscaled];[vscaled][1:v]overlay=0:0`
} else {
filterComplex = `[0:v]${baseScale}`
}
filterComplex += `,fps=${calculatedFps}`
if (isVaapi) {
filterComplex += ',format=nv12,hwupload=extra_hw_frames=64'
}
filterComplex += '[vout]'
args.push('-filter_complex', filterComplex, '-map', '[vout]')
if (!params.disableAudio) {
args.push('-map', '0:a?')
}
args.push('-c:v', encoder.name)
if (encoder.presetArgs.length > 0) {
args.push(...encoder.presetArgs)
}
if (encoder.name === 'h264_videotoolbox') {
if (params.videoBitrate) {
args.push('-b:v', params.videoBitrate)
} else {
const targetBps = calculateEffectiveBitrateBps(
params.height,
params.width,
params.sourceVideoBitrate,
calculatedFps,
)
const targetKbps = Math.max(50, Math.round(targetBps / 1000))
args.push('-b:v', `${targetKbps}k`)
}
} else if (isVaapi) {
let maxKbps: number
if (params.videoBitrate) {
maxKbps = parseBitrateKbps(params.videoBitrate)
} else {
const { maxrate } = calculateMaxBitrate(
params.height,
params.width,
params.sourceVideoBitrate,
calculatedFps,
)
maxKbps = parseInt(maxrate, 10)
}
const targetKbps = Math.max(25, Math.ceil(maxKbps / 1.45))
const minKbps = Math.max(10, Math.round(targetKbps / 2))
args.push(
'-rc_mode',
'3',
'-b:v',
`${targetKbps}k`,
'-minrate',
`${minKbps}k`,
'-maxrate',
`${maxKbps}k`,
)
} else if (encoder.name === 'h264_qsv') {
let maxKbps: number
if (params.videoBitrate) {
maxKbps = parseBitrateKbps(params.videoBitrate)
} else {
const { maxrate } = calculateMaxBitrate(
params.height,
params.width,
params.sourceVideoBitrate,
calculatedFps,
)
maxKbps = parseInt(maxrate, 10)
}
const targetKbps = Math.max(25, Math.ceil(maxKbps / 1.45))
args.push('-b:v', `${targetKbps}k`, '-maxrate', `${maxKbps}k`)
} else if (encoder.name === 'h264_nvenc') {
let maxrateStr: string
let bufsizeStr: string
if (params.videoBitrate) {
const kbps = parseBitrateKbps(params.videoBitrate)
maxrateStr = `${kbps}k`
bufsizeStr = `${kbps * 2}k`
} else {
const res = calculateMaxBitrate(
params.height,
params.width,
params.sourceVideoBitrate,
calculatedFps,
)
maxrateStr = res.maxrate
bufsizeStr = res.bufsize
}
args.push(
'-rc',
'vbr',
'-cq',
'23',
'-maxrate',
maxrateStr,
'-bufsize',
bufsizeStr,
'-spatial-aq',
'1',
'-temporal-aq',
'1',
)
} else {
let maxrateStr: string
let bufsizeStr: string
if (params.videoBitrate) {
const kbps = parseBitrateKbps(params.videoBitrate)
maxrateStr = `${kbps}k`
bufsizeStr = `${kbps * 2}k`
} else {
const res = calculateMaxBitrate(
params.height,
params.width,
params.sourceVideoBitrate,
calculatedFps,
)
maxrateStr = res.maxrate
bufsizeStr = res.bufsize
}
args.push('-crf', '23', '-maxrate', maxrateStr, '-bufsize', bufsizeStr)
}
if (!isVaapi) {
args.push('-pix_fmt', 'yuv420p')
}
if (isSourceHdr && isHdrOutput) {
const isHlg = params.sourceHdrType === 'hlg' || params.sourceColorTransfer === 'arib-std-b67'
const trc = isHlg ? 'arib-std-b67' : 'smpte2084'
args.push('-color_primaries', 'bt2020', '-color_trc', trc, '-colorspace', 'bt2020nc')
if (encoder.name === 'libx264') {
args.push('-x264-params', `colorprim=bt2020:transfer=${trc}:colormatrix=bt2020nc`)
}
}
args.push(
'-r',
calculatedFps.toString(),
'-g',
gopSize.toString(),
'-keyint_min',
gopSize.toString(),
'-sc_threshold',
'0',
'-flags',
'+cgop',
'-force_key_frames',
`expr:gte(t,n_forced*${segmentDuration})`,
)
if (!params.disableAudio) {
args.push('-c:a', 'aac', '-b:a', '128k')
}
if (params.threads && params.threads > 0) {
args.push('-threads', params.threads.toString())
}
const initFilename = 'init.mp4'
const segmentPattern = path.join(params.outputDir, 'segment_%03d.m4s')
const playlistFile = path.join(params.outputDir, 'index.m3u8')
args.push(
'-f',
'hls',
'-hls_time',
segmentDuration.toString(),
'-hls_playlist_type',
'vod',
'-hls_segment_type',
'fmp4',
'-hls_fmp4_init_filename',
initFilename,
'-hls_segment_filename',
segmentPattern,
'-max_muxing_queue_size',
'1024',
playlistFile,
)
if (params.signal) {
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args], {
signal: params.signal,
})
} else {
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
}
}
async transcodeImage(
inputFile: string | Buffer,
outputFile: string,
@@ -2385,3 +2704,60 @@ function renderAnnotationsToSvg(
}
export const transcodeService = new TranscodeService()
export function buildHlsMasterPlaylist(
renditions: Array<{
resolution: string
width: number
height: number
bitrateBps: number
isHdr?: boolean
}>,
): string {
let content = '#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-INDEPENDENT-SEGMENTS\n'
for (const r of renditions) {
const bandwidth = r.bitrateBps + 128_000
const codecs = r.isHdr ? 'avc1.640028,mp4a.40.2' : 'avc1.64001f,mp4a.40.2'
content += `#EXT-X-STREAM-INF:BANDWIDTH=${bandwidth},AVERAGE-BANDWIDTH=${bandwidth},RESOLUTION=${r.width}x${r.height},CODECS="${codecs}"\n`
content += `${r.resolution}/index.m3u8\n`
}
return content
}
export async function rewriteM3u8WithPresignedUrls(
m3u8Content: string,
s3Prefix: string,
bucket: string,
): Promise<string> {
const lines = m3u8Content.split(/\r?\n/)
const rewrittenLines: string[] = []
for (const line of lines) {
const trimmed = line.trim()
if (!trimmed) {
rewrittenLines.push(line)
continue
}
const mapMatch = trimmed.match(/^#EXT-X-MAP:URI="([^"]+)"(.*)$/)
if (mapMatch) {
const initRelPath = mapMatch[1]
const extra = mapMatch[2] || ''
const initKey = path.posix.join(s3Prefix, initRelPath)
const presignedUrl = await s3Service.presign(bucket, initKey, 'GET')
rewrittenLines.push(`#EXT-X-MAP:URI="${presignedUrl}"${extra}`)
continue
}
if (trimmed.endsWith('.m4s') || trimmed.endsWith('.mp4') || trimmed.endsWith('.ts')) {
const segmentKey = path.posix.join(s3Prefix, trimmed)
const presignedUrl = await s3Service.presign(bucket, segmentKey, 'GET')
rewrittenLines.push(presignedUrl)
continue
}
rewrittenLines.push(line)
}
return rewrittenLines.join('\n')
}
+2
View File
@@ -198,6 +198,7 @@ describe('UploadService', () => {
threads: 0,
sprite: true,
poster: true,
hlsEnabled: false,
},
})
})
@@ -249,6 +250,7 @@ describe('UploadService', () => {
threads: 12,
sprite: true,
poster: true,
hlsEnabled: false,
},
})
})
+3
View File
@@ -335,11 +335,14 @@ export class UploadService {
if (isVideo) {
const strategy = settings?.transcode?.videoStrategy || 'best_match'
const hardwareAcceleration = settings?.transcode?.hardwareAcceleration || 'off'
const hlsEnabled = settings?.transcode?.hlsEnabled ?? false
const hlsResolutions = settings?.transcode?.hlsResolutions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await new VideoTranscoder(tx as any, asset.id, team.id, projectId)
.setStrategy(strategy)
.setHardwareAcceleration(hardwareAcceleration)
.setThreads(threads)
.setHls(hlsEnabled, hlsResolutions)
.withSprite()
.withPoster()
.submit()
+1 -1
View File
@@ -323,7 +323,7 @@ export class WatermarkService {
if (isVideo || isImage) {
mediaAssetIds.push(asset.id)
}
} else if (asset.type === 'folder') {
} else if (asset.type === 'folder' || asset.type === 'version_stack') {
folderIds.push(asset.id)
} else if (asset.type === 'symlink' && asset.targetId) {
// Resolve nested symlinks (e.g. folders that contain their own symlinks)
+14
View File
@@ -57,10 +57,14 @@ declare global {
// ----------------------------------------------------------------------
export type CollectionFilter = import('@shumai/dtos').CollectionFilter
export type HlsResolutionLadder = '480p' | '720p' | '1080p' | '1440p' | '2160p'
export interface TranscodeSettings {
videoStrategy: VideoTranscodeStrategy
hardwareAcceleration?: HardwareAcceleration
threads?: number
hlsEnabled?: boolean
hlsResolutions?: HlsResolutionLadder[]
}
export interface MediaProviderConfig {
@@ -178,6 +182,12 @@ declare global {
url?: string
}
export interface HlsInfo {
key: string
url?: string
resolutions: VideoTranscode[]
}
export interface MediaInfo {
duration: number
filesize: number
@@ -193,6 +203,8 @@ declare global {
finishedAt: string
metadata: Metadata | null
original: OriginalInfo | null
isHls?: boolean
hls?: HlsInfo
}
// ----------------------------------------------------------------------
@@ -341,6 +353,8 @@ declare global {
thumbnail?: boolean
sprite?: boolean
poster?: boolean
hlsEnabled?: boolean
hlsResolutions?: HlsResolutionLadder[]
}
export interface AgentTaskPayload {
+19
View File
@@ -123,6 +123,25 @@ export const assetInfoSchema = z.object({
.optional(),
videoPreview: z.object({ url: z.string(), key: z.string().optional() }).optional(),
pdfTranscode: z.object({ url: z.string(), key: z.string().optional() }).optional(),
isHls: z.boolean().optional(),
hls: z
.object({
key: z.string(),
url: z.string().optional(),
resolutions: z
.array(
z.object({
width: z.number(),
height: z.number(),
resolution: z.string().optional(),
hdr: z.boolean().optional(),
key: z.string().optional(),
url: z.string().optional(),
}),
)
.optional(),
})
.optional(),
proxyType: z.enum(['image', 'video', 'audio', 'pdf']).nullable().optional(),
metadata: mediaMetadataSchema.optional(),
})
+21
View File
@@ -80,6 +80,17 @@ export const HardwareAcceleration = {
} as const
export type HardwareAcceleration = (typeof HardwareAcceleration)[keyof typeof HardwareAcceleration]
export const HlsResolutionLadder = {
r480p: '480p',
r720p: '720p',
r1080p: '1080p',
r1440p: '1440p',
r2160p: '2160p',
} as const
export type HlsResolutionLadder = (typeof HlsResolutionLadder)[keyof typeof HlsResolutionLadder]
export const DEFAULT_HLS_RESOLUTIONS: HlsResolutionLadder[] = ['480p', '720p', '1080p']
export const updateTeamSettingsRequestSchema = z.union([
z.object({
key: z.literal('transcode.videoStrategy'),
@@ -93,6 +104,14 @@ export const updateTeamSettingsRequestSchema = z.union([
key: z.literal('transcode.threads'),
value: z.number().int().min(0).max(32),
}),
z.object({
key: z.literal('transcode.hlsEnabled'),
value: z.boolean(),
}),
z.object({
key: z.literal('transcode.hlsResolutions'),
value: z.array(z.nativeEnum(HlsResolutionLadder)),
}),
z.object({
key: z.literal('appearance.hideAgent'),
value: z.boolean(),
@@ -109,6 +128,8 @@ export interface TeamSettingsResponse {
videoStrategy?: VideoTranscodeStrategy
hardwareAcceleration?: HardwareAcceleration
threads?: number
hlsEnabled?: boolean
hlsResolutions?: HlsResolutionLadder[]
}
appearance?: TeamAppearanceSettings
semanticSearchEnabled?: boolean
Binary file not shown.
+151 -1
View File
@@ -1,9 +1,10 @@
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
import { prisma, AssetStatus } from '@shumai/db'
import { prisma, AssetStatus, AssetType } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { workflowService, TaskQueueTranscode } from '@shumai/workflow-core'
import { initTranscodeWorkflows } from '@shumai/transcode'
import { s3Service } from '@shumai/core/src/s3/s3'
import { assetService } from '@shumai/core/src/asset/asset'
import { fileURLToPath } from 'url'
import * as path from 'path'
import * as fs from 'fs'
@@ -221,5 +222,154 @@ describe.each(['local', 'temporal'] as const)(
// Audio proxy key should end with -audio-proxy.mp4
expect(mediaInfo.videoTranscodes[0].key).toContain('-audio-proxy.mp4')
}, 50000)
it('should run transcodeMedia workflow with HLS enabled and serve playlists for version stacks and symlinks', async () => {
// 1. Seed Database
const team = await prisma.team.create({
data: {
name: 'E2E HLS Video Transcode Team',
settings: {
transcode: {
videoStrategy: 'best_match',
hlsEnabled: true,
hlsResolutions: ['480p'],
},
},
},
})
const project = await prisma.project.create({
data: { name: 'E2E HLS Video Transcode Project', teamId: team.id },
})
const storageKey = await prisma.storageKey.create({
data: {
key: 'projects/e2e/video-hls.mp4',
},
})
const asset = await prisma.asset.create({
data: {
name: 'video-hls.mp4',
type: 'file',
status: 'uploaded',
mediaType: 'video/mp4',
projectId: project.id,
storageKeyId: storageKey.id,
},
})
// 2. Seed S3 Storage from Fixture (small-480p.mp4)
const mp4Path = path.join(fixturesDir, 'small-480p.mp4')
const mp4Buffer = fs.readFileSync(mp4Path)
await s3Service.putObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/video-hls.mp4',
mp4Buffer,
mp4Buffer.length,
'video/mp4',
)
// 3. Create Workflow Task
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'],
},
},
},
})
// 4. Wait for workflow to complete
console.log(
`Submitted E2E HLS Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`,
)
const completedTask = await workflowService.executeWait(task, 45000)
expect(completedTask.status).toBe('completed')
// 5. Verification of HLS generation
const updatedAsset = await prisma.asset.findUnique({
where: { id: asset.id },
})
expect(updatedAsset?.status).toBe(AssetStatus.processed)
const mediaInfo = updatedAsset?.media as PrismaJson.MediaInfo
expect(mediaInfo.isHls).toBe(true)
expect(mediaInfo.hls).toBeDefined()
expect(mediaInfo.hls?.key).toBe('projects/e2e/hls/master.m3u8')
expect(mediaInfo.hls?.resolutions).toBeDefined()
expect(mediaInfo.hls?.resolutions?.length).toBeGreaterThan(0)
expect(mediaInfo.hls?.resolutions?.[0]?.resolution).toBe('480p')
// Verify master playlist in S3
const masterObj = await s3Service.getObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/hls/master.m3u8',
)
const masterPlaylist = masterObj.buffer.toString('utf-8')
expect(masterPlaylist).toContain('#EXTM3U')
expect(masterPlaylist).toContain('480p/index.m3u8')
// Verify variant playlist in S3
const variantObj = await s3Service.getObject(
'shumai-e2e-test-bucket-transcode',
'projects/e2e/hls/480p/index.m3u8',
)
expect(variantObj.buffer.toString('utf-8')).toContain('#EXTM3U')
// 6. Test Version Stack & Symlink Resolution
const stack = await prisma.asset.create({
data: {
name: 'video-hls-stack',
type: AssetType.version_stack,
status: AssetStatus.processed,
projectId: project.id,
},
})
await prisma.asset.update({
where: { id: asset.id },
data: {
parentId: stack.id,
sortIndex: 'a',
},
})
const symlink = await prisma.asset.create({
data: {
name: 'video-hls-symlink',
type: AssetType.symlink,
status: AssetStatus.processed,
projectId: project.id,
targetId: stack.id,
},
})
// Calling getHlsMasterPlaylist with stack ID
const stackMaster = await assetService.getHlsMasterPlaylist({ assetId: stack.id })
expect(stackMaster).toContain('#EXTM3U')
expect(stackMaster).toContain('480p/index.m3u8')
// Calling getHlsMasterPlaylist with symlink ID
const symlinkMaster = await assetService.getHlsMasterPlaylist({ assetId: symlink.id })
expect(symlinkMaster).toContain('#EXTM3U')
expect(symlinkMaster).toContain('480p/index.m3u8')
// Calling getHlsVariantPlaylist with symlink ID
const symlinkVariant = await assetService.getHlsVariantPlaylist({
assetId: symlink.id,
resolution: '480p',
})
expect(symlinkVariant).toContain('#EXTM3U')
}, 60000)
},
)
+188 -1
View File
@@ -1,10 +1,16 @@
import { AssetStatus, prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService, type HdrType } from '@shumai/core/src/transcode/transcode'
import {
transcodeService,
buildHlsMasterPlaylist,
calculateEffectiveBitrateBps,
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'
import { parseCsvContent } from '@shumai/core/src/transcode/transcode'
import { resolutionToDimensions } from '../workflows/transcode-utils'
import {
getProxyType,
isCsvDocument,
@@ -380,6 +386,187 @@ export async function transcodeVideoActivity(
}
}
export interface HlsActivityParams {
taskId: string
assetKey: string
filePath: string
ladders: PrismaJson.HlsResolutionLadder[]
originalWidth: number
originalHeight: number
duration: number
originalFps: number
hardwareAcceleration?: 'off' | 'auto'
sourceVideoBitrate?: number
threads?: number
sourceIsHdr?: boolean
sourceHdrType?: HdrType
sourceColorTransfer?: string
sourceColorPrimaries?: string
sourceColorSpace?: string
}
export async function transcodeHlsActivity(params: HlsActivityParams): Promise<PrismaJson.HlsInfo> {
const bucket = process.env.S3_BUCKET || 'shumai'
const assetDir = path.posix.dirname(params.assetKey)
const hlsDirKey = path.posix.join(assetDir, 'hls')
const masterKey = path.posix.join(hlsDirKey, 'master.m3u8')
const signal = getActivityCancellationSignal(params.taskId)
if (signal?.aborted) {
throw ApplicationFailure.create({
message: 'HLS transcoding cancelled',
nonRetryable: true,
})
}
const hlsTmpDir = path.join(path.dirname(params.filePath), `hls-${ulid()}`)
try {
const renditionSpecs: Array<{
resolution: string
width: number
height: number
bitrateBps: number
isHdr?: boolean
}> = []
const mediaResolutions: PrismaJson.VideoTranscode[] = []
for (const ladder of params.ladders) {
if (signal?.aborted) {
throw ApplicationFailure.create({
message: 'HLS transcoding cancelled',
nonRetryable: true,
})
}
const [width, height] = resolutionToDimensions(
ladder,
params.originalWidth,
params.originalHeight,
)
const targetBps = calculateEffectiveBitrateBps(
height,
width,
params.sourceVideoBitrate,
params.originalFps,
)
const ladderDir = path.join(hlsTmpDir, ladder)
await transcodeService.transcodeHlsRendition({
inputFile: params.filePath,
outputDir: ladderDir,
width,
height,
frameRate: params.originalFps,
hardwareAcceleration: params.hardwareAcceleration,
sourceVideoBitrate: params.sourceVideoBitrate,
threads: params.threads,
signal,
hdr: Boolean(params.sourceIsHdr),
sourceIsHdr: params.sourceIsHdr,
sourceHdrType: params.sourceHdrType,
sourceColorTransfer: params.sourceColorTransfer,
sourceColorPrimaries: params.sourceColorPrimaries,
sourceColorSpace: params.sourceColorSpace,
})
renditionSpecs.push({
resolution: ladder,
width,
height,
bitrateBps: targetBps,
isHdr: params.sourceIsHdr,
})
mediaResolutions.push({
resolution: ladder,
width,
height,
hdr: Boolean(params.sourceIsHdr),
})
await ensureAssetNotPurging(params.assetKey)
const files = fs.readdirSync(ladderDir)
for (const file of files) {
const localFilePath = path.join(ladderDir, file)
const stat = fs.statSync(localFilePath)
const s3Key = path.posix.join(hlsDirKey, ladder, file)
let contentType = 'application/octet-stream'
if (file.endsWith('.m3u8')) {
contentType = 'application/vnd.apple.mpegurl'
} else if (file.endsWith('.mp4')) {
contentType = 'video/mp4'
} else if (file.endsWith('.m4s')) {
contentType = 'video/iso.segment'
}
const stream = Bun.file(localFilePath).stream()
await s3Service.putObject(bucket, s3Key, stream, stat.size, contentType)
}
}
const masterContent = buildHlsMasterPlaylist(renditionSpecs)
const masterBuffer = Buffer.from(masterContent, 'utf-8')
await ensureAssetNotPurging(params.assetKey)
await s3Service.putObject(
bucket,
masterKey,
masterBuffer,
masterBuffer.length,
'application/vnd.apple.mpegurl',
)
return {
key: masterKey,
resolutions: mediaResolutions,
}
} catch (err) {
const { code, message } = getErrorDetails(err)
const lowerMsg = message.toLowerCase()
if (
signal?.aborted ||
code === 'ABORT_ERR' ||
message.includes('aborted') ||
lowerMsg.includes('abort')
) {
throw ApplicationFailure.create({
message: 'HLS transcoding cancelled',
nonRetryable: true,
cause: err instanceof Error ? err : undefined,
})
}
if (
code === 'ENOENT' ||
lowerMsg.includes('enoent') ||
lowerMsg.includes('ffmpeg') ||
lowerMsg.includes('ffprobe') ||
lowerMsg.includes('spawn') ||
lowerMsg.includes('format') ||
lowerMsg.includes('no video stream found') ||
lowerMsg.includes('zscale') ||
lowerMsg.includes('tonemap')
) {
throw ApplicationFailure.create({
message: `HLS transcoding failed: ${message}`,
nonRetryable: true,
cause: err instanceof Error ? err : undefined,
})
}
throw err
} finally {
if (fs.existsSync(hlsTmpDir)) {
try {
fs.rmSync(hlsTmpDir, { recursive: true, force: true })
} catch {
// ignore cleanup error
}
}
}
}
export interface AudioActivityParams {
taskId?: string
assetKey: string
@@ -45,26 +45,37 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
},
}))
vi.mock('@shumai/core/src/transcode/transcode', () => ({
transcodeService: {
getVideoInfo: vi.fn(),
getImageInfo: vi.fn(),
createTempDir: vi.fn().mockReturnValue('/tmp'),
removeDir: vi.fn(),
renderSvgToPng: vi.fn().mockResolvedValue(Buffer.from('fake-overlay-png')),
downscaleImageToPng: vi.fn().mockResolvedValue({
buffer: Buffer.from('fake-block-png'),
width: 32,
height: 32,
}),
compositeOverlayToWebpFile: vi.fn().mockImplementation(async (_in, _overlay, out) => {
fs.writeFileSync(out, 'fake-webp')
}),
transcodeVideo: vi.fn().mockImplementation(async (params) => {
fs.writeFileSync(params.outputFile, 'fake-mp4')
}),
},
}))
vi.mock('@shumai/core/src/transcode/transcode', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/core/src/transcode/transcode')>()
return {
...actual,
transcodeService: {
...actual.transcodeService,
getVideoInfo: vi.fn(),
getImageInfo: vi.fn(),
createTempDir: vi.fn().mockReturnValue('/tmp'),
removeDir: vi.fn(),
renderSvgToPng: vi.fn().mockResolvedValue(Buffer.from('fake-overlay-png')),
downscaleImageToPng: vi.fn().mockResolvedValue({
buffer: Buffer.from('fake-block-png'),
width: 32,
height: 32,
}),
compositeOverlayToWebpFile: vi.fn().mockImplementation(async (_in, _overlay, out) => {
fs.writeFileSync(out, 'fake-webp')
}),
transcodeVideo: vi.fn().mockImplementation(async (params) => {
fs.writeFileSync(params.outputFile, 'fake-mp4')
}),
transcodeHlsRendition: vi.fn().mockImplementation(async (params) => {
fs.mkdirSync(params.outputDir, { recursive: true })
fs.writeFileSync(path.join(params.outputDir, 'init.mp4'), 'fake-init')
fs.writeFileSync(path.join(params.outputDir, 'segment_000.m4s'), 'fake-segment')
fs.writeFileSync(path.join(params.outputDir, 'index.m3u8'), 'fake-index')
}),
},
}
})
describe('Watermark Activities', () => {
setupTestDbHooks()
@@ -524,6 +535,89 @@ describe('Watermark Activities', () => {
fs.rmSync(sdrOut, { force: true })
fs.rmSync(hdrOut, { force: true })
})
it('produces watermarked HLS renditions and updates mediaInfo.hls when original is HLS', async () => {
const originalMedia: PrismaJson.MediaInfo = {
proxyType: 'video',
duration: 10,
filesize: 1000,
frames: 300,
videoTranscodes: [
{ key: 'files/asset/video-1080p.mp4', width: 1920, height: 1080, resolution: '1080p' },
],
imageTranscodes: [],
videoPreview: { width: 1920, height: 1080 },
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
startTimecode: '00:00:00:00',
hasAudio: false,
format: {},
bitRate: 1000,
},
original: { key: 'files/asset/raw.mp4', filesizeInBytes: 1000, codec: 'h264' },
finishedAt: new Date().toISOString(),
isHls: true,
hls: {
key: 'files/asset/hls/master.m3u8',
resolutions: [{ resolution: '1080p', width: 1920, height: 1080 }],
},
}
const storageKey = await prisma.storageKey.create({
data: { key: 'files/asset/raw.mp4' },
})
const asset = await prisma.asset.create({
data: {
name: 'wm-hls.mp4',
type: 'file',
status: 'processed',
storageKeyId: storageKey.id,
mediaType: 'video/mp4',
media: originalMedia,
},
})
const config = await prisma.watermarkConfig.create({
data: {
hash: 'hls-hash-' + Math.random(),
config: {
blocks: [
{
id: 'b1',
type: 'text',
text: 'HLS Confidential',
opacity: 0.5,
size: 0.05,
color: '#FFFFFF',
x: 0.5,
y: 0.5,
rotation: 0,
},
],
},
},
})
const media = await transcodeWatermarkMediaActivity({
assetId: asset.id,
watermarkConfigId: config.id,
})
expect(media.isHls).toBe(true)
expect(media.hls).toBeDefined()
expect(media.hls?.key).toContain(`hls-watermark-${config.id}/master.m3u8`)
expect(media.hls?.resolutions).toHaveLength(1)
expect(media.hls?.resolutions?.[0]?.resolution).toBe('1080p')
expect(transcodeService.transcodeHlsRendition).toHaveBeenCalledWith(
expect.objectContaining({
width: 1920,
height: 1080,
}),
)
})
})
describe('completeWatermarkFileActivity', () => {
+116 -1
View File
@@ -1,7 +1,11 @@
import { prisma, WatermarkFileStatus } from '@shumai/db'
import '@shumai/db/src/prisma-json-types'
import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core/src/transcode/transcode'
import {
transcodeService,
buildHlsMasterPlaylist,
calculateEffectiveBitrateBps,
} from '@shumai/core/src/transcode/transcode'
import {
generateWatermarkSvg,
RenderBlockImageData,
@@ -388,6 +392,117 @@ export async function transcodeWatermarkMediaActivity(
hdr: isVtHdr,
})
}
if (originalMedia?.isHls && originalMedia?.hls?.resolutions?.length) {
const hlsWatermarkDirKey = path.posix.join(
path.posix.dirname(assetKey),
`hls-watermark-${params.watermarkConfigId}`,
)
const masterKey = path.posix.join(hlsWatermarkDirKey, 'master.m3u8')
const hlsTmpDir = path.join(tmpDir, `hls-watermark-${params.watermarkConfigId}`)
fs.mkdirSync(hlsTmpDir, { recursive: true })
const renditionSpecs: Array<{
resolution: string
width: number
height: number
bitrateBps: number
isHdr?: boolean
}> = []
const hlsResolutions: PrismaJson.VideoTranscode[] = []
for (const vt of originalMedia.hls.resolutions) {
const ladder = vt.resolution || `${vt.height}p`
const targetWidth = vt.width || originalWidth
const targetHeight = vt.height || originalHeight
const isVtHdr = Boolean(vt.hdr)
const svgString = generateWatermarkSvg(config, targetWidth, targetHeight, blockImagesMap)
const overlayPngBuffer = await transcodeService.renderSvgToPng(svgString)
const overlayPngPath = path.join(tmpDir, `watermarkOverlay-hls-${ladder}.png`)
fs.writeFileSync(overlayPngPath, overlayPngBuffer)
const ladderDir = path.join(hlsTmpDir, ladder)
await transcodeService.transcodeHlsRendition({
inputFile: rawFilePath,
outputDir: ladderDir,
width: targetWidth,
height: targetHeight,
overlayFile: overlayPngPath,
frameRate,
hardwareAcceleration,
sourceVideoBitrate,
threads,
hdr: isVtHdr,
disableAudio: !hasAudio,
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 targetBps = calculateEffectiveBitrateBps(
targetHeight,
targetWidth,
sourceVideoBitrate,
frameRate,
)
renditionSpecs.push({
resolution: ladder,
width: targetWidth,
height: targetHeight,
bitrateBps: targetBps,
isHdr: isVtHdr,
})
const ladderFiles = fs.readdirSync(ladderDir)
for (const file of ladderFiles) {
const filePath = path.join(ladderDir, file)
const stat = fs.statSync(filePath)
totalSize += stat.size
const contentType = file.endsWith('.m3u8')
? 'application/vnd.apple.mpegurl'
: file.endsWith('.m4s')
? 'video/iso.segment'
: 'video/mp4'
const s3Key = path.posix.join(hlsWatermarkDirKey, ladder, file)
await s3Service.putObject(
bucket,
s3Key,
Bun.file(filePath).stream(),
stat.size,
contentType,
)
}
hlsResolutions.push({
key: path.posix.join(hlsWatermarkDirKey, ladder, 'index.m3u8'),
width: targetWidth,
height: targetHeight,
resolution: ladder,
hdr: isVtHdr,
})
}
const masterM3u8Content = buildHlsMasterPlaylist(renditionSpecs)
const masterM3u8Buffer = Buffer.from(masterM3u8Content, 'utf-8')
await s3Service.putObject(
bucket,
masterKey,
masterM3u8Buffer,
masterM3u8Buffer.length,
'application/vnd.apple.mpegurl',
)
mediaInfo.isHls = true
mediaInfo.hls = {
key: masterKey,
resolutions: hlsResolutions,
}
}
mediaInfo.filesize = totalSize
}
+10
View File
@@ -46,6 +46,16 @@ export class VideoTranscoder {
return this
}
setHls(enabled?: boolean, resolutions?: PrismaJson.HlsResolutionLadder[]): this {
if (enabled !== undefined) {
this.spec.hlsEnabled = enabled
}
if (resolutions) {
this.spec.hlsResolutions = resolutions
}
return this
}
async submit(): Promise<string> {
const task = await this.db.workflowTask.create({
data: {
@@ -0,0 +1,96 @@
import { describe, it, expect } from 'vitest'
import {
getTargetHlsResolutions,
resolutionToDimensions,
getTargetVideoResolutions,
isMimePsd,
} from './transcode-utils'
describe('transcode-utils', () => {
describe('getTargetHlsResolutions', () => {
it('uses default ladders when configuredLadders is undefined or empty', () => {
// 1080p source video (1920x1080)
const res1 = getTargetHlsResolutions(undefined, 1920, 1080)
expect(res1).toEqual(['1080p', '720p', '480p'])
const res2 = getTargetHlsResolutions([], 1920, 1080)
expect(res2).toEqual(['1080p', '720p', '480p'])
})
it('caps ladders up to source resolution (no upscaling)', () => {
// 1080p source with 4K configured ladders -> should cap to 1080p
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 1920, 1080)
expect(res).toEqual(['1080p', '720p', '480p'])
})
it('includes higher ladders when source resolution supports them', () => {
// 4K source (3840x2160)
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 3840, 2160)
expect(res).toEqual(['2160p', '1440p', '1080p', '720p', '480p'])
// 1440p source (2560x1440)
const res1440 = getTargetHlsResolutions(
['480p', '720p', '1080p', '1440p', '2160p'],
2560,
1440,
)
expect(res1440).toEqual(['1440p', '1080p', '720p', '480p'])
})
it('handles vertical videos correctly by using the longest side', () => {
// 1080x1920 (vertical 1080p)
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 1080, 1920)
expect(res).toEqual(['1080p', '720p', '480p'])
})
it('returns empty array when source is smaller than smallest ladder', () => {
// 640x360 source is smaller than 480p (854 long side)
const res = getTargetHlsResolutions(undefined, 640, 360)
expect(res).toEqual([])
})
it('respects selective subset of configured ladders', () => {
// User only selected 720p and 1440p
const res = getTargetHlsResolutions(['720p', '1440p'], 2560, 1440)
expect(res).toEqual(['1440p', '720p'])
})
})
describe('resolutionToDimensions', () => {
it('calculates dimensions correctly for 1440p and 480p', () => {
const [w1440, h1440] = resolutionToDimensions('1440p', 3840, 2160)
expect(w1440).toBe(2560)
expect(h1440).toBe(1440)
const [w480, h480] = resolutionToDimensions('480p', 1920, 1080)
expect(w480).toBe(854)
expect(h480).toBe(480)
})
it('ensures even dimensions for odd calculations', () => {
const [w, h] = resolutionToDimensions('720p', 1921, 1081)
expect(w % 2).toBe(0)
expect(h % 2).toBe(0)
})
it('returns [0, 0] for unknown resolution', () => {
expect(resolutionToDimensions('invalid', 1920, 1080)).toEqual([0, 0])
})
})
describe('getTargetVideoResolutions', () => {
it('returns best match resolution for standard video', () => {
const resolutions = getTargetVideoResolutions('best_match', 1920, 1080)
expect(resolutions).toEqual(['180p', '1080p'])
})
})
describe('isMimePsd', () => {
it('identifies PSD mime types', () => {
expect(isMimePsd('image/vnd.adobe.photoshop')).toBe(true)
expect(isMimePsd('image/x-photoshop')).toBe(true)
expect(isMimePsd('image/psd')).toBe(true)
expect(isMimePsd('image/png')).toBe(false)
})
})
})
@@ -13,13 +13,42 @@ export function isMimePsd(mimeType: string): boolean {
const RESOLUTION_LONG_SIDES: Record<string, number> = {
'2160p': 3840,
'1440p': 2560,
'1080p': 1920,
'720p': 1280,
'540p': 960,
'480p': 854,
'360p': 640,
'180p': 320,
}
export const HLS_LADDER_ORDER: PrismaJson.HlsResolutionLadder[] = [
'2160p',
'1440p',
'1080p',
'720p',
'480p',
]
export function getTargetHlsResolutions(
configuredLadders: PrismaJson.HlsResolutionLadder[] | undefined,
originalWidth: number,
originalHeight: number,
): PrismaJson.HlsResolutionLadder[] {
const selectedLadders =
configuredLadders && configuredLadders.length > 0
? configuredLadders
: (['480p', '720p', '1080p'] as PrismaJson.HlsResolutionLadder[])
const rawLongSide = Math.max(originalWidth, originalHeight)
return HLS_LADDER_ORDER.filter((ladder) => {
if (!selectedLadders.includes(ladder)) return false
const targetLongSide = RESOLUTION_LONG_SIDES[ladder]
return targetLongSide <= rawLongSide
})
}
export function resolutionToDimensions(
resolution: string,
originalWidth: number,
@@ -25,6 +25,7 @@ describe('transcodeVideoWorkflow', () => {
getAssetActivity: Object.assign(vi.fn(), { _activityName: 'getAssetActivity' }),
getMediaInfoActivity: Object.assign(vi.fn(), { _activityName: 'getMediaInfoActivity' }),
transcodeVideoActivity: Object.assign(vi.fn(), { _activityName: 'transcodeVideoActivity' }),
transcodeHlsActivity: Object.assign(vi.fn(), { _activityName: 'transcodeHlsActivity' }),
transcodeAudioActivity: Object.assign(vi.fn(), { _activityName: 'transcodeAudioActivity' }),
transcodeImageActivity: Object.assign(vi.fn(), { _activityName: 'transcodeImageActivity' }),
generateSpriteActivity: Object.assign(vi.fn(), { _activityName: 'generateSpriteActivity' }),
@@ -702,4 +703,97 @@ describe('transcodeVideoWorkflow', () => {
status: WorkflowTaskStatus.completed,
})
})
it('should trigger HLS transcoding when hlsEnabled is true', async () => {
const task: WorkflowTask = {
id: 'task-hls',
assetId: 'asset-hls',
type: WorkflowTaskType.transcode_video,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
transcode: {
videoStrategy: 'best_match',
hlsEnabled: true,
hlsResolutions: ['480p', '720p', '1080p'],
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid-hls',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-hls',
storageKey: { key: 'files/asset-hls/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: {},
},
videoTranscodes: [],
imageTranscodes: [],
})
mockActivities.transcodeVideoActivity.mockResolvedValue({
key: 'files/asset-hls/video-1080p.mp4',
width: 1920,
height: 1080,
resolution: '1080p',
})
mockActivities.transcodeHlsActivity.mockResolvedValue({
key: 'files/asset-hls/hls/master.m3u8',
resolutions: ['1080p', '720p', '480p'],
})
await transcodeVideoWorkflow(task)
expect(mockActivities.transcodeHlsActivity).toHaveBeenCalledWith(
expect.objectContaining({
taskId: 'task-hls',
assetKey: 'files/asset-hls/video.mp4',
filePath: '/tmp/video.mp4',
ladders: ['1080p', '720p', '480p'],
originalWidth: 1920,
originalHeight: 1080,
}),
)
expect(mockActivities.updateAssetMediaActivity).toHaveBeenCalledWith({
assetId: 'asset-hls',
mediaInfo: expect.objectContaining({
isHls: true,
hls: {
key: 'files/asset-hls/hls/master.m3u8',
resolutions: ['1080p', '720p', '480p'],
},
}),
})
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
taskId: 'task-hls',
status: WorkflowTaskStatus.completed,
})
})
})
@@ -8,7 +8,11 @@ import {
failTask,
cleanupTmpDir,
} from './common'
import { getTargetVideoResolutions, resolutionToDimensions } from './transcode-utils'
import {
getTargetVideoResolutions,
getTargetHlsResolutions,
resolutionToDimensions,
} from './transcode-utils'
export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void> {
let tmpDir: string | undefined
@@ -21,6 +25,7 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
updateAssetStatusActivity,
getMediaInfoActivity,
transcodeVideoActivity,
transcodeHlsActivity,
transcodeAudioActivity,
transcodeImageActivity,
extractPosterActivity,
@@ -189,6 +194,35 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
mediaInfo.videoTranscodes.push(videoTranscode)
}
if (spec.hlsEnabled) {
const hlsLadders = getTargetHlsResolutions(
spec.hlsResolutions,
metadata.originalWidth,
metadata.originalHeight,
)
if (hlsLadders.length > 0) {
const hlsResult = await executeActivity(workerQueue, transcodeHlsActivity, {
taskId: task.id,
assetKey: key,
filePath,
ladders: hlsLadders,
originalWidth: metadata.originalWidth,
originalHeight: metadata.originalHeight,
originalFps: metadata.frameRate,
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
hardwareAcceleration: spec.hardwareAcceleration,
threads: spec.threads,
sourceIsHdr: metadata.isHdr,
sourceHdrType: metadata.hdrType,
sourceColorTransfer: metadata.colorTransfer,
sourceColorPrimaries: metadata.colorPrimaries,
sourceColorSpace: metadata.colorSpace,
})
mediaInfo.isHls = true
mediaInfo.hls = hlsResult
}
}
}
const isAudio = mediaInfo.proxyType === 'audio'
+10 -3
View File
@@ -1,10 +1,17 @@
import type { VideoTranscode } from '@shumai/dtos'
export type PaneKind = 'image' | 'video' | 'unsupported'
export type CompareSide = 'left' | 'right'
export type DisplayTranscode = VideoTranscode & { resolution: string }
export interface DisplayTranscode {
id?: string
url?: string
key?: string
width: number
height: number
size?: number
hdr?: boolean
resolution: string
}
/**
* Reactive state a pane reports upward so the shared control bar can render.
@@ -32,15 +32,23 @@ interface CompareVideoPaneProps {
function computeResolutions(file: AssetInfo): DisplayTranscode[] {
// Only transcoded proxy versions are ever displayed; the raw original file
// is never used as a playback source.
return (file.media?.videoTranscodes ?? []).map((t) => {
const isHls = Boolean(file.media?.isHls && file.media.hls?.url)
const baseTranscodes = isHls
? (file.media?.hls?.resolutions ?? [])
: (file.media?.videoTranscodes ?? [])
return baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return { ...t, resolution }
})
}
@@ -117,11 +125,14 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
const containerDuration = metadata?.duration || 0
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
const isHls = Boolean(file.media?.isHls && file.media.hls?.url)
const resolutions = computeResolutions(file)
const initialRes = getInitialResolution(resolutions)
const [currentResolution, setCurrentResolution] = useState(initialRes?.resolution ?? '')
const initialRes = isHls ? null : getInitialResolution(resolutions)
const [currentResolution, setCurrentResolution] = useState(
isHls ? 'Auto' : (initialRes?.resolution ?? ''),
)
const [isCurrentHdr, setIsCurrentHdr] = useState(initialRes?.hdr)
const currentSrcRef = useRef(initialRes?.url)
const currentSrcRef = useRef(isHls ? file.media!.hls!.url : initialRes?.url)
const isAudio = file.proxyType === 'audio'
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames, isAudio)
@@ -160,12 +171,12 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
// if the parent renders this pane without a per-asset `key`. Runs before the
// video.js init effect below (declaration order) so the ref is fresh.
useEffect(() => {
const res = getInitialResolution(resolutions)
setCurrentResolution(res?.resolution ?? '')
const res = isHls ? null : getInitialResolution(resolutions)
setCurrentResolution(isHls ? 'Auto' : (res?.resolution ?? ''))
setIsCurrentHdr(res?.hdr)
currentSrcRef.current = res?.url
currentSrcRef.current = isHls ? file.media!.hls!.url : res?.url
setHasStartedPlaying(false)
}, [file.id])
}, [file.id, Boolean(isHls ? file.media?.hls?.url : initialRes?.url)])
// Initialize video.js
useEffect(() => {
@@ -175,12 +186,15 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
videoElement.style.pointerEvents = 'none'
videoContainerRef.current.appendChild(videoElement)
const targetSrc = isHls ? file.media!.hls!.url! : (currentSrcRef.current ?? '')
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
const player = (playerRef.current = videojs(videoElement, {
controls: false,
autoplay: false,
preload: 'auto',
playsinline: true,
sources: [{ src: currentSrcRef.current ?? '', type: 'video/mp4' }],
sources: [{ src: targetSrc, type: targetType }],
}))
const htmlVid = videoElement.querySelector('video')
@@ -325,6 +339,30 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
(resolution: string, hdr?: boolean) => {
const player = playerRef.current
if (!player) return
if (isHls) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ql = (player as any).qualityLevels?.()
if (resolution === 'Auto') {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = true
}
}
setCurrentResolution('Auto')
} else {
const target = resolutions.find((r) => r.resolution === resolution)
if (target && ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = ql[i].height === target.height
}
}
setCurrentResolution(resolution)
setIsCurrentHdr(target?.hdr)
}
return
}
const target =
hdr !== undefined
? resolutions.find(
@@ -346,7 +384,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
}
})
},
[resolutions],
[resolutions, isHls],
)
useImperativeHandle(
@@ -24,7 +24,7 @@ import {
} from '../../ui/dropdown-menu'
import ProgressBar from './progress-bar'
import { formatTime, formatTimecode } from './utils'
import type { ControlBarProps } from './video-control-bar'
import type { ControlBarProps, DisplayTranscode } from './video-control-bar'
export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
state,
@@ -189,9 +189,11 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
<span className="flex items-center gap-1 text-[11px]">
<span>
{!isAudio && state.currentResolution
? state.currentResolution === 'Original'
? m.original()
: state.currentResolution
? state.currentResolution === 'Auto'
? m.quality_auto()
: state.currentResolution === 'Original'
? m.original()
: state.currentResolution
: `${state.playbackRate}x`}
</span>
{!isAudio && state.isCurrentHdr && (
@@ -208,6 +210,25 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
<DropdownMenuLabel className="text-[10px] font-bold text-muted-foreground uppercase tracking-wider">
{m.quality()}
</DropdownMenuLabel>
{data.media?.isHls && (
<DropdownMenuItem
onClick={() =>
changeResolution({
resolution: 'Auto',
width: data.media?.metadata?.originalWidth ?? 0,
height: data.media?.metadata?.originalHeight ?? 0,
} as DisplayTranscode)
}
className={cn(
'flex w-full items-center justify-between text-xs cursor-pointer',
state.currentResolution === 'Auto'
? 'text-primary font-medium'
: 'text-foreground',
)}
>
<span>{m.quality_auto()}</span>
</DropdownMenuItem>
)}
{resolutions.map((res) => {
const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
const isSelected =
@@ -1,7 +1,7 @@
import { cn } from '@/ui/lib/utils'
import { m } from '@/ui/paraglide/messages.js'
import { useUiStore } from '@/ui/stores/ui'
import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
import type { AssetInfo } from '@shumai/dtos'
import {
Check,
ChevronDown,
@@ -46,7 +46,16 @@ export interface PlayerState {
isCurrentHdr?: boolean
}
export type DisplayTranscode = VideoTranscode & { resolution: string }
export interface DisplayTranscode {
id?: string
url?: string
key?: string
width: number
height: number
size?: number
hdr?: boolean
resolution: string
}
export interface ControlBarProps {
state: PlayerState
@@ -322,9 +331,11 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
<Settings className="h-3.5 w-3.5" />
<span className="flex items-center gap-1">
<span>
{state.currentResolution === 'Original'
? m.original()
: state.currentResolution}
{state.currentResolution === 'Auto'
? m.quality_auto()
: state.currentResolution === 'Original'
? m.original()
: state.currentResolution}
</span>
{state.isCurrentHdr && (
<span className="px-1 py-0.2 rounded text-[10px] font-bold bg-amber-500/20 text-amber-500 border border-amber-500/30">
@@ -337,6 +348,25 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
<DropdownMenuContent>
<DropdownMenuLabel>{m.quality()}</DropdownMenuLabel>
{data.media?.isHls && (
<DropdownMenuItem
onClick={() =>
changeResolution({
resolution: 'Auto',
width: data.media?.metadata?.originalWidth ?? 0,
height: data.media?.metadata?.originalHeight ?? 0,
} as DisplayTranscode)
}
className={cn(
'flex w-full items-center justify-between cursor-pointer',
state.currentResolution === 'Auto'
? 'text-primary font-medium'
: 'text-foreground',
)}
>
<span>{m.quality_auto()}</span>
</DropdownMenuItem>
)}
{resolutions.map((res) => {
const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
const isSelected =
@@ -389,8 +419,13 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{resolutions.map((res) => {
const itemKey = res.key || `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
{(data.media?.videoTranscodes && data.media.videoTranscodes.length > 0
? data.media.videoTranscodes
: resolutions
).map((res) => {
const resLabel =
'resolution' in res && res.resolution ? res.resolution : `${res.height}p`
const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}`
return (
<DropdownMenuItem
key={itemKey}
@@ -398,7 +433,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground cursor-pointer"
>
<div className="flex items-center gap-1.5">
<span>{res.resolution}</span>
<span>{resLabel}</span>
{res.hdr && (
<span className="px-1 py-0.2 rounded text-[10px] font-bold bg-amber-500/20 text-amber-500 border border-amber-500/30">
{m.hdr()}
@@ -33,15 +33,23 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
) => {
const localPlayerRef = useRef<Player | null>(null)
const playerRef = localPlayerRef
const resolutions: DisplayTranscode[] = (data.media?.videoTranscodes ?? []).map((t) => {
const isHls = Boolean(data.media?.isHls && data.media.hls?.url)
const baseTranscodes = isHls
? (data.media?.hls?.resolutions ?? [])
: (data.media?.videoTranscodes ?? [])
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return {
...t,
@@ -50,7 +58,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
})
// Only transcoded proxy versions are ever displayed; the raw original file
// is never used as a playback source.
const hasMedia = resolutions.length > 0 && !!data.media?.metadata
const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata
// We use a container ref to manually append the video element
const videoContainerRef = useRef<HTMLDivElement>(null)
@@ -90,7 +98,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
return bestFit || sortedResolutions[sortedResolutions.length - 1]
}
const initialRes = getInitialResolution()
const initialRes = isHls ? null : getInitialResolution()
// State
const [state, setState] = useState<PlayerState>({
@@ -104,8 +112,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
playbackRate: 1,
isFullScreen: false,
showFrames: false,
currentResolution: initialRes?.resolution ?? '',
currentSrc: initialRes?.url ?? '',
currentResolution: isHls ? 'Auto' : (initialRes?.resolution ?? ''),
currentSrc: isHls ? data.media!.hls!.url : (initialRes?.url ?? ''),
isCurrentHdr: initialRes?.hdr ?? false,
})
const [hasStartedPlaying, setHasStartedPlaying] = useState(false)
@@ -147,15 +155,15 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
useEffect(() => {
const res = getInitialResolution()
const res = isHls ? null : getInitialResolution()
setState((prev) => ({
...prev,
isPlaying: false,
progress: 0,
currentTime: 0,
duration: data.media?.metadata?.duration || 0,
currentResolution: res?.resolution ?? '',
currentSrc: res?.url ?? '',
currentResolution: isHls ? 'Auto' : (res?.resolution ?? ''),
currentSrc: isHls ? (data.media?.hls?.url ?? '') : (res?.url ?? ''),
isCurrentHdr: res?.hdr ?? false,
}))
setHasManuallyZoomed(false)
@@ -163,7 +171,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
setBuffered(0)
lastProcessedStartTimeRef.current = null
setHasStartedPlaying(false)
}, [data.id, Boolean(initialRes?.url)])
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url)])
const vidW = data.media?.metadata?.originalWidth || 1920
const vidH = data.media?.metadata?.originalHeight || 1080
@@ -294,8 +302,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
useEffect(() => {
if (!videoContainerRef.current) return
const initialRes = getInitialResolution()
const targetSrc = initialRes?.url ?? ''
const targetSrc = isHls ? (data.media?.hls?.url ?? '') : (getInitialResolution()?.url ?? '')
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
if (!targetSrc) return
// Clean up previous player if exists
@@ -325,7 +333,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
sources: [
{
src: targetSrc,
type: 'video/mp4',
type: targetType,
},
],
}))
@@ -429,7 +437,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
videoRef.current = null
}
}
}, [data.id, Boolean(initialRes?.url), autoPlay])
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url), autoPlay])
// Handle changes to startTime (e.g., clicking different chunks in search results)
useEffect(() => {
@@ -620,6 +628,34 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const player = playerRef.current
if (!player) return
if (isHls) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ql = (player as any).qualityLevels?.()
if (res.resolution === 'Auto') {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = true
}
}
setState((prev) => ({
...prev,
currentResolution: 'Auto',
}))
} else {
if (ql) {
for (let i = 0; i < ql.length; i++) {
ql[i].enabled = ql[i].height === res.height
}
}
setState((prev) => ({
...prev,
currentResolution: res.resolution,
isCurrentHdr: res.hdr ?? false,
}))
}
return
}
const wasPlaying = !player.paused()
const currentT = player.currentTime()
+5
View File
@@ -263,6 +263,11 @@
"hardware_acceleration_auto_description": "Automatically probes and uses GPU/Hardware acceleration (NVENC, QSV, VA-API, AMF, VideoToolbox). If hardware acceleration is unsupported on the host, video transcoding may fail.",
"ffmpeg_threads": "FFmpeg Threads",
"ffmpeg_threads_description": "Maximum number of CPU threads used by FFmpeg during transcoding. Set to 0 for Auto.",
"hls_streaming": "HLS Streaming",
"hls_streaming_description": "Enable HTTP Live Streaming (HLS) with adaptive bitrate ladders for smooth video playback.",
"hls_resolution_ladders": "Resolution Ladders",
"hls_resolution_ladders_description": "Select the target resolutions to generate for HLS streaming. Ladders are only generated up to the source video resolution.",
"quality_auto": "Auto",
"threads_auto": "Auto",
"threads_count": "{count} threads",
"hdr": "HDR",
+5
View File
@@ -263,6 +263,11 @@
"hardware_acceleration_auto_description": "自动检测并使用 GPU/硬件加速(NVENC、QSV、VA-API、AMF、VideoToolbox)。若宿主硬件不支持加速,视频转码可能会失败。",
"ffmpeg_threads": "FFmpeg 线程数",
"ffmpeg_threads_description": "FFmpeg 转码时使用的最大 CPU 线程数。设置为 0 表示自动。",
"hls_streaming": "HLS 流媒体",
"hls_streaming_description": "启用自适应码率阶梯的 HTTP Live Streaming (HLS),带来流畅的视频流播放体验。",
"hls_resolution_ladders": "分辨率阶梯",
"hls_resolution_ladders_description": "选择 HLS 流媒体生成的目标分辨率。仅会生成不超过原始视频分辨率的分辨率阶梯。",
"quality_auto": "自动",
"threads_auto": "自动",
"threads_count": "{count} 线程",
"hdr": "HDR",
@@ -4,7 +4,11 @@ import {
HardwareAcceleration,
UpdateTeamSettingsRequest,
TeamSettingsResponse,
HlsResolutionLadder,
DEFAULT_HLS_RESOLUTIONS,
} from '@shumai/dtos'
import { Switch } from '@/ui/components/ui/switch'
import { Checkbox } from '@/ui/components/ui/checkbox'
import { client } from '@/ui/api/client'
import { AgentsSettings } from '@/ui/components/settings/AgentsSettings'
import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings'
@@ -333,6 +337,35 @@ function TeamSettingsPage() {
})
}
const handleHlsEnabledChange = (enabled: boolean) => {
updateSettings({
teamId,
data: {
key: 'transcode.hlsEnabled',
value: enabled,
},
})
}
const handleHlsLadderToggle = (ladder: HlsResolutionLadder) => {
const current =
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const updated = current.includes(ladder)
? current.filter((l) => l !== ladder)
: [...current, ladder]
if (updated.length === 0) return
updateSettings({
teamId,
data: {
key: 'transcode.hlsResolutions',
value: updated,
},
})
}
if (isSettingsLoading || isMeLoading) {
return (
<div className="flex h-screen items-center justify-center">
@@ -359,6 +392,12 @@ function TeamSettingsPage() {
(settings as any)?.transcode?.hardwareAcceleration || HardwareAcceleration.off
const currentThreads = localThreads ?? serverThreads
const currentHlsEnabled =
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsEnabled ?? false
const currentHlsResolutions: HlsResolutionLadder[] =
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const ALL_HLS_LADDERS: HlsResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p']
return (
<div className="h-full bg-background font-sans selection:bg-primary/20 transition-colors duration-300">
@@ -891,6 +930,50 @@ function TeamSettingsPage() {
</div>
</div>
</div>
{/* HLS Streaming */}
<div className="space-y-4 pt-6 border-t border-border">
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<h3 className="text-lg font-medium">{m.hls_streaming()}</h3>
<p className="text-sm text-muted-foreground">
{m.hls_streaming_description()}
</p>
</div>
<Switch
checked={currentHlsEnabled}
onCheckedChange={handleHlsEnabledChange}
/>
</div>
{currentHlsEnabled && (
<div className="space-y-3 pt-2 pl-1">
<div className="space-y-1">
<h4 className="text-sm font-medium">{m.hls_resolution_ladders()}</h4>
<p className="text-xs text-muted-foreground">
{m.hls_resolution_ladders_description()}
</p>
</div>
<div className="flex flex-wrap gap-4 pt-1">
{ALL_HLS_LADDERS.map((ladder) => {
const checked = currentHlsResolutions.includes(ladder)
return (
<label
key={ladder}
className="flex items-center space-x-2 cursor-pointer text-sm"
>
<Checkbox
checked={checked}
onCheckedChange={() => handleHlsLadderToggle(ladder)}
/>
<span>{ladder}</span>
</label>
)
})}
</div>
</div>
)}
</div>
</CardContent>
</Card>
</div>