Node.js Express FFMPEG WAV to OGG Audio Converter in Browser Using libvorbis Filter in Javascript
const express = require('express')
const multer = require('multer')
const {exec} = require('child_process')
const path = require('path')
const app = express()
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploads");
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname)); //Appending extension
},
});
var upload = multer({storage:storage}).single('file')
app.use(express.static("public/uploads"))
app.set('view engine', 'ejs')
app.get('/', (req, res) => {
res.render('index')
})
app.post('/wavtoogg', (req, res) => {
// upload the file from the html
outputfilename = Date.now() + "output.ogg"
upload(req, res, (err) => {
if (err) {
console.log(err)
} else {
console.log(req.file.path)
exec(`ffmpeg -i ${req.file.path} -c:a libvorbis -qscale:a 3 ${outputfilename}`, (err, stderr, stdout) => {
if (err) {
console.log(err)
} else {
console.log("conversion is successful")
// download the ogg file as attachment
res.download(outputfilename, () => {
console.log("file downloaded")
})
}
})
}
})
})
app.listen(5000, () => {
console.log('App is listening on port 5000')
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WAV TO OGG Audio Converter</title>
</head>
<body>
<form action="/wavtoogg" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept=".wav" required id="">
<input type="submit" value="Convert to OGG">
</form>
</body>
</html>