feat(video): show saved transcripts in the player

This commit is contained in:
yiling
2026-12-03 11:19:22 +08:00
parent e806435e49
commit 3952928b88
26 changed files with 1103 additions and 33 deletions
@@ -55,6 +55,65 @@ describe('TranscriptService', () => {
return result.taskId
}
it('lists only the asset’s saved languages and reads every segment in position order', async () => {
const otherAsset = await prisma.asset.create({
data: { name: 'Another video', type: 'file', status: 'processed', projectId },
})
const save = (id: string, language: string) =>
prisma.assetTranscript.create({
data: {
assetId: id,
language,
model,
workflowTaskId: 'test-task',
segments: {
create: [
{ position: 1, startMs: 2000, endMs: 3000, text: 'Second' },
{ position: 0, startMs: 125, endMs: 1425, text: 'First' },
],
},
},
include: { segments: true },
})
await expect(transcriptService.listSavedLanguages(assetId)).resolves.toEqual({ languages: [] })
const english = await save(assetId, 'en')
await save(assetId, 'zh')
await save(otherAsset.id, 'ko')
vi.stubEnv('EMBEDDING_PROVIDER', 'gemini-embedding-2')
const savedLanguages = await transcriptService.listSavedLanguages(assetId)
expect(savedLanguages.languages.sort()).toEqual(['en', 'zh'])
const transcript = await transcriptService.getSavedTranscript(assetId, 'en')
expect(transcript).toEqual({
id: english.id,
language: 'en',
segments: [
{
id: english.segments.find(({ position }) => position === 0)?.id,
startMs: 125,
endMs: 1425,
text: 'First',
},
{
id: english.segments.find(({ position }) => position === 1)?.id,
startMs: 2000,
endMs: 3000,
text: 'Second',
},
],
})
await expect(transcriptService.getSavedTranscript(assetId, 'ko')).rejects.toMatchObject({
status: 404,
})
await prisma.asset.update({ where: { id: assetId }, data: { isDeleted: true } })
await expect(transcriptService.listSavedLanguages(assetId)).resolves.toEqual({ languages: [] })
await expect(transcriptService.getSavedTranscript(assetId, 'en')).rejects.toMatchObject({
status: 404,
})
expect(fetch).not.toHaveBeenCalled()
})
it('validates provider capability, language and asset eligibility before queuing', async () => {
await expect(transcriptService.listLanguages()).resolves.toMatchObject({ model })
await expect(transcriptService.generate(assetId, 'xx')).rejects.toMatchObject({ status: 400 })
@@ -9,6 +9,8 @@ import {
import {
transcriptionErrors,
type GenerateTranscriptResult,
type SavedTranscript,
type SavedTranscriptLanguages,
type TranscriptJobStatus,
} from '@shumai/dtos'
import { HTTPException } from 'hono/http-exception'
@@ -31,6 +33,30 @@ export class TranscriptService {
}
}
async listSavedLanguages(assetId: string): Promise<SavedTranscriptLanguages> {
const transcripts = await prisma.assetTranscript.findMany({
where: { assetId, asset: { isDeleted: false } },
orderBy: { id: 'desc' },
select: { language: true },
})
return { languages: transcripts.map(({ language }) => language) }
}
async getSavedTranscript(assetId: string, language: string): Promise<SavedTranscript> {
const transcript = await prisma.assetTranscript.findUnique({
where: { assetIdLanguage: { assetId, language }, asset: { isDeleted: false } },
select: {
...transcriptSelection,
segments: {
orderBy: { position: 'asc' },
select: { id: true, startMs: true, endMs: true, text: true },
},
},
})
if (!transcript) throw new HTTPException(404, { message: 'Transcript not found' })
return transcript
}
async generate(assetId: string, language: string | null): Promise<GenerateTranscriptResult> {
this.requireEnabled()
const saved = await this.findSaved(prisma, assetId, language)