mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
fix(video): transcribe archived videos and show progress (#560)
This commit is contained in:
@@ -4,12 +4,18 @@ import { tmpdir } from 'node:os'
|
||||
import { basename, join } from 'node:path'
|
||||
import { promisify } from 'node:util'
|
||||
import { initAgentWorkflows } from '@shumai/agent'
|
||||
import { assetService } from '@shumai/core/src/asset/asset'
|
||||
import {
|
||||
getArchiveStorageService,
|
||||
resetArchiveStorageServiceForTests,
|
||||
} from '@shumai/core/src/s3/archive-storage'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcriptService } from '@shumai/core/src/transcript/transcript'
|
||||
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { TaskQueueAgent, workflowService } from '@shumai/workflow-core'
|
||||
import { initTranscodeWorkflows } from '@shumai/transcode'
|
||||
import { TaskQueueAgent, TaskQueueArchive, workflowService } from '@shumai/workflow-core'
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
@@ -110,8 +116,12 @@ describe('Transcribe video workflow', () => {
|
||||
vi.stubEnv('S3_BUCKET', bucket)
|
||||
vi.stubEnv('EMBEDDING_PROVIDER', 'gemma-embedding-2-openai')
|
||||
vi.stubEnv('GEMMA_EMBEDDING_OPENAI_BASE_URL', `${provider.url}v1/embeddings/`)
|
||||
vi.stubEnv('ARCHIVE_STORAGE_BACKEND', 'local')
|
||||
vi.stubEnv('ARCHIVE_LOCAL_ROOT', join(directory, 'archive'))
|
||||
resetArchiveStorageServiceForTests()
|
||||
initAgentWorkflows()
|
||||
await workflowService.startWorkers(TaskQueueAgent)
|
||||
initTranscodeWorkflows()
|
||||
await workflowService.startWorkers([TaskQueueAgent, TaskQueueArchive])
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -126,10 +136,11 @@ describe('Transcribe video workflow', () => {
|
||||
await provider?.stop(true)
|
||||
await s3Service.deletePrefix(bucket, '')
|
||||
if (directory) await rm(directory, { recursive: true, force: true })
|
||||
resetArchiveStorageServiceForTests()
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
async function submitVideo(name: string, language: string | null) {
|
||||
async function createVideo(name: string, originalKey = 'video/unavailable-original.mkv') {
|
||||
const team = await prisma.team.create({ data: { name: 'Transcription E2E' } })
|
||||
const project = await prisma.project.create({ data: { name: 'Videos', teamId: team.id } })
|
||||
const fullProxy = { key: `video/${name}`, resolution: '480p', width: 64, height: 64 }
|
||||
@@ -138,14 +149,14 @@ describe('Transcribe video workflow', () => {
|
||||
s3Service.uploadFileToKey(join(directory, basename(key)), key, 'video/mp4'),
|
||||
),
|
||||
)
|
||||
const asset = await prisma.asset.create({
|
||||
return await prisma.asset.create({
|
||||
data: {
|
||||
name,
|
||||
type: 'file',
|
||||
status: 'processed',
|
||||
project: { connect: { id: project.id } },
|
||||
mediaType: 'video/x-matroska',
|
||||
storageKey: { create: { key: 'video/unavailable-original.mkv' } },
|
||||
storageKey: { create: { key: originalKey } },
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
videoTranscodes: [largerProxy, preview, fullProxy],
|
||||
@@ -160,14 +171,47 @@ describe('Transcribe video workflow', () => {
|
||||
},
|
||||
},
|
||||
})
|
||||
const result = await transcriptService.generate(asset.id, language)
|
||||
}
|
||||
|
||||
async function startTranscript(assetId: string, language: string | null) {
|
||||
const result = await transcriptService.generate(assetId, language)
|
||||
if (result.kind !== 'queued') throw new Error(`Unexpected generation result ${result.kind}`)
|
||||
return {
|
||||
assetId: asset.id,
|
||||
assetId,
|
||||
task: await prisma.workflowTask.findUniqueOrThrow({ where: { id: result.taskId } }),
|
||||
}
|
||||
}
|
||||
|
||||
async function submitVideo(name: string, language: string | null) {
|
||||
const asset = await createVideo(name)
|
||||
return await startTranscript(asset.id, language)
|
||||
}
|
||||
|
||||
it('generates and saves a transcript after the archive workflow moves the original', async () => {
|
||||
const originalKey = 'video/archive-original.mkv'
|
||||
await s3Service.uploadFileToKey(join(directory, 'audio.mkv'), originalKey, 'video/x-matroska')
|
||||
const asset = await createVideo('audio.mp4', originalKey)
|
||||
const { taskIds } = await assetService.archiveAssets([asset.id])
|
||||
expect(taskIds).toHaveLength(1)
|
||||
const archiveTask = await prisma.workflowTask.findUniqueOrThrow({ where: { id: taskIds[0] } })
|
||||
await workflowService.executeWait(archiveTask, 20_000)
|
||||
await expect(prisma.asset.findUnique({ where: { id: asset.id } })).resolves.toMatchObject({
|
||||
status: 'archived',
|
||||
})
|
||||
await expect(s3Service.headObject(bucket, originalKey)).rejects.toThrow('NoSuchKey')
|
||||
await expect(getArchiveStorageService().headObject(originalKey)).resolves.toMatchObject({
|
||||
size: expect.any(Number),
|
||||
})
|
||||
|
||||
const { task } = await startTranscript(asset.id, 'en')
|
||||
await workflowService.executeWait(task, 20_000)
|
||||
|
||||
expect(uploads).toHaveLength(1)
|
||||
await expect(transcriptService.getSavedTranscript(asset.id, 'en')).resolves.toMatchObject({
|
||||
segments: [{ startMs: 1012, endMs: 1875, text: 'Hello' }],
|
||||
})
|
||||
}, 45_000)
|
||||
|
||||
it('uses the smallest full proxy to send a timeline-aligned WAV and saves the detected language and segments after retrying a busy server', async () => {
|
||||
firstRequestBusy = true
|
||||
const { task, assetId } = await submitVideo('audio.mp4', null)
|
||||
|
||||
Reference in New Issue
Block a user