File size: 569 Bytes
2cae2a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { promises as fs } from "fs"

export async function readMp3FileToBase64(filePath: string): Promise<string> {
  try {
    // Read the file's content as a Buffer
    const fileBuffer = await fs.readFile(filePath);

    // Convert the buffer to a base64 string
    const base64 = fileBuffer.toString('base64');

    // Prefix the base64 string with the Data URI scheme for PNG images
    return `data:audio/mp3;base64,${base64}`;
  } catch (error) {
    // Handle errors (e.g., file not found, no permissions, etc.)
    console.error(error);
    throw error;
  }
}