Build a file conversion API using FFmpeg, Docker, and Node.js

FFmpeg

Docker

Node.js

With that said, let’s hop to it.

mkdir file-conversion-api && cd file-conversion-api && npm init
touch Dockerfile server.js setup-ffmpeg.sh
#!/usr/bin/env bash
echo 'deb http://www.deb-multimedia.org jessie main non-free' >> /etc/apt/sources.list
echo 'deb-src http://www.deb-multimedia.org jessie main non-free' >> /etc/apt/sources.list
apt-get update
apt-get install -y --force-yes deb-multimedia-keyring
apt-get remove -y --force-yes ffmpeg
apt-get install -y --force-yes build-essential libmp3lame-dev libvorbis-dev libtheora-dev libspeex-dev yasm pkg-config libfaac-dev libopenjpeg-dev libx264-dev libav-tools ffmpeg
chmod +x setup-ffmpeg.sh
FROM node:carbonVOLUME ["/root"]ADD setup-ffmpeg.sh /root
RUN /root/setup-ffmpeg.sh
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8080CMD [ "npm", "start" ]

Saving the best for last

Roll up your sleeves

'use strict';var express = require('express');
var cors = require('cors')
var ffmpeg = require('fluent-ffmpeg');
var PORT = 8080;
var HOST = '0.0.0.0';
const app = express();
app.use(cors());
app.get('/', (req, res) => {
res.contentType('audio/mp3');
res.attachment('myfile.mp3');
var pathToAudio = 'https://dl.dropbox.com/s/pc7qp4wrf46t9op/test-clip.webm?dl=0';
ffmpeg(pathToAudio)
.toFormat('mp3')
.on('end', function(err) {
console.log('done!')
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
})
.pipe(res, {end: true})
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
npm i --save express cors fluent-ffmpeg
"scripts": {
"build": "docker build -t alien35/node-web-app .",
"startd": "docker run -p 49160:8080 -d alien35/node-web-app",
"open": "open http://localhost:49160",
"go": "npm run build && npm run startd && docker ps"
},

Recommendations for production use

That’s it folks

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store