63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
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";
|
|
|
|
const client = new OpenAI({
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
baseURL: process.env.OPENAI_API_BASE_URL,
|
|
});
|
|
|
|
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",
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: prompt,
|
|
},
|
|
{
|
|
type: "input_audio",
|
|
input_audio: {
|
|
data: base64Audio,
|
|
format: "mp3",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
console.log(response.choices[0].message.content);
|
|
return response.choices[0].message.content;
|
|
}
|
|
|
|
export async function transcriptAweme(awemeId: string) {
|
|
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: 96 });
|
|
|
|
if (!audioDat || !audioDat.buffer) {
|
|
throw new Error("Failed to extract audio from video");
|
|
}
|
|
|
|
return await transcriptAudio(audioDat.buffer);
|
|
}
|