feat: convert wav to mp3 and save
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
import fs from "fs";
 | 
			
		||||
import path from "path";
 | 
			
		||||
import ffmpeg from "fluent-ffmpeg";
 | 
			
		||||
 | 
			
		||||
// VOICEVOX APIの設定
 | 
			
		||||
const VOICEVOX_HOST = import.meta.env["VOICEVOX_HOST"];
 | 
			
		||||
@@ -61,10 +62,29 @@ export async function generateTTS(
 | 
			
		||||
    fs.mkdirSync(outputDir, { recursive: true });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const filePath = path.resolve(outputDir, `${itemId}.wav`); // Use the provided filename directly
 | 
			
		||||
  console.log(`音声ファイル保存開始: ${filePath}`);
 | 
			
		||||
  fs.writeFileSync(filePath, audioBuffer);
 | 
			
		||||
  console.log(`音声ファイル保存完了: ${filePath}`);
 | 
			
		||||
  const wavFilePath = path.resolve(outputDir, `${itemId}.wav`);
 | 
			
		||||
  const mp3FilePath = path.resolve(outputDir, `${itemId}.mp3`);
 | 
			
		||||
 | 
			
		||||
  return path.basename(filePath);
 | 
			
		||||
  console.log(`WAVファイル保存開始: ${wavFilePath}`);
 | 
			
		||||
  fs.writeFileSync(wavFilePath, audioBuffer);
 | 
			
		||||
  console.log(`WAVファイル保存完了: ${wavFilePath}`);
 | 
			
		||||
 | 
			
		||||
  console.log(`MP3変換開始: ${wavFilePath} -> ${mp3FilePath}`);
 | 
			
		||||
  await new Promise<void>((resolve, reject) => {
 | 
			
		||||
    ffmpeg(wavFilePath)
 | 
			
		||||
      .audioCodec("libmp3lame")
 | 
			
		||||
      .format("mp3")
 | 
			
		||||
      .on("end", () => {
 | 
			
		||||
        console.log(`MP3変換完了: ${mp3FilePath}`);
 | 
			
		||||
        fs.unlinkSync(wavFilePath); // WAVファイルを削除
 | 
			
		||||
        resolve();
 | 
			
		||||
      })
 | 
			
		||||
      .on("error", (err) => {
 | 
			
		||||
        console.error(`MP3変換エラー: ${err.message}`);
 | 
			
		||||
        reject(err);
 | 
			
		||||
      })
 | 
			
		||||
      .save(mp3FilePath);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  return path.basename(mp3FilePath);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user