fix(video): transcribe archived videos and show progress (#560)

This commit is contained in:
Yiling-J
2026-12-04 15:37:58 +08:00
committed by GitHub
parent d3899d1257
commit 72a7f68c92
21 changed files with 408 additions and 171 deletions
@@ -1,4 +1,4 @@
import { prisma, WorkflowTaskType } from '@shumai/db'
import { prisma, WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { transcriptionErrors } from '@shumai/dtos'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -177,6 +177,85 @@ describe('TranscriptService', () => {
expect(await prisma.workflowTask.count({ where: { assetId } })).toBe(0)
})
it.each(['archiving', 'archived', 'restoring'] as const)(
'queues generation from a full proxy while the video is %s',
async (status) => {
await prisma.asset.update({ where: { id: assetId }, data: { status } })
await expect(transcriptService.generate(assetId, 'en')).resolves.toMatchObject({
kind: 'queued',
})
},
)
it('lists each live project file with a pending or running transcription once', async () => {
const project = await prisma.project.findUniqueOrThrow({ where: { id: projectId } })
const otherProject = await prisma.project.create({
data: { name: 'Another project', teamId: project.teamId },
})
const [running, finished, deleted, external, transcoding] = await Promise.all(
[
{ projectId, name: 'Running video' },
{ projectId, name: 'Finished video' },
{ projectId, name: 'Deleted video', isDeleted: true },
{ projectId: otherProject.id, name: 'External video' },
{ projectId, name: 'Transcoding video' },
].map((file) =>
prisma.asset.create({ data: { ...file, type: 'file', status: 'processed' } }),
),
)
await expect(transcriptService.listActiveAssetIds(projectId)).resolves.toEqual([])
await prisma.workflowTask.createMany({
data: [
{ assetId, status: WorkflowTaskStatus.pending },
{ assetId, status: WorkflowTaskStatus.processing },
{ assetId, status: WorkflowTaskStatus.failed },
{ assetId: running.id, status: WorkflowTaskStatus.processing },
{ assetId: finished.id, status: WorkflowTaskStatus.completed },
{ assetId: finished.id, status: WorkflowTaskStatus.failed },
{ assetId: deleted.id, status: WorkflowTaskStatus.pending },
{ assetId: external.id, status: WorkflowTaskStatus.processing },
{
assetId: transcoding.id,
status: WorkflowTaskStatus.pending,
type: WorkflowTaskType.transcode_video,
},
].map((job) => ({ type: WorkflowTaskType.ai_transcription, ...job })),
})
await expect(transcriptService.listActiveAssetIds(projectId)).resolves.toEqual(
[assetId, running.id].sort().reverse(),
)
await expect(transcriptService.listActiveAssetIds(otherProject.id)).resolves.toEqual([
external.id,
])
})
it('rejects an archived video without a full proxy', async () => {
await prisma.asset.update({
where: { id: assetId },
data: { status: 'archived', media: { ...media, videoTranscodes: [] } },
})
await expect(transcriptService.generate(assetId, 'en')).rejects.toMatchObject({
status: 400,
message: transcriptionErrors.noProxy,
})
expect(await prisma.workflowTask.count({ where: { assetId } })).toBe(0)
})
it.each(['archiving', 'archived', 'restoring'] as const)(
'saves a transcript when the video has become %s while the job runs',
async (status) => {
const taskId = await generateAndStart('en')
await prisma.asset.update({ where: { id: assetId }, data: { status } })
await expect(
transcriptService.saveResult({ taskId, assetId, model, language: 'en', segments }),
).resolves.toBe(true)
await expect(transcriptService.getSavedTranscript(assetId, 'en')).resolves.toMatchObject({
segments,
})
},
)
it('rejects an audio-only proxy even when the file was uploaded with a video MIME type', async () => {
await prisma.asset.update({
where: { id: assetId },
+15 -2
View File
@@ -9,6 +9,7 @@ import {
import {
selectTranscriptVideoProxyKey,
transcriptionErrors,
transcriptVideoStatuses,
type GenerateTranscriptResult,
type SavedTranscript,
type SavedTranscriptLanguages,
@@ -96,7 +97,7 @@ export class TranscriptService {
where: {
id: assetId,
type: 'file',
status: 'processed',
status: { in: [...transcriptVideoStatuses] },
isDeleted: false,
mediaType: { startsWith: 'video/' },
},
@@ -173,6 +174,18 @@ export class TranscriptService {
}
}
async listActiveAssetIds(projectId: string): Promise<string[]> {
const assets = await prisma.$queryRaw<{ assetId: string }[]>`
SELECT DISTINCT task.asset_id AS "assetId"
FROM workflow_tasks task
JOIN assets asset ON asset.id = task.asset_id
WHERE asset.project_id = ${projectId} AND NOT asset.is_deleted AND asset.type = 'file'
AND task.type = ${type}::"WorkflowTaskType" AND task.status IN ('pending', 'processing')
ORDER BY task.asset_id DESC
`
return assets.map(({ assetId }) => assetId)
}
async saveResult(params: {
taskId: string
assetId: string
@@ -184,7 +197,7 @@ export class TranscriptService {
return await prisma.$transaction(async (tx) => {
const [asset] = await tx.$queryRaw<{ id: string }[]>`
SELECT id FROM assets
WHERE id = ${assetId} AND NOT is_deleted AND status = 'processed'
WHERE id = ${assetId} AND NOT is_deleted AND status::text IN (${Prisma.join(transcriptVideoStatuses)})
FOR UPDATE
`
if (!asset) return false