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




With that said, let’s hop to it.

mkdir file-conversion-api && cd file-conversion-api && npm init
touch Dockerfile server.js
#!/usr/bin/env bash
echo 'deb jessie main non-free' >> /etc/apt/sources.list
echo 'deb-src 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
FROM node:carbonVOLUME ["/root"]ADD /root
RUN /root/
# 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 = '';
const app = express();
app.get('/', (req, res) => {
var pathToAudio = '';
.on('end', function(err) {
.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