Skip to content

Instantly share code, notes, and snippets.

@lfelguetac
Last active July 29, 2022 21:44
Show Gist options
  • Save lfelguetac/c621085f7f3279eea22f62f7a9e246ae to your computer and use it in GitHub Desktop.
Save lfelguetac/c621085f7f3279eea22f62f7a9e246ae to your computer and use it in GitHub Desktop.
Esta rutina permite comprimir audios y cambiar el formato desde Node.
import ffmpeg from "fluent-ffmpeg";
import { promises as fs } from 'fs';
function convertAudioTo (srcPath:string, fileName:string): Promise<string> {
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
ffmpeg.setFfmpegPath(ffmpegPath);
const format = 'ogg';
const randomName = fileName.split('.')[0];
const newFile = './uploads/' + randomName + '.' + format;
// Format Codec Bit rate Sample Rate
// OGG OPUS 256 kpbs 16 kHz, mono
return new Promise((resolve, reject) => {
ffmpeg(srcPath)
.format(format)
.audioCodec('opus')
.audioBitrate('256k')
.audioChannels(1)
.on('error', async (error) => {
await fs.unlink(srcPath)
reject(error||'')
})
.on('end', async () => {
logger.info('Audio convertido en ' + newFile)
await fs.unlink(srcPath)
resolve(newFile)
})
.save(newFile)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment