I am downloading an audio file in .oga format and saving it. Then I am trying to convert the file to .mp3 format, but the issue is the output file is always truncated and a maximum of 3 seconds. I have gone through the fluent-ffmpeg library and I can't seem to find what I'm doing wrong.
I have ffmpeg installed and I'm using the fluent-ffmpeg library. The downloaded .oga file doesn't seem to have any issues, the issue is after it gets converted to .mp3.
I also tried converting to .wav, and I faced the same issue.
Below is my current code:
const fs = require("fs");
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
const axios = require("axios");
const inputPath = __dirname + '/audio/input.oga';
const outputPath = __dirname + '/audio/output.mp3';
const saveVoiceMessage = async (url) => {
try {
const response = await axios({
method: 'GET',
url: url,
responseType: 'stream'
});
const inStream = fs.createWriteStream(inputPath);
await response.data.pipe(inStream);
} catch (error) {
console.error(error);
}
}
const convertToMp3 = async () => {
try {
const outStream = fs.createWriteStream(outputPath);
const inStream = fs.createReadStream(inputPath);
// I also tried using the inputPath directly, still didn't work
ffmpeg(inStream)
.toFormat("mp3")
.on('error', error => console.log(`Encoding Error: ${error.message}`))
.on('exit', () => console.log('Audio recorder exited'))
.on('close', () => console.log('Audio recorder closed'))
.on('end', () => console.log('Audio Transcoding succeeded !'))
.pipe(outStream, { end: true })
} catch (error) {
console.error(error);
}
}
const saveAndConvertToMp3 = async (url) => {
try {
await saveVoiceMessage(url);
await convertToMp3();
}
} catch (error) {
console.error(error);
}
}