feat(embedding): decouple video chunk transcode and embedding generation

This commit is contained in:
yiling
2026-06-18 11:54:30 +08:00
parent 9d1e8378f7
commit 46815736e2
6 changed files with 277 additions and 73 deletions
@@ -4,6 +4,11 @@ import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { transcodeService } from '@shumai/core'
import * as child_process from 'child_process'
vi.mock('child_process', () => ({
execFile: vi.fn(),
}))
import {
getMediaInfoActivity,
overlayAnnotationsActivity,
@@ -13,6 +18,8 @@ import {
downloadMediaToTmpActivity,
transcodeImageActivity,
generateSpriteActivity,
transcodeVideoChunkActivity,
deleteS3ObjectActivity,
} from './transcode'
vi.mock('@shumai/core/src/s3/s3', () => ({
@@ -22,6 +29,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
headObject: vi.fn(),
presign: vi.fn(),
downloadToFile: vi.fn(),
deleteObject: vi.fn(),
},
}))
@@ -365,4 +373,56 @@ describe('Transcode Activities', () => {
).rejects.toThrowError(/Overlay annotations failed/)
})
})
describe('Video Chunk Activities', () => {
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
;(child_process.execFile as any).mockImplementation(
(file: string, args: string[], cb: any) => {
cb(null, { stdout: '', stderr: '' })
},
)
const res = await transcodeVideoChunkActivity({
assetId: 'asset-abc',
filePath: '/tmp/full-video.mp4',
startTime: 10,
endTime: 25,
})
// Verify ffmpeg command args
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining([
'-y',
'-i',
'/tmp/full-video.mp4',
'-ss',
'10',
'-t',
'15',
'-c',
'copy',
]),
expect.any(Function),
)
// Verify S3 upload
expect(s3Service.putObject).toHaveBeenCalledWith(
'shumai',
expect.stringMatching(/^files\/asset-abc\/tmp-embedding-chunks\/chunk-10-25-.*\.mp4$/),
expect.any(Buffer),
expect.any(Number),
'video/mp4',
)
expect(res.chunkKey).toMatch(/^files\/asset-abc\/tmp-embedding-chunks\/chunk-10-25-.*\.mp4$/)
})
it('should delete S3 object successfully', async () => {
await deleteS3ObjectActivity({ key: 'files/test-key.mp4' })
expect(s3Service.deleteObject).toHaveBeenCalledWith('shumai', 'files/test-key.mp4')
})
})
})
@@ -5,6 +5,12 @@ import { metadataService } from '@shumai/core/src/metadata/metadata'
import { ApplicationFailure } from '@temporalio/activity'
import * as path from 'path'
import * as fs from 'fs'
import * as os from 'os'
import { execFile } from 'child_process'
import { promisify } from 'util'
import { ulid } from 'ulid'
const execFileAsync = promisify(execFile)
export interface GetMediaInfoActivityParams {
assetId: string
@@ -539,3 +545,59 @@ export async function createEmbeddingTaskIfEnabledActivity(
},
})
}
export interface TranscodeVideoChunkParams {
assetId: string
filePath: string
startTime: number
endTime: number
}
export async function transcodeVideoChunkActivity(
params: TranscodeVideoChunkParams,
): Promise<{ chunkKey: string }> {
const chunkTmp = path.join(os.tmpdir(), `video-chunk-${Date.now()}.mp4`)
try {
// Slice video segment using ffmpeg
await execFileAsync('ffmpeg', [
'-y',
'-i',
params.filePath,
'-ss',
params.startTime.toString(),
'-t',
(params.endTime - params.startTime).toString(),
'-c',
'copy',
chunkTmp,
])
const chunkData = fs.readFileSync(chunkTmp)
// Upload to S3
const bucket = process.env.S3_BUCKET || 'shumai'
const key = `files/${params.assetId}/tmp-embedding-chunks/chunk-${params.startTime}-${params.endTime}-${ulid()}.mp4`
await s3Service.putObject(bucket, key, chunkData, chunkData.length, 'video/mp4')
return { chunkKey: key }
} catch (err) {
throw ApplicationFailure.create({
message: `Failed to transcode video chunk for ${params.startTime}-${params.endTime}: ${err instanceof Error ? err.message : String(err)}`,
nonRetryable: false,
})
} finally {
if (fs.existsSync(chunkTmp)) {
try {
fs.unlinkSync(chunkTmp)
} catch (e) {
console.error('Failed to cleanup local chunk file:', e)
}
}
}
}
export async function deleteS3ObjectActivity(params: { key: string }): Promise<void> {
const bucket = process.env.S3_BUCKET || 'shumai'
await s3Service.deleteObject(bucket, params.key)
}