Generating DASH Streams with Node.js

Mehran
3 min readMar 29, 2023

Photo by Erik Mclean on Unsplash

Introduction

The increasing demand for video content across various devices and network conditions has driven the need for efficient video delivery techniques. One such method is Dynamic Adaptive Streaming over HTTP (DASH), which allows for the seamless delivery of video streams tailored to the viewer’s device and network capabilities. In this tutorial, we’ll dive into generating DASH streams from a video file using Node.js and the fluent-ffmpeg library.

Setting Up Your Environment

Before we start, let’s make sure you have everything you need:

  1. Install Node.js: Download and install the latest version of Node.js from the official website: https://nodejs.org/en/download/
  2. Install fluent-ffmpeg: Run the following command in your terminal or command prompt to install the fluent-ffmpeg library:
npm install fluent-ffmpeg

3. Install FFmpeg: Download and install FFmpeg from the official website: https://ffmpeg.org/download.html. Make sure the FFmpeg executable is in your system’s PATH.

4. Install TypeScript and ts-node globally (if you haven’t already) using the following command:

npm install -g typescript ts-node

5. Create a new directory for your project and navigate to it:

mkdir dash-transcoding-ts && cd dash-transcoding-ts

6. Initialize a new Node.js project and install the required dependencies:

npm init -y
npm install fluent-ffmpeg @types/fluent-ffmpeg

7. Create a new TypeScript configuration file (tsconfig.json) in the project directory with the following content:

{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
}
}

Generating DASH Streams with Node.js

With the environment set up, we can now write a Node.js script to generate DASH streams from a video file. Create a new JavaScript file (e.g., dash-transcoding.ts) and paste the following code:

import ffmpeg from 'fluent-ffmpeg';
import path from 'path';

// Make sure FFmpeg is available in your system's PATH
ffmpeg.setFfmpegPath('/path/to/your/ffmpeg');

// Input video file
const inputVideo = 'input.mp4';

// DASH output directory and files
const outputDir = 'dash_output';
const outputManifest = path.join(outputDir, 'manifest.mpd');

// Configure FFmpeg for DASH transcoding
ffmpeg(inputVideo)
.outputOptions([
// DASH options
'-f dash',
'-init_seg_name init_$RepresentationID$.m4s',
'-media_seg_name chunk_$RepresentationID$_$Number%05d$.m4s',
'-use_timeline 1',
'-use_template 1',
'-window_size 5',
'-adaptation_sets "id=0,streams=v id=1,streams=a"',

// Video options
'-map 0:v:0',
'-c:v:0 libx264',
'-b:v:0 500k',
'-s:v:0 640x360',

// Audio options
'-map 0:a:0',
'-c:a:0 aac',
'-b:a:0 128k',
])
.output(outputManifest)
.on('start', () => {
console.log('Starting DASH transcoding...');
})
.on('progress', (progress) => {
console.log(`Progress: ${progress.percent.toFixed(2)}%`);
})
.on('error', (err: Error, stdout: string, stderr: string) => {
console.error('Error:', err.message);
console.error('FFmpeg stdout:', stdout);
console.error('FFmpeg stderr:', stderr);
})
.on('end', () => {
console.log('DASH transcoding completed.');
})
.run();

Replace /path/to/your/ffmpeg with the actual path to the FFmpeg executable on your system. This script takes an input video file named input.mp4 and generates a DASH stream with separate video and audio AdaptationSets. The generated DASH files will be stored in the dash_output directory.

To run the script, execute the following command:

ts-node dash-transcoding.ts

Conclusion

Creating efficient video delivery techniques is essential with the increasing demand for video content. This tutorial explored how to generate DASH streams from a video file using Node.js and the fluent-ffmpeg library.

Now that you’ve learned how to generate DASH streams with Node.js, you can explore other transcoding techniques, such as creating HLS streams or diving into video processing and analysis. Happy streaming!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mehran
Mehran

Written by Mehran

Tech Team Lead | Cloud, Video & Microservices Expert | Insights on streaming innovations & programming. #ContinuousLearning

No responses yet

Write a response