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
@@ -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)
}