Initial commit
This commit is contained in:
40
services/tts.ts
Normal file
40
services/tts.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import {
|
||||
PollyClient,
|
||||
SynthesizeSpeechCommand,
|
||||
} from "@aws-sdk/client-polly";
|
||||
|
||||
const polly = new PollyClient({ region: "ap-northeast-1" });
|
||||
|
||||
export async function generateTTS(
|
||||
itemId: string,
|
||||
scriptText: string
|
||||
): Promise<string> {
|
||||
const params = {
|
||||
OutputFormat: "mp3",
|
||||
Text: scriptText,
|
||||
VoiceId: "Mizuki",
|
||||
LanguageCode: "ja-JP",
|
||||
};
|
||||
const command = new SynthesizeSpeechCommand(params);
|
||||
const response = await polly.send(command);
|
||||
|
||||
if (!response.AudioStream) {
|
||||
throw new Error("TTSのAudioStreamが空です");
|
||||
}
|
||||
|
||||
const outputDir = path.join(__dirname, "../static/podcast_audio");
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
}
|
||||
const filePath = path.join(outputDir, \`\${itemId}.mp3\`);
|
||||
|
||||
const chunks: Uint8Array[] = [];
|
||||
for await (const chunk of response.AudioStream as any) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
const buffer = Buffer.concat(chunks);
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
return filePath;
|
||||
}
|
Reference in New Issue
Block a user