The goal of this tutorial will be to build a robust video (or music) streaming API using Node JS. Don’t worry, its surprisingly easy since we will be utilizing a modern protocol, HTTP Live Streaming, or HLS.
HLS allows us to serve large media files as many smaller files. We will use a program to convert a single .mp3
file into several text files that can be served by a typical NodeJS file server. There are a few advantages to this:
FFmpeg will convert mp3 files to HLS format, which is really a bunch of files. The main HLS file is the .m3u8
file, and the URL that will be given to the streaming client will be the path to this file. This .m3u8
metadata file tells the client where to find each data (.ts) file. Data files typically contain small chunks of media data (~10 seconds) and are fetched at the client’s request as the user progresses through the song or video.
Let’s format some media.
Install FFmpeg. If you are on a Mac:
brew install ffmpeg
Navigate to the directory of the mp4 file and run FFMPEG:
ffmpeg -i sample-mp4-file.mp4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls index.m3u8
#hls #javascript #nodejs #streaming #video