feat: 将视频转写重构为纯文字异步语音识别

This commit is contained in:
2026-10-03 21:58:06 +08:00
parent 040273586b
commit 6f0e38054b
34 changed files with 2283 additions and 1771 deletions
+22
View File
@@ -0,0 +1,22 @@
import { NextResponse } from "next/server";
import { getAsrConfig } from "@/lib/stt/dashscope";
import { enqueueMissingTranscriptions } from "@/lib/stt/tasks";
export const runtime = "nodejs";
export async function POST() {
try {
getAsrConfig();
} catch (error) {
return NextResponse.json(
{ error: (error as Error).message },
{ status: 503 },
);
}
try {
return NextResponse.json(
{ queued: await enqueueMissingTranscriptions() },
{ status: 202 },
);
} catch {
return NextResponse.json({ error: "批量任务入队失败" }, { status: 500 });
}
}
-113
View File
@@ -1,113 +0,0 @@
import fs from "fs";
import OpenAI from "openai";
import prompt from "./prompt.md";
import { prisma } from "@/lib/prisma";
import { downloadFile } from "@/lib/minio";
import { extractAudio } from "../media";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
import { ProxyAgent, setGlobalDispatcher } from "undici";
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
if (proxy) {
setGlobalDispatcher(new ProxyAgent(proxy));
}
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_API_BASE_URL,
});
// Zod schema aligned with prompt.md
export const SttSchema = z
.object({
speech_detected: z.boolean(),
language: z.string().min(1).nullable(),
audio_type: z.string().nullable(),
transcript: z.array(z.string()),
non_speech_summary: z.string().nullable(),
})
.strict();
export type SttResult = z.infer<typeof SttSchema>;
async function transcriptAudio(audio: Buffer | string) {
if (typeof audio === "string") {
audio = fs.readFileSync(audio);
}
const base64Audio = Buffer.from(audio).toString("base64");
const completion = await client.chat.completions.create({
model: "gemini-2.5-flash",
messages: [
{
role: "user",
content: [
{ type: "text", text: prompt },
{
type: "input_audio",
input_audio: { data: base64Audio, format: "mp3" },
},
],
},
],
response_format: zodResponseFormat(SttSchema, "stt_result"),
});
const data = completion.choices?.[0]?.message;
console.log("转写结果", data.content);
if (!data || !data.content) {
throw new Error("No STT result returned from model");
}
try {
return JSON.parse(data.content) as SttResult;
} catch (e) {
throw new Error("Failed to parse STT result JSON");
}
}
export async function transcriptAweme(awemeId: string): Promise<SttResult> {
const aweme = await prisma.video.findUnique({
where: { aweme_id: awemeId },
});
if (!aweme) {
throw new Error("Aweme not found or aweme is not a video post");
}
const vPath = aweme.video_url;
const buffer = await downloadFile(vPath);
const audioDat = await extractAudio(buffer, {
format: "mp3",
bitrateKbps: 128,
});
if (!audioDat || !audioDat.buffer) {
throw new Error("Failed to extract audio from video");
}
// 调用大模型生成转写结果
const result = await transcriptAudio(audioDat.buffer);
// 将转写结果持久化到数据库
await prisma.videoTranscript.upsert({
where: { videoId: awemeId },
update: {
speech_detected: result.speech_detected,
language: result.language,
audio_type: result.audio_type,
transcript: result.transcript,
non_speech_summary: result.non_speech_summary,
},
create: {
videoId: awemeId,
speech_detected: result.speech_detected,
language: result.language,
audio_type: result.audio_type,
transcript: result.transcript,
non_speech_summary: result.non_speech_summary,
},
});
return result;
}
-29
View File
@@ -1,29 +0,0 @@
你将接收一段音频。请完成:你将接收一段音频。语音活动检测(VAD)与声源分类。
**输出 JSON Schema(示例)**
```json
{
"speech_detected": true,
"language": "zh-CN",
"audio_type": null,
"transcript": ["大家好,我是xxx", "欢迎来到今天的视频", "今天我们来聊聊AI的未来"],
"non_speech_summary": null,
}
```
**当无发言时返回:**
```json
{
"speech_detected": false,
"language": null,
"audio_type": "music | ambience | animal | mechanical | other",
"transcript": [],
"non_speech_summary": "示例:纯音乐-钢琴独奏,节奏舒缓;或 环境声-雨声伴随雷鸣。",
}
```
** 规则补充 **
* 允许`language` 为多标签(如 "zh-CN,en")或为 `null`(无发言时)。
+29 -19
View File
@@ -1,23 +1,33 @@
import { type NextRequest, NextResponse } from "next/server";
import { enqueueTranscription, readTranscription } from "@/lib/stt/tasks";
import { isActive } from "@/lib/stt/types";
export const runtime = "nodejs";
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import type { FeedItem, FeedResponse } from "@/app/types/feed";
import { getFileUrl } from "@/lib/minio";
import { transcriptAweme } from ".";
// Contract
// Inputs: search params { before?: ISOString, limit?: number }
// Output: { items: FeedItem[], nextCursor: ISOString | null }
export const dynamic = "force-dynamic";
const validId = (id: unknown): id is string =>
typeof id === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(id);
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const awemeId = searchParams.get("awemeId");
if (!awemeId) {
return NextResponse.json(
{ error: "Missing awemeId parameter" },
{ status: 400 },
);
const id = req.nextUrl.searchParams.get("awemeId");
if (!validId(id))
return NextResponse.json({ error: "需要有效的 awemeId" }, { status: 400 });
try {
const state = await readTranscription(id);
return state
? NextResponse.json(state)
: NextResponse.json({ error: "视频不存在" }, { status: 404 });
} catch {
return NextResponse.json({ error: "读取转写状态失败" }, { status: 500 });
}
}
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
if (!validId(body?.awemeId))
return NextResponse.json({ error: "需要有效的 awemeId" }, { status: 400 });
try {
const state = await enqueueTranscription(body.awemeId, true);
return state
? NextResponse.json(state, { status: isActive(state.status) ? 202 : 200 })
: NextResponse.json({ error: "视频不存在" }, { status: 404 });
} catch {
return NextResponse.json({ error: "转写任务入队失败" }, { status: 500 });
}
const script = await transcriptAweme(awemeId);
return NextResponse.json(script);
}