mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
fix(search): preview matched video chunks on hover (#540)
* fix(search): preview matched video chunks on hover * fix(search): show chunk covers before hover * fix(search): reuse sprites for whole-video matches
This commit is contained in:
@@ -9,12 +9,17 @@ import {
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { gotenbergService } from '@shumai/core/src/gotenberg/gotenberg'
|
||||
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||
import { sampleVideoChunkSprite } from '@shumai/core/src/transcode/video-chunk-sprite'
|
||||
import * as child_process from 'child_process'
|
||||
import * as fs from 'fs'
|
||||
|
||||
vi.mock('child_process', () => ({
|
||||
execFile: vi.fn(),
|
||||
}))
|
||||
vi.mock('@shumai/core/src/transcode/video-chunk-sprite', () => ({
|
||||
composeVideoChunkSprite: vi.fn(),
|
||||
sampleVideoChunkSprite: vi.fn(),
|
||||
}))
|
||||
import {
|
||||
ensureLocalMediaFile,
|
||||
getMediaInfoActivity,
|
||||
@@ -1238,6 +1243,42 @@ describe('Transcode Activities', () => {
|
||||
})
|
||||
|
||||
describe('Video Chunk Activities', () => {
|
||||
it('returns the embedding clip when the optional sprite upload fails', async () => {
|
||||
vi.mocked(child_process.execFile).mockImplementation(
|
||||
// The activity awaits the promisified callback overload.
|
||||
((_file: string, _args: string[], callback: (err: null) => void) => {
|
||||
callback(null)
|
||||
}) as typeof child_process.execFile,
|
||||
)
|
||||
vi.mocked(sampleVideoChunkSprite).mockResolvedValueOnce({
|
||||
image: Buffer.from('mock sprite'),
|
||||
columns: 1,
|
||||
rows: 1,
|
||||
frameCount: 1,
|
||||
})
|
||||
vi.mocked(s3Service.putObject)
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockRejectedValueOnce(new Error('Sprite storage unavailable'))
|
||||
|
||||
const chunk = await transcodeVideoChunkActivity({
|
||||
assetId: 'asset-abc',
|
||||
assetKey: 'files/storage-id/proxy.mp4',
|
||||
originalKey: 'files/storage-id/original.mp4',
|
||||
startTime: 0,
|
||||
endTime: 1,
|
||||
})
|
||||
|
||||
expect(chunk.chunkKey).toMatch(/\.mp4$/)
|
||||
expect(chunk.sprite).toBeUndefined()
|
||||
expect(s3Service.putObject).toHaveBeenLastCalledWith(
|
||||
'shumai',
|
||||
'files/storage-id/chunk-sprites/asset-abc/video-0-1.webp',
|
||||
expect.any(Buffer),
|
||||
expect.any(Number),
|
||||
'image/webp',
|
||||
)
|
||||
})
|
||||
|
||||
it('should transcode video chunk using ffmpeg and upload to S3', async () => {
|
||||
// Mock child_process.execFile to simulate ffmpeg succeeding
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
@@ -8,6 +8,11 @@ import {
|
||||
} from '@shumai/core/src/transcode/transcode'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { getDerivedArtifactDirectory, stemFromKey } from '@shumai/core/src/utils/filename'
|
||||
import {
|
||||
composeVideoChunkSprite,
|
||||
sampleVideoChunkSprite,
|
||||
} from '@shumai/core/src/transcode/video-chunk-sprite'
|
||||
import { hasStoredVideoSprite } from '@shumai/core/src/transcode/video-sprite'
|
||||
import {
|
||||
gotenbergService,
|
||||
type GotenbergConvertOptions,
|
||||
@@ -1603,15 +1608,49 @@ export interface TranscodeVideoChunkParams {
|
||||
taskId?: string
|
||||
assetId: string
|
||||
assetKey?: string
|
||||
originalKey?: string
|
||||
filePath?: string
|
||||
startTime: number
|
||||
endTime: number
|
||||
format?: 'video' | 'frames'
|
||||
fullSpriteKey?: string
|
||||
}
|
||||
|
||||
async function uploadVideoChunkSprite(
|
||||
params: TranscodeVideoChunkParams,
|
||||
render: () => ReturnType<typeof composeVideoChunkSprite>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<PrismaJson.VideoChunkSprite | undefined> {
|
||||
if (signal?.aborted) return
|
||||
try {
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
if (params.fullSpriteKey && (await hasStoredVideoSprite(bucket, params.fullSpriteKey))) return
|
||||
|
||||
if (signal?.aborted) return
|
||||
const sprite = await render()
|
||||
if (!sprite || signal?.aborted) return
|
||||
|
||||
const originalKey = params.originalKey ?? params.assetKey
|
||||
const directory = getDerivedArtifactDirectory(originalKey ?? '', params.assetId)
|
||||
const format = params.format ?? 'video'
|
||||
const key = `${directory}/chunk-sprites/${params.assetId}/${format}-${params.startTime}-${params.endTime}.webp`
|
||||
await ensureAssetNotPurgingByIdOrKey(params.assetId, originalKey)
|
||||
const { image, ...layout } = sprite
|
||||
await s3Service.putObject(bucket, key, image, image.length, 'image/webp')
|
||||
return { key, ...layout }
|
||||
} catch (err) {
|
||||
if (!signal?.aborted) {
|
||||
logger.warn(
|
||||
{ err, assetId: params.assetId, startTime: params.startTime, endTime: params.endTime },
|
||||
'Failed to generate video chunk sprite',
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const transcodeVideoChunkActivity = defineStep(async function transcodeVideoChunkActivity(
|
||||
params: TranscodeVideoChunkParams,
|
||||
): Promise<{ chunkKey: string }> {
|
||||
): Promise<{ chunkKey: string; sprite?: PrismaJson.VideoChunkSprite }> {
|
||||
if (!params.filePath && !params.assetKey) {
|
||||
throw ApplicationFailure.create({
|
||||
message: 'Neither filePath nor assetKey was provided for video chunk transcoding',
|
||||
@@ -1704,7 +1743,12 @@ export const transcodeVideoChunkActivity = defineStep(async function transcodeVi
|
||||
const key = `files/${params.assetId}/tmp-embedding-chunks/frames-${params.startTime}-${params.endTime}-${ulid()}.tar`
|
||||
await s3Service.putObject(bucket, key, archiveData, archiveData.length, 'application/x-tar')
|
||||
|
||||
return { chunkKey: key }
|
||||
const sprite = await uploadVideoChunkSprite(
|
||||
params,
|
||||
() => composeVideoChunkSprite(frameFiles.map((file) => path.join(tmpDir, file))),
|
||||
signal,
|
||||
)
|
||||
return sprite ? { chunkKey: key, sprite } : { chunkKey: key }
|
||||
} catch (err) {
|
||||
if (err instanceof ApplicationFailure) {
|
||||
throw err
|
||||
@@ -1777,7 +1821,12 @@ export const transcodeVideoChunkActivity = defineStep(async function transcodeVi
|
||||
|
||||
await s3Service.putObject(bucket, key, chunkData, chunkData.length, 'video/mp4')
|
||||
|
||||
return { chunkKey: key }
|
||||
const sprite = await uploadVideoChunkSprite(
|
||||
params,
|
||||
() => sampleVideoChunkSprite(chunkTmp, params.endTime - params.startTime, signal),
|
||||
signal,
|
||||
)
|
||||
return sprite ? { chunkKey: key, sprite } : { chunkKey: key }
|
||||
} catch (err) {
|
||||
if (err instanceof ApplicationFailure) {
|
||||
throw err
|
||||
|
||||
@@ -73,11 +73,19 @@ describe('transcodeVideoChunksWorkflow', () => {
|
||||
status: 'processed',
|
||||
storageKey: { key: 'files/asset-video/raw.mp4', isArchived: false },
|
||||
mediaType: 'video/mp4',
|
||||
media: { duration: 110, sprite: { key: 'files/asset-video/sprite.webp' } },
|
||||
} as unknown as Awaited<ReturnType<typeof getAssetActivity>>)
|
||||
|
||||
const sprite = {
|
||||
key: 'files/asset-video/chunk-sprites/asset-video/video-0-60.webp',
|
||||
columns: 5,
|
||||
rows: 3,
|
||||
frameCount: 15,
|
||||
}
|
||||
mockActivities.transcodeVideoChunkActivity
|
||||
.mockResolvedValueOnce({
|
||||
chunkKey: 'files/asset-video/tmp-embedding-chunks/chunk-0-60.mp4',
|
||||
sprite,
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
chunkKey: 'files/asset-video/tmp-embedding-chunks/chunk-55-110.mp4',
|
||||
@@ -90,6 +98,7 @@ describe('transcodeVideoChunksWorkflow', () => {
|
||||
taskId: 'task-video-chunks',
|
||||
assetId: 'asset-video',
|
||||
assetKey: 'files/asset-video/proxy.mp4',
|
||||
originalKey: 'files/asset-video/raw.mp4',
|
||||
startTime: 0,
|
||||
endTime: 60,
|
||||
format: 'video',
|
||||
@@ -98,6 +107,7 @@ describe('transcodeVideoChunksWorkflow', () => {
|
||||
taskId: 'task-video-chunks',
|
||||
assetId: 'asset-video',
|
||||
assetKey: 'files/asset-video/proxy.mp4',
|
||||
originalKey: 'files/asset-video/raw.mp4',
|
||||
startTime: 55,
|
||||
endTime: 110,
|
||||
format: 'video',
|
||||
@@ -112,6 +122,7 @@ describe('transcodeVideoChunksWorkflow', () => {
|
||||
startTime: 0,
|
||||
endTime: 60,
|
||||
chunkKey: 'files/asset-video/tmp-embedding-chunks/chunk-0-60.mp4',
|
||||
sprite,
|
||||
},
|
||||
{
|
||||
startTime: 55,
|
||||
@@ -155,6 +166,7 @@ describe('transcodeVideoChunksWorkflow', () => {
|
||||
taskId: 'task-video-chunks',
|
||||
assetId: 'asset-video',
|
||||
assetKey: 'files/asset-video/proxy-720p.mp4',
|
||||
originalKey: 'files/asset-video/raw.mp4',
|
||||
startTime: 0,
|
||||
endTime: 30,
|
||||
format: 'frames',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { isFullVideoRange } from '@shumai/core/src/transcode/video-sprite'
|
||||
import type { WorkflowTask } from '@shumai/db'
|
||||
import { ApplicationFailure } from '@shumai/workflow-core'
|
||||
import { deleteS3ObjectActivity, transcodeVideoChunkActivity } from '../activities/transcode'
|
||||
@@ -8,6 +9,7 @@ export interface TranscodedVideoChunkItem {
|
||||
startTime: number
|
||||
endTime: number
|
||||
chunkKey: string
|
||||
sprite?: PrismaJson.VideoChunkSprite
|
||||
}
|
||||
|
||||
export async function transcodeVideoChunksWorkflow(task: WorkflowTask): Promise<void> {
|
||||
@@ -47,20 +49,21 @@ export async function transcodeVideoChunksWorkflow(task: WorkflowTask): Promise<
|
||||
|
||||
const chunks: TranscodedVideoChunkItem[] = []
|
||||
for (const range of payload.videoChunks.chunks) {
|
||||
const { chunkKey } = await transcodeVideoChunkActivity({
|
||||
const fullSpriteKey = isFullVideoRange(range.startTime, range.endTime, asset.media?.duration)
|
||||
? asset.media?.sprite?.key
|
||||
: undefined
|
||||
const chunk = await transcodeVideoChunkActivity({
|
||||
taskId: task.id,
|
||||
assetId: asset.id,
|
||||
assetKey: targetKey,
|
||||
originalKey: key,
|
||||
startTime: range.startTime,
|
||||
endTime: range.endTime,
|
||||
format: payload.videoChunks.format,
|
||||
...(fullSpriteKey && { fullSpriteKey }),
|
||||
})
|
||||
uploadedChunkKeys.push(chunkKey)
|
||||
chunks.push({
|
||||
startTime: range.startTime,
|
||||
endTime: range.endTime,
|
||||
chunkKey,
|
||||
})
|
||||
uploadedChunkKeys.push(chunk.chunkKey)
|
||||
chunks.push({ ...range, ...chunk })
|
||||
}
|
||||
|
||||
await completeTask(task.id, { chunks })
|
||||
|
||||
Reference in New Issue
Block a user