import ffmpeg from 'fluent-ffmpeg';
/**
* Extracts audio from a video file and saves it as an MP3 file.
* @param videoFilePath - The path to the video file.
* @param outputAudioPath - The path where the extracted audio file should be saved.
*/
function extractAudioFromVideo(videoFilePath: string, outputAudioPath: string): Promise<void> {
return new Promise((resolve, reject) => {
ffmpeg(videoFilePath)
.outputFormat('mp3') // Set the output format to mp3
.on('end', () => {
console.log(`Extraction completed: ${outputAudioPath}`);
resolve();
})
.on('error', (err) => {
console.error(`Error extracting audio: ${err.message}`);
reject(err);
})
.save(outputAudioPath); // Specify the output file path
});
}
// Example usage
const videoFilePath = 'path/to/your/video.mp4';
const outputAudioPath = 'path/to/output/audio.mp3';
extractAudioFromVideo(videoFilePath, outputAudioPath)
.then(() => console.log('Audio extraction successful.'))
.catch((err) => console.error(err));