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
+5 -17
View File
@@ -1,26 +1,23 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getFileUrl } from "@/lib/minio";
import { prisma } from "@/lib/prisma";
import { transcriptionState } from "@/lib/stt/types";
export async function GET() {
try {
const videos = await prisma.video.findMany({
orderBy: { created_at: "desc" },
take: 1000,
include: {
author: {
select: {
nickname: true,
},
},
transcriptionTask: true,
transcript: {
select: {
id: true,
speech_detected: true,
language: true,
audio_type: true,
transcript: true,
non_speech_summary: true,
},
},
},
@@ -29,21 +26,12 @@ export async function GET() {
const formattedVideos = videos.map((v) => ({
aweme_id: v.aweme_id,
desc: v.desc,
cover_url: getFileUrl(v.cover_url ?? ""),
cover_url: v.cover_url ? getFileUrl(v.cover_url) : "",
duration_ms: v.duration_ms,
author: {
nickname: v.author.nickname,
},
transcript: v.transcript
? {
id: v.transcript.id,
speech_detected: v.transcript.speech_detected,
language: v.transcript.language,
audio_type: v.transcript.audio_type,
transcript: v.transcript.transcript,
non_speech_summary: v.transcript.non_speech_summary,
}
: null,
transcription: transcriptionState(v.transcript, v.transcriptionTask),
}));
return NextResponse.json({
+6 -2
View File
@@ -21,7 +21,7 @@ import {
releaseIsolatedContext,
} from "@/app/api/fetcher/browser";
import { extractFirstFrame } from "@/app/api/media";
import { transcriptAweme } from "../stt";
import { enqueueTranscription } from "@/lib/stt/tasks";
const DETAIL_PATH = "/aweme/v1/web/aweme/detail/";
const COMMENT_PATH = "/aweme/v1/web/comment/list/";
@@ -429,7 +429,11 @@ export async function scrapeDouyin(url: string) {
fps ?? undefined,
);
console.log(chalk.green.bold("✓ 视频作品保存成功"));
transcriptAweme(detail.aweme_detail.aweme_id).catch((e) => {}); // 异步转写,不阻塞主流程
try {
await enqueueTranscription(detail.aweme_detail.aweme_id);
} catch {
console.error("视频已保存,但转写任务入队失败,可在后台批量补转");
}
return { type: "video", ...saved };
} else {
throw new ScrapeError(
+16 -6
View File
@@ -111,17 +111,17 @@ export async function getVideoDuration(
}
/**
* 使用 ffmpeg 将视频二进制转换为音频,返回音频 Buffer
* 使用 ffmpeg 将视频 Buffer 或本地文件转换为音频,返回音频 Buffer
*
* 默认输出 mp3,可通过 opts.format 指定 'mp3' | 'aac' | 'wav'
* - mp3: 使用 libmp3lame,contentType 为 'audio/mpeg'
* - mp3: 使用 libmp3lame,16 kHz 单声道,contentType 为 'audio/mpeg'
* - aac: 输出 m4a 容器(AAC 编码),contentType 为 'audio/mp4'
* - wav: PCM 16-bit,44100Hz,双声道,contentType 为 'audio/wav'
*
* 若系统未安装 ffmpeg 或未在 PATH 中,返回 null 并给出提示。
*/
export async function extractAudio(
videoBuffer: Buffer,
videoBuffer: Buffer | string,
opts?: { format?: "mp3" | "aac" | "wav"; bitrateKbps?: number },
): Promise<{ buffer: Buffer; contentType: string; ext: string } | null> {
const ffmpegCmd = process.env.FFMPEG_PATH || "ffmpeg";
@@ -129,6 +129,7 @@ export async function extractAudio(
const base = `dy_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
const inPath = path.join(tmpDir, `${base}.mp4`);
const inputPath = typeof videoBuffer === "string" ? videoBuffer : inPath;
const format = opts?.format ?? "mp3";
const bitrate = Math.max(32, Math.min(512, opts?.bitrateKbps ?? 192)); // 安全范围 32~512 kbps
@@ -174,19 +175,28 @@ export async function extractAudio(
}
try {
await fs.writeFile(inPath, videoBuffer);
if (typeof videoBuffer !== "string") await fs.writeFile(inPath, videoBuffer);
const args = [
"-hide_banner",
"-loglevel",
"error",
"-i",
inPath,
inputPath,
"-map",
"0:a:0",
"-ac",
"1",
"-ar",
"16000",
"-vn", // 丢弃视频流
...codecArgs,
"-y",
outPath,
];
await execFileAsync(ffmpegCmd, args, { windowsHide: true });
await execFileAsync(ffmpegCmd, args, {
windowsHide: true,
timeout: 60 * 60 * 1000,
});
const audio = await fs.readFile(outPath);
return { buffer: audio, contentType, ext };
} catch (e: any) {
+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);
}