File size: 2,216 Bytes
2cae2a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
import ffmpeg from "fluent-ffmpeg";
import { Buffer } from "node:buffer";

type ConvertAudioToWavParams = {
  input: string;
  outputAudioPath?: string;
  asBase64?: boolean;
};

export async function convertAudioToWav({
  input,
  outputAudioPath,
  asBase64 = false,
}: ConvertAudioToWavParams): Promise<string> {
  let inputAudioPath = input;

  // Check if the input is a base64 string
  if (input.startsWith("data:")) {
    const matches = input.match(/^data:audio\/(mp3|wav);base64,(.+)$/);

    if (!matches) {
      throw new Error("Invalid base64 audio data");
    }

    const inputBuffer = Buffer.from(matches[2], "base64");
    const inputFormat = matches[1]; // Either 'mp3' or 'wav'
    const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ffmpeg-input-"));
    inputAudioPath = path.join(tempDir, `temp.${inputFormat}`);

    // Write the base64 data to the temporary file
    await fs.writeFile(inputAudioPath, inputBuffer);
  } else {
    // Verify that the input file exists
    if (!(await fs.stat(inputAudioPath)).isFile()) {
      throw new Error(`Input audio file does not exist: ${inputAudioPath}`);
    }
  }

  // If no output path is provided, create a temporary file for the output
  if (!outputAudioPath) {
    const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ffmpeg-output-"));
    outputAudioPath = path.join(tempDir, `${path.parse(inputAudioPath).name}.wav`);
  }

  return new Promise((resolve, reject) => {
    ffmpeg(inputAudioPath)
      .toFormat("wav")
      .on("error", (err) => {
        reject(new Error(`Error converting audio to WAV: ${err.message}`));
      })
      .on("end", async () => {
        if (asBase64) {
          try {
            const audioBuffer = await fs.readFile(outputAudioPath);
            const audioBase64 = `data:audio/wav;base64,${audioBuffer.toString("base64")}`;
            resolve(audioBase64);
          } catch (error) {
            reject(new Error(`Error reading audio file: ${(error as Error).message}`));
          }
        } else {
          resolve(outputAudioPath);
        }
      })
      .save(outputAudioPath);
  });
}