<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Borisa Krstic on Medium]]></title>
        <description><![CDATA[Stories by Borisa Krstic on Medium]]></description>
        <link>https://medium.com/@borisa?source=rss-f096ba9e42ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uKuogvU-mRiNKdxrd5zX4w.jpeg</url>
            <title>Stories by Borisa Krstic on Medium</title>
            <link>https://medium.com/@borisa?source=rss-f096ba9e42ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:14:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@borisa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Video compression API — FFmpeg,  Docker,  Nodejs/Express]]></title>
            <link>https://medium.com/@borisa/video-compression-api-ffmpeg-docker-nodejs-express-60e5b3b732f1?source=rss-f096ba9e42ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/60e5b3b732f1</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[compression]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ffmpeg]]></category>
            <dc:creator><![CDATA[Borisa Krstic]]></dc:creator>
            <pubDate>Sun, 12 Jun 2022 13:23:25 GMT</pubDate>
            <atom:updated>2022-06-12T17:00:46.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KhhIb1aDeQjSBTpGXE8tjA.png" /></figure><h3>Video compression API — NodeJS/Express, Docker, FFmpeg</h3><p><em>Create an API that compresses videos up to 10x while maintaining maximum quality.</em></p><blockquote><strong><em>PROBLEM</em></strong></blockquote><p>While working on a social media app primarily used for content sharing we came across the problem with video playback for user-uploaded videos. If the user is only on a bit slower internet it would take a long time for the video to event load bit by bit. We had to implement some video compression solution for it. From my Javascript background, I assumed it would be as simple as <strong>npm install “some video compressor”</strong>, it turned out to be a much more complex process and this is how we managed to do it.</p><p>Our solution was inspired by a couple of other articles but we had to adjust all solutions for the production environment and simplicity. <a href="https://alexanderleon.medium.com/build-a-file-conversion-api-using-ffmpeg-docker-and-node-js-f08a2c412aac">Article</a>.</p><blockquote><strong><em>SOLUTION DESCRIPTION</em></strong></blockquote><p>In our research, we figured that the backend app (<a href="https://expressjs.com/"><strong>NodeJS/Express</strong></a>) will have to use some sort of already existing processing software, the most popular software we came across was <a href="https://ffmpeg.org/"><strong>FFmpeg</strong></a><strong>. </strong>The next question was how to make that software available for all environments without much hassle, the obvious answer was containerizing the whole app using <a href="https://www.docker.com/"><strong>Docker</strong></a><strong>. </strong>Video processing is a very CPU-heavy operation and we all know NodeJS and its single-threaded nature so another thing that had to be done was to offload work to another process (<a href="https://nodejs.org/api/child_process.html"><strong>child_process</strong></a>).</p><blockquote><strong><em>TECHNOLOGIES and PACKAGES</em></strong></blockquote><p><a href="https://nodejs.org/en/"><strong>NodeJS</strong></a> - is a JavaScript runtime built on <a href="https://v8.dev/">Chrome’s V8 JavaScript engine</a>.</p><p><a href="https://expressjs.com/"><strong>ExpressJS</strong></a><strong> </strong>-<strong> </strong>Fast, unopinionated, minimalist web framework for NodeJS.</p><p><a href="https://www.docker.com/"><strong>Docker</strong></a><strong> </strong>- Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers.</p><p><a href="https://ffmpeg.org/"><strong>FFMpeg</strong></a><strong> </strong>-<strong> </strong>At its core is the command-line FFmpeg tool itself, designed for processing video and audio files.</p><p><a href="https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#readme"><strong>fluent-ffmpeg</strong></a><strong> </strong>-<strong> </strong>NPM package used for the abstraction of the complex command-line usage of FFmpeg into a fluent, easy-to-use node.js module</p><p><a href="https://github.com/richardgirges/express-fileupload#readme"><strong>express-fileupload</strong></a><strong> </strong>-<strong> </strong>NPM Package<strong>. </strong>Simple express middleware for uploading files.</p><blockquote><strong><em>STEP-BY-STEP GUIDE</em></strong></blockquote><blockquote>I will assume that you have <a href="https://nodejs.org/en/"><strong>NodeJS</strong></a><strong> </strong>and<strong> </strong><a href="https://www.docker.com/"><strong>Docker</strong></a><strong> </strong>installed.</blockquote><p><strong>INITIALIZE NODEJS APP</strong></p><p>In terminal</p><pre>mkdir nodejs-video-compression // Makes new directory</pre><pre>cd nodejs-video-compression // Navigates to newly created directory</pre><pre>npm init -y // Initializes new package.json.</pre><pre>npm install express cors express-fileupload fluent-ffmpeg // Installs neccesary npm packages.</pre><pre>touch server.js // Creates new root server file.</pre><pre>code . // Opens project in VSCode</pre><p>Create an initial server instance in <strong>server.js</strong> file</p><pre>const express = require(&quot;express&quot;);</pre><pre>const cors = require(&quot;cors&quot;);</pre><pre><em>// Create a new express application instance</em></pre><pre>const PORT = 5000;</pre><pre>const app = express();</pre><pre><em>// Middleware</em></pre><pre>app.use(cors());</pre><pre>app.use(express.json());</pre><pre><em>// Routes</em></pre><pre>app.get(&quot;/&quot;, (req, res) =&gt; {</pre><pre>  res.send(&quot;Hello World!&quot;);</pre><pre>});</pre><pre><em>// Start the server</em></pre><pre>app.listen(PORT, () =&gt; {</pre><pre>  console.log(`Server started on  http://localhost:${PORT}`);</pre><pre>});</pre><p>Add start script to the <strong>package.json</strong> file</p><pre>&quot;start&quot;: &quot;node server.js&quot;</pre><p>Your <strong>package.json</strong> file should look like this now</p><pre>{</pre><pre> &quot;name&quot;: &quot;nodejs-video-compression&quot;,</pre><pre> &quot;version&quot;: &quot;1.0.0&quot;,</pre><pre> &quot;description&quot;: &quot;&quot;,</pre><pre>&quot;main&quot;: &quot;server.js&quot;,</pre><pre>&quot;scripts&quot;: {</pre><pre>&quot;start&quot;: &quot;node server.js&quot;</pre><pre>},</pre><pre>&quot;keywords&quot;: [],</pre><pre>&quot;author&quot;: &quot;&quot;,</pre><pre>&quot;license&quot;: &quot;ISC&quot;,</pre><pre>&quot;dependencies&quot;: {</pre><pre>&quot;cors&quot;: &quot;^2.8.5&quot;,</pre><pre>&quot;express&quot;: &quot;^4.18.1&quot;,</pre><pre>&quot;express-fileupload&quot;: &quot;^1.4.0&quot;,</pre><pre>&quot;fluent-ffmpeg&quot;: &quot;^2.1.2&quot;</pre><pre>}</pre><pre>}</pre><p>Run the app in terminal</p><pre>npm run start</pre><p>Navigate to your browser to the server URL and you should see this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Y2JHu9eHu_rNwdysV5wcA.png" /><figcaption><a href="http://localhost:5000/">http://localhost:5000</a></figcaption></figure><p><strong>DOCKERIZE THE APP</strong></p><p>In the terminal in the app root</p><pre>touch Dockerfile docker-compose.yml .dockerignore </pre><p>Open the newly created <strong>Dockerfile </strong>and paste the following code to it</p><pre>FROM node:16</pre><pre><em># Create app directory</em></pre><pre>WORKDIR /usr/src/app</pre><pre><em># A wildcard is used to ensure both package.json AND package-lock.json are copied</em></pre><pre><em># where available (npm@5+)</em></pre><pre>COPY package*.json ./</pre><pre><em># Install FFmpeg</em></pre><pre>RUN apt-get update</pre><pre>RUN apt-get install ffmpeg -y</pre><pre><em># Install app dependencies</em></pre><pre>RUN npm install</pre><pre><em># If you are building your code for production</em></pre><pre><em># RUN npm ci --only=production</em></pre><pre><em># Bundle app source<br></em></pre><pre><em># Copy app source to /usr/src/app</em></pre><pre>COPY . .</pre><pre><em># Run the app</em></pre><pre>EXPOSE 5000</pre><pre>CMD [ &quot;npm&quot;, &quot;run&quot;, &quot;start&quot; ]</pre><p>Navigate to <strong>docker-compose.yml </strong>and paste the following code to it</p><pre>services:</pre><pre>  nodejs-video-compression:</pre><pre>    build: .</pre><pre>    ports:</pre><pre>      - &quot;5000:5000&quot;</pre><pre>    volumes:</pre><pre>      - ./temp:/usr/src/app/temp</pre><p>In <strong>.dockerignore </strong>file</p><pre>node_modules</pre><pre>npm-debug.log</pre><p>Now in the terminal finally run the app via Docker by running</p><pre>docker-compose up --build</pre><p>After the execution is over navigate back to <a href="http://localhost:5000/">http://localhost:5000</a> to check if everything went well</p><p><strong>CREATE THE VIDEO COMPRESSION API</strong></p><p>Let&#39;s start by importing <strong>express-fileupload </strong>in our<strong> server.js</strong></p><pre>const fileUpload = require(&quot;express-fileupload&quot;);</pre><p>Now let&#39;s add file upload middleware and get access to <strong>req.files</strong> in our routes</p><pre>app.use(</pre><pre>  fileUpload({</pre><pre>    tempFileDir: &quot;temp&quot;,</pre><pre>    useTempFiles: true,</pre><pre>  })</pre><pre>);</pre><p>Next step is to create an initial <strong>POST</strong> route for video compression in our <strong>server.js</strong>. For now, just the console logs the incoming file and returns a successful response.</p><pre>app.post(&quot;/compress-video&quot;, (req, res) =&gt; {</pre><pre>  console.log(req.files.video);</pre><pre>  res.send(&quot;Success&quot;);</pre><pre>});</pre><p>I used <a href="https://www.postman.com/"><strong>Postman</strong></a> for API testing and my request looked like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HYsA3DuzbDaLmGp8hjgrRA.png" /><figcaption>Request example in Postman</figcaption></figure><p>And in our terminal, we should see a console log of our incoming file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NoeWRw5rzok-bRrrhgBpVA.png" /><figcaption>Terminal output for console log in POST route</figcaption></figure><p><strong>CREATE CHILD PROCESS</strong></p><p>In order to make the process of video compression not block any other request to our server, we should offload it to separate <a href="https://nodejs.org/api/child_process.html"><strong>child_process</strong></a>. Let&#39;s start by making a new <strong>video.js</strong> file. In terminal run</p><pre>touch video.js</pre><p>I would not go into details about what are <strong>worker_threads</strong> and <strong>child_processes</strong> in NodeJS but three is <a href="https://spectrumstutz.com/nodejs/nodejs-child-process-worker-threads/">one great article that explains it</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUKM-lom3vOB07lBGOv3VQ.jpeg" /></figure><p>Let&#39;s start by listening to the message in our video.js and responding to the parent process in <strong>video.js</strong></p><pre>process.on(&quot;message&quot;, (message) =&gt; {</pre><pre>  console.log(&quot;Recieved:&quot;,message)</pre><pre>  setTimeout(() =&gt; {</pre><pre>    process.send(&quot;Hello from child process&quot;);</pre><pre>    process.exit();</pre><pre>  }, 3000);</pre><pre>});</pre><p>And let&#39;s spawn child_process in our <strong>POST</strong> request in server.js</p><p>At the top of the file import:</p><pre>const { fork } = require(&quot;child_process&quot;);</pre><p>And in our video compression route let&#39;s check if the video exists as well as spawn the process.</p><p>Our <strong>/compress-video </strong>route should look like this now.</p><pre>app.post(&quot;/compress-video&quot;, (req, res) =&gt; {</pre><pre>  const video = req.files.video;</pre><pre><em>  // When file is uploaded it is stored in temp file</em></pre><pre><em>  // this is made possible by express-fileupload</em></pre><pre>  const tempFilePath = video.tempFilePath;</pre><pre><em>  if</em> (video &amp;&amp; tempFilePath) {</pre><pre><em>    // Create a new child process</em></pre><pre>    const child = fork(&quot;video.js&quot;);</pre><pre><em>    // Send message to child process</em></pre><pre>    child.send(tempFilePath);</pre><pre><em>    // Listen for message from child process</em></pre><pre>    child.on(&quot;message&quot;, (message) =&gt; {</pre><pre>      console.log(</pre><pre>      &quot;🚀 ~ file: server.js ~ line 37 ~ child.on ~ message&quot;,</pre><pre>      message</pre><pre>      );</pre><pre>      res.send(&quot;Success&quot;);</pre><pre>    });</pre><pre>  } <em>else</em> {</pre><pre>  res.status(400).send(&quot;No file uploaded&quot;);</pre><pre>  }</pre><pre>});</pre><p>In our terminal, there should be logs of both actions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9vcCd9ePsF8kNESqY3OT5A.png" /><figcaption>Terminal output after initial child_process spawn</figcaption></figure><p><strong>USE FFMPEG IN THE CHILD PROCESS</strong></p><p>Our goal is to offload processing to our newly created child process and save the uploaded file locally as well as delete the temp file.</p><p>Now import <a href="https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#readme"><strong>fluent_ffmpeg</strong></a><strong> </strong>and<strong> </strong><a href="https://nodejs.org/api/fs.html"><strong>fs</strong></a><strong> </strong>in <strong>video.js</strong>.</p><pre>const fs = require(&quot;fs&quot;);</pre><pre>const ffmpeg = require(&quot;fluent-ffmpeg&quot;);</pre><p>In our process message listener, let&#39;s expect a new payload and restructure to get <strong>tempFilePath </strong>and<strong> name</strong></p><pre>process.on(&quot;message&quot;, (payload) =&gt; {</pre><pre>const { tempFilePath, name } = payload;</pre><pre>...</pre><p>To reduce code repetition let&#39;s make a function that will be used for ending the child process. Its task will be to pass the response object to the main process, remove the temp file, and exit the child process.</p><pre>const endProcess = (endPayload) =&gt; {</pre><pre>  const { statusCode, text } = endPayload;</pre><pre><em>  // Remove temp file</em></pre><pre>  fs.unlink(tempFilePath, (err) =&gt; {</pre><pre><em>    if</em> (err) {</pre><pre>      console.log(err);</pre><pre>    }</pre><pre>  });</pre><pre><em>  // Format response so it fits the api response</em></pre><pre>  process.send({ statusCode, text });</pre><pre><em>  // End process</em></pre><pre>  process.exit();</pre><pre>};</pre><p>Let&#39;s finally process the video using the <a href="https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#readme"><strong>fluent_ffmpeg</strong></a><strong> </strong>instance. We have to pass the file path to it and then add any additional options to it. I decided to lower the <a href="https://en.wikipedia.org/wiki/Frame_rate"><strong>FPS</strong></a> to 30 as well as to set <a href="https://trac.ffmpeg.org/wiki/Encode/H.264"><strong>CRF</strong></a> to 28. You can play around with the FFmpeg options and configuration is completely up to you, this is only one example I used for the particular use case. <a href="https://ffmpeg.org/ffmpeg.html"><strong>FFmpeg documentation and options</strong></a></p><pre><em>// Process video and send back the result</em></pre><pre>ffmpeg(tempFilePath)</pre><pre>  .fps(30)</pre><pre>  .addOptions([&quot;-crf 28&quot;])</pre><pre>  .on(&quot;end&quot;, () =&gt; {</pre><pre>    endProcess({ statusCode: 200, text: &quot;Success&quot; });</pre><pre>  })</pre><pre>  .on(&quot;error&quot;, (err) =&gt; {</pre><pre>    endProcess({ statusCode: 500, text: err.message });</pre><pre>  }).save(`./temp/${name}`);</pre><p>Our finished <strong>video.js</strong> should look like this.</p><pre>const fs = require(&quot;fs&quot;);</pre><pre>const ffmpeg = require(&quot;fluent-ffmpeg&quot;);</pre><pre>process.on(&quot;message&quot;, (payload) =&gt; {</pre><pre>const { tempFilePath, name } = payload;</pre><pre>const endProcess = (endPayload) =&gt; {</pre><pre>  const { statusCode, text } = endPayload;</pre><pre><em>  // Remove temp file</em></pre><pre>  fs.unlink(tempFilePath, (err) =&gt; {</pre><pre><em>    if</em> (err) {</pre><pre>      process.send({ statusCode: 500, text: err.message });</pre><pre>    }</pre><pre>  });</pre><pre><em>  // Format response so it fits the api response</em></pre><pre>  process.send({ statusCode, text });</pre><pre><em>  // End process</em></pre><pre>  process.exit();</pre><pre>};</pre><pre><em>// Process video and send back the result</em></pre><pre>ffmpeg(tempFilePath)</pre><pre>  .fps(30)</pre><pre>  .addOptions([&quot;-crf 28&quot;])</pre><pre>  .on(&quot;end&quot;, () =&gt; { </pre><pre>    endProcess({ statusCode: 200, text: &quot;Success&quot; });</pre><pre>  })</pre><pre>  .on(&quot;error&quot;, (err) =&gt; {</pre><pre>    endProcess({ statusCode: 500, text: err.message });</pre><pre>  }).save(`./temp/${name}`);</pre><pre>});</pre><p>In the end, let&#39;s handle the child process response in our<strong> server.js </strong>compression route.</p><pre>...</pre><pre><em>// Send message to child process</em></pre><pre>child.send({ tempFilePath, name: video.name });</pre><pre><em>// Listen for message from child process</em></pre><pre>child.on(&quot;message&quot;, (message) =&gt; {</pre><pre>  const { statusCode, text } = message;</pre><pre>  res.status(statusCode).send(text);</pre><pre>});<br>...</pre><blockquote><strong><em>COMPRESSION COMPARISON</em></strong></blockquote><p><a href="https://drive.google.com/file/d/145jLxrWQUW6Dzt3taW-SKtCqVaNcmQx9/view?usp=sharing">BEFORE</a><strong> 100MB</strong></p><p><a href="https://drive.google.com/file/d/1KIl4pt4XJpHGxUjh38N2w0qzMQP6Ae2y/view?usp=sharing">AFTER</a><strong> 10MB</strong></p><p><a href="https://github.com/borisa99/nodejs-video-compression"><strong>Github repo with full code</strong></a></p><p>Thank you for reading through this article, I am hoping it will help at least someone. Also, I would appreciate any feedback on our approach and I am happy to discuss it anytime.</p><p>Have a nice one, my fellow developers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60e5b3b732f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>