Last active
July 29, 2022 21:44
-
-
Save lfelguetac/c621085f7f3279eea22f62f7a9e246ae to your computer and use it in GitHub Desktop.
Esta rutina permite comprimir audios y cambiar el formato desde Node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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