How To Use Node - Js Workers For Video Encoding - by Patrick Kalkman - Better Programming
How To Use Node - Js Workers For Video Encoding - by Patrick Kalkman - Better Programming
Open in app
Member-only story
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 1/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
Last night, I again worked on my side project, Mini Video Encoder. I added a new
service for encoding video. The Workflow Encoder, as I call the service, uses
Node.js. You may know that video encoding is very CPU-intensive.
In the past, Node.js has never been a good candidate for performing CPU-intensive
tasks. The reason for this is Node.js uses a single thread to execute JavaScript code.
But since the release of 11.7.0, Node.js has a new module called worker_threads .
“Workers (threads) are useful for performing CPU-intensive JavaScript operations. They
will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O
operations are more efficient than Workers can be.”
Because of this, I implemented the Workflow Encoder using Workers. In this piece,
I describe the implementation.
Encoding Video
Before describing the implementation, I need to talk a bit about why encoding is
necessary for streaming video.
The Workflow Encoder is part of a larger project called Mini Video Encoder (MVE).
MVE is an open-source platform for converting videos. After conversion, a regular
HTTP server can deliver the videos using adaptive streaming.
To make this possible, Workflow Encoder creates many versions of the same video.
Each version has a different bit rate and resolution. This list of bit rates and
resolutions is called an encoding ladder.
Apple recommends the following encoding ladder for a 1080p video. If you encode
your videos, use this ladder — the video will play correctly on Apple devices.
Caminandes 3: Llamigos
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 3/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
The Workflow Encoder communicates with the Workflow engine and asks if there’s
a task to perform. The Workflow Encoder uses the REST API of the Workflow engine
for communication.
The startEncoder function creates and starts the Worker. It creates the Worker
object by calling the constructor and passing a relative path to a JavaScript file. This
file contains the function that must be executed on a different thread.
1 /*
2 * Encoder Engine
3 */
4
5 // Dependencies
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 4/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
6 const workerThreads = require('worker_threads');
Starting the encoder by creating a Worker
7 const superagent = require('superagent');
8
The
9
second argument of the Worker constructor is an options object. I use this to set
const log = require('./log');
workerData
10 to the encodingInstructions
const constants = require('./constants');. This assignment clones
11 and makes it available
const config = require('./config');
encodingInstructions in the Worker function.
12
13 const encoderEngine = {};
The actual function performing the work is encode in encoder.js . The file contains
14
a 15
single function that the Worker
encoderEngine.startEncoder executes.
= function I left out most to focus only
startEncoder(encodingInstructions, cb) { on the
essential
16 part.
log.info('Starting Worker....');
17
18 const worker = new workerThreads.Worker('./lib/encoder/encoder.js', {
1 const { parentPort, workerData } = require("worker_threads");
19 workerData: encodingInstructions,
2
20 });
3 async function encode() {
21 };
4
encoderEngine.js
5 hosted with ❤ by GitHub
const encodingInstructions = workerData; view raw
6
7 ...
8
9 }
10
11 encode();
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 5/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
The main thread, on the other end, receives these messages through events. On line
8, worker.on defines the function that receives the message events.
Depending on the type of message, the function performs a specific action. In the
case of the PROGRESS message, it logs the message using the log object. This way, we
see the progress of the worker in the main thread.
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 6/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
The mechanism is almost the same as communicating from the Worker to the main
thread. Here, we use the postMessage method on the worker object.
The Worker uses the parentPort to create an event handler for receiving messages.
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 7/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
When the Worker receives STOP_ENCODING , it stops the running encoding task. It
stops the task by calling ffmpegCommand.kill() . This will SIGKILL to the FFmpeg
process and stop it.
Worker thread stops when it receives a stop message from the main thread
Conclusion
I like how the Node.js team implemented threading using Workers. By having an
explicit communication channel between threads, they prevent synchronization
issues. Synchronization causes a lot of issues with other programming languages.
The current implementation of the Workflow Encoder uses Workers for encoding
video. You can find the source on GitHub. It’s still a work in progress.
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 8/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
Follow
Dev & writer exploring open-source innovation & agile. Passionate about learning & teaching.
https://medium.com/@pkalkman/membership
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 9/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
824 5
1.6K 13
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 10/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
4K 38
Leveraging the power of Large Language Models and the LangChain framework for an
innovative approach to document querying
146 5
Dinubhagya
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 12/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
18
693 6
Lists
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 13/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
604 2
4K 38
75
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 15/16
8/14/23, 11:56 AM How to Use Node.js Workers for Video Encoding | by Patrick Kalkman | Better Programming
34
https://betterprogramming.pub/how-to-use-node-js-workers-for-video-encoding-f1379d3933d0 16/16