#6 M&E Technical : How to setup single bitrate HLS streaming
HLS or HTTP Live Streaming provides a reliable way to deliver continuous and long-form audio and video over the Internet. It has become a default standard for online delivery of audio and video serving content on host of devices and browser environments. What makes HLS a better option than RTMP or RTSP chunked streaming is deployment with standard http severs and http as communication protocol. HLS can be used to deliver a single bitrate file or in a more advanced format, multiple bitrates. Multi bitrate configuration enables receiver or client to adapt to required bitrate based on its current network conditions for an uninterrupted playback. HLS or HTTP Live Streaming (HLS) specification is available here HLS-IETF
Key benefits with HLS Streaming
- Well structured and simple protocol. Playlist is accessible in text format and easy to modify.
- Use of TS (transport stream) files ensures ecosystem for testing and conformance.
- TS files can carry other metadata, such as SCTE 35 cues or ID3 tags (audio metadata standard for MP3 audio)
- HLS is supported natively on iOS
HLS has some disadvantages also like it is not supported natively on windows platform and TS files mux audio, video and data together. The mix limits modifications to files like adding additional audio tracks etc.
HLS File structure
HTTP live streaming works on sequence of MPEG-2 TS file segments which are listed in a manifest index file. The TS media segments encapsulates both the audio and video and are typically of 10 seconds duration, but it can be easily configured. An index file provides an ordered list of the URLs of the media segment files and is saved with an .m3u8 extension. The receiver or client downloads the index file, parses the URLs and downloads media segments in the sequence for playback. The URLs for segments can be a remote HTTP resource or local file on web server, accessed using the standard http file protocol.
Index file has details on sequence number to associate chunks from different profiles, time information, type of stream, information about the chunk duration etc. Index file also has optional metadata directives signaling whether chunks can be cached and the location of decryption keys.
Setup for HLS Streaming
Deploying HTTP Live Streaming needs few simple steps. We need source content which will be streamed. If content is not in desired bitrate, it can be easily recoded to desired bitrate
Desired bitrate files can be created by recoding the mezzanine or original file to target bitrates. In the example below, we encode the file for 500 kbps which is ideal for 48op resolution using constant rate factor (constant quality), a more desired option is using 2 pass encoding process.
ffmpeg -i Glacier.mp4 -acodec copy -vcodec copy -preset slow -b:v 500k -maxrate
Next we create HLS segments and store them behind a server. Lastly we create either an HTML page or a client app to receive and playback the media. Below are the steps to have HLS streaming service up and running in no time.
Step 1 : Source content and prepare in HLS chunked files
- Get Source content and check its config
- In the below example, we have a 2 min mp4 file with video as avc1 and audio as aac
ffmpeg -i Geography.mp4 Duration: 00:02:00.05, start: 0.000000, bitrate: 4014 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3883 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc (default) Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default) Metadata:
3. Create TS segments from the source mp4 file
There are multiple tools which can be used to create MPEG TS segments, namely ffmpeg, mediafilesegmenter from Apple media toolkit or mp42hls utility from Bento4 library.
Use ffmpeg command to create a single bitrate HLS segments
Check here to setup ffmpeg for mac or windows
ffmpeg -i Geography.mp4 -hls_time 10 output.m3u8
-hls_time defines the chunk interval in seconds
Use Apple mediafilesegmenter to create single bitrate HLS segments
mediafilesegmenter Geography.mp4 -B segment -i output.m3u8
fileSequence is the default prefix for the generated .ts files. It can be changed using the -B option. Similarly index file is named as program_index.m3u8 by default and can be changed using -i option, duration of each segment is 10 seconds by default and can be changed using -t sec option.
Use Bento4 toolkit to create single bitrate HLS segments
Get Bento4 toolkit from here. Bento4 is a C++ library and tools designed to read and write ISO-MP4 files.
mp42hls Geography.mp4
mp42hls converts an MP4 file to a single-bitrate HLS presentation, generating TS segments and .m3u8 playlist. Segment is the default prefix for the generated .ts files, it can be changed using the –segment-url-template option. Similarly default index file is named as stream.m3u8 and can be changed using –index-filename option, duration of each segment is 10 seconds by default and can be changed using –segment-duration sec option.
Sample manifest file
Below is an sample m3u8 file created using the mp42hls command.
#EXTM3U #EXT-X-VERSION:3 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-TARGETDURATION:14 #EXT-X-MEDIA-SEQUENCE:0 #EXTINF:14.714700, segment-0.ts #EXTINF:10.677333, segment-1.ts #EXTINF:10.677333, segment-2.ts #EXTINF:10.677333, segment-3.ts #EXTINF:10.677333, segment-4.ts #EXTINF:10.677333, segment-5.ts #EXTINF:10.677333, segment-6.ts #EXTINF:10.677333, segment-7.ts #EXTINF:10.677333, segment-8.ts #EXTINF:10.677333, segment-9.ts #EXTINF:9.175833, segment-10.ts #EXT-X-ENDLIST
Structure of manifest file
M3U file format defines two key tags: EXTM3U
and EXTINF
. EXTINF
tag is a record marker, has a unique sequence number, specific the duration and describes the media file by the URL that follows it. In the above example the first TS segment (segment-0.ts) refers to the URL which is local to the server and 14.714 denotes the duration of first segment in seconds. The EXT-X-PLAYLIST-TYPE tag provides mutability information about the event type e.g VOD. The EXT-X-MEDIA-SEQUENCE
tag indicates the sequence number of the first URL that appears in a playlist file. Other important tags are
- The
EXT-X-TARGETDURATION
tag specifies the maximum media file duration. - The
EXT-X-VERSION
tag indicates the compatibility version of the playlist file. - The
EXT-X-ENDLIST
tag indicates that no more media files will be added to the playlist file.
Above we created a HLS segments for a single bitrate. A more advanced usage is creating multiple bitrate files and generate manifest with respective chunked segments.
Setup Server
HTTP Live Streaming can be served from an ordinary web server e.g. apache and requires no special configuration. It is recommended to associate the MIME types of the files being served with their file extensions
- .m3u8 – application/x-mpegURL
Client side code
The easiest way to distribute HTTP Live Streaming media is to create a webpage that includes the HTML5 <video>
tag, using an .M3U8
playlist file as the video source. The source can also be a relative path on the web server or refer to file on a remote/CDN server location.
<H2> HLS streaming using HTML player </H2> <p> <video width="600" height="400" controls> <source src="stream.m3u8" application/x-mpegURL> </video> </p>
For browsers that don’t support HTML5 video
element or do not support HTTP Live Streaming, one can include fallback code for progressive download as mentioned below
<H2> HLS streaming using HTML player </H2> <p> <video width="600" height="400" controls> <source src="stream.m3u8" application/x-mpegURL> Your browser is not supporting HLS with HTML5 video. <source src="Geography.mp4"> </video> </p>
Need to restrict media access to designated users, it can be easily done by using encrypted streams. We will cover next how to create HLS streaming with encryption