A TypeScript utility for converting audio to MP3 format in the browser.
npm install ts-audio-to-mp3
import { convertToMP3 } from 'ts-audio-to-mp3';
// Example with File input
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
try {
const mp3Blob = await convertToMP3(file, {
sampleRate: 44100, // optional, default: 44100
bitRate: 128 // optional, default: 128
});
// Use the MP3 blob as needed
const url = URL.createObjectURL(mp3Blob);
const audio = new Audio(url);
audio.play();
} catch (error) {
console.error('Error converting to MP3:', error);
}
});
Converts an audio file or blob to MP3 format.
-
audioData
: The input audio data as a Blob or File -
options
: (Optional) Conversion options-
sampleRate
: Sample rate in Hz (default: 44100) -
bitRate
: Bit rate in kbps (default: 128)
-
Returns a Promise that resolves with a Blob containing the MP3 data.
MIT