完成转录功能,中文脚本搜索,播放界面显示脚本
This commit is contained in:
+55
-15
@@ -4,45 +4,62 @@ 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";
|
||||
|
||||
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 response = await client.chat.completions.create({
|
||||
model: "gemini-2.5-flash-lite",
|
||||
const completion = await client.chat.completions.create({
|
||||
model: "gemini-2.5-flash",
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: prompt,
|
||||
},
|
||||
{ type: "text", text: prompt },
|
||||
{
|
||||
type: "input_audio",
|
||||
input_audio: {
|
||||
data: base64Audio,
|
||||
format: "mp3",
|
||||
},
|
||||
input_audio: { data: base64Audio, format: "mp3" },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
response_format: zodResponseFormat(SttSchema, "stt_result"),
|
||||
});
|
||||
|
||||
console.log(response.choices[0].message.content);
|
||||
return response.choices[0].message.content;
|
||||
const data = completion.choices?.[0]?.message
|
||||
if (!data) {
|
||||
throw new Error("No STT result returned from model");
|
||||
}
|
||||
const parsed = SttSchema.safeParse(data?.content).data;
|
||||
if (!parsed) {
|
||||
throw new Error("Failed to parse STT result with zod");
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export async function transcriptAweme(awemeId: string) {
|
||||
export async function transcriptAweme(awemeId: string): Promise<SttResult> {
|
||||
const aweme = await prisma.video.findUnique({
|
||||
where: { aweme_id: awemeId },
|
||||
});
|
||||
@@ -52,11 +69,34 @@ export async function transcriptAweme(awemeId: string) {
|
||||
const vPath = aweme.video_url
|
||||
const buffer = await downloadFile(vPath);
|
||||
|
||||
const audioDat = await extractAudio(buffer, { format: "mp3", bitrateKbps: 96 });
|
||||
const audioDat = await extractAudio(buffer, { format: "mp3", bitrateKbps: 128 });
|
||||
|
||||
if (!audioDat || !audioDat.buffer) {
|
||||
throw new Error("Failed to extract audio from video");
|
||||
}
|
||||
|
||||
return await transcriptAudio(audioDat.buffer);
|
||||
// 调用大模型生成转写结果
|
||||
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;
|
||||
}
|
||||
|
||||
+5
-27
@@ -1,42 +1,24 @@
|
||||
你将接收一段音频。请完成:
|
||||
A.语音活动检测(VAD)与声源分类;
|
||||
B.条件式处理:
|
||||
- 若包含可辨识的人类发言:** 进行转录 **(保留原语言,不翻译),并尽可能给出说话人分离与时间戳;
|
||||
- 若不包含人类发言:** 不转录 **,仅返回音频类型与简要描述。
|
||||
C.严格输出为下方 JSON,字段不得缺失或额外编造。听不清处用“[听不清]”。
|
||||
你将接收一段音频。请完成:你将接收一段音频。语音活动检测(VAD)与声源分类。
|
||||
|
||||
** 输出 JSON Schema(示例)**
|
||||
**输出 JSON Schema(示例)**
|
||||
|
||||
```json
|
||||
{
|
||||
"speech_detected": true,
|
||||
"language": "zh-CN",
|
||||
"audio_type": null,
|
||||
"background": "music | ambience | none | unknown",
|
||||
"transcript": [
|
||||
{
|
||||
"start": 0.00,
|
||||
"end": 3.42,
|
||||
"text": "大家好,我是……"
|
||||
},
|
||||
{
|
||||
"start": 3.50,
|
||||
"end": 6.10,
|
||||
"text": "欢迎来到今天的节目。"
|
||||
}
|
||||
],
|
||||
"transcript": ["大家好,我是xxx", "欢迎来到今天的视频", "今天我们来聊聊AI的未来"],
|
||||
"non_speech_summary": null,
|
||||
}
|
||||
```
|
||||
>
|
||||
** 当无发言时返回:**
|
||||
|
||||
**当无发言时返回:**
|
||||
|
||||
```json
|
||||
{
|
||||
"speech_detected": false,
|
||||
"language": null,
|
||||
"audio_type": "music | ambience | animal | mechanical | other",
|
||||
"background": "none",
|
||||
"transcript": [],
|
||||
"non_speech_summary": "示例:纯音乐-钢琴独奏,节奏舒缓;或 环境声-雨声伴随雷鸣。",
|
||||
}
|
||||
@@ -44,8 +26,4 @@ C.严格输出为下方 JSON,字段不得缺失或额外编造。听不清处
|
||||
|
||||
** 规则补充 **
|
||||
|
||||
* 只要存在可理解的人类发言(即便有音乐 / 噪声),就执行转录,并在 `background` 标注“music / ambience”。
|
||||
* 不要将唱词 / 哼唱视为“发言”;若仅有人声演唱且无口语发言,视为 ** 音乐 **。
|
||||
* 不要臆测未听清内容;不要添加与音频无关的信息。
|
||||
* 时间单位统一为秒,保留两位小数。
|
||||
* 允许`language` 为多标签(如 "zh-CN,en")或为 `null`(无发言时)。
|
||||
|
||||
Reference in New Issue
Block a user