Introduction to the HTTP Live Streaming (HLS) protocol in Live / on-demand

First, live broadcast / on-demand overview

In 2016, it was called live broadcast in the first year, and various live broadcasts were released. After several years of discovery, it has become increasingly mature, but its application technology is basically shown below.

The stream is the process of transmitting the acquisition of content to the server

The draw is the process of playing the content of the user

If only consider the drawing process, it can also be called on-demand, such as Aiqiyi, Tencent Video and other video websites.

We can see that the HLS protocol is a commonly used protocol during the draw.

Second, HLS

HTTP LIVE streaming is a HTTP-based stream media network transfer protocol proposed by Apple. It is part of Apple QuickTime X and iPhone Software Systems. Its work is to divide the entire stream into a small HTTP-based file. Download only every time. When the media stream is playing, the client can choose to download the same resources from many different alternate sources, allowing streaming media sessions to accommodate different data rates. When starting a streaming session, the client downloads an Extended M3U (M3U8) Playlist file that contains metadata for finding the available media stream.


Apple development, background power, Apple and Android support

Based on HTTP protocol transmission, you can use CDN to distribute

Adaptive code rate broadcast, the client automatically selects video of different code rates based on network conditions

Use M3U8 file as an index

Video segmentation, you can drag, download only one small part of data (10 seconds) each time

The package format of the video is TS, and the video is encoded is H264, the audio coding format is MP3, AAC or AC-3.


The delay is relatively high

Third, HLS principle

3.1 Working mode

As shown in the figure below, download the M3U8 file first, then get the primary and secondary index files, get the TS file download address via the index file so that the client can download the TS video file in order and play.

3.2 M3U8 file format (single code rate adaptation flow)

# ExtM3u # Ext-X-Version: 3 # ext-x-allow-cache: Yes # ext-x-media-sequence: 2 # ext-x-targetduration: 16 # extinf: 14.357, no descliVestream-2.ts # Extinf: 15.617, No DescliVestream-3.ts # extinf: 14.358, No DescliVestream-4.TS # extinf: 15.618, no descliVestream-5.ts

3.3 Playing mode

On-demand is the current time point can get all the INDEX files and TS files, and the address of all TS files is recorded in the secondary index file. This mode allows clients to access all content.

Live is to generate M3U8 and TS files in real time. Its index file is always dynamically changed. When playing, you need to continue to download the secondary index file to get the latest generated TS file play video. If there is no # ext-x-endlist flag at the end of a secondary index file, it means that it is a Live video stream. (Such as IPTV TV,

Fourth, create HLS using Ffmpeg

You can use the following command

mkdir -p __ABC.mp4__ffmpeg -i ABC.mp4 -c copy -bsf: v h264_mp4toannexb -hls_time 6 -hls_list_size 0 -hls_segment_filename “__ABC.mp4 __ /% 04d.ts” “__ABC.mp4 __ / video.m3u8″> / dev / NULL 2> & 1

Creating the completed M3U8 and TS files can play with HTML5 DPLAYER direct play

Five, HLS encryption and anti-theft chain

Some websites that need to be charged in some on-demand, such as Iqiyi, spend the copyright of the money to buy, the video is not casually downloaded, encryption and anti-theft chain into an important technology.

HLS supports the AES-128 mode encryption, just need to add a key at the FFMPEG slice, and specify the key in the M3U8 file.

# EXT-X-Key: Methodaes-128, URI “/hls/key.key”

But in this case, the client can see the content of Key, the meaning is not large, too unsafe. If you want to real safety, you can make a dynamic key. According to the user ID, browser ua, cookie, etc., the key is generated in real time, and each video key can be inconsistent, different time keys are inconsistent. As shown below, the way the dynamic key has a high cost. Dynamic acquisition involving a key, may involve video real-time encryption.

Anti-theft chain generally generates a string key based on user information (such as timestamp, user ID, browser ua, cookie, user IP address), with server comparison, if Key is allowed, Key generally has time limit, After the restriction time is invalid, the current CDN is very good for the anti-theft chain, and can make full use of the CDN authentication mechanism.

V. New International Standard Dash Protocol

Dash (MPEG-DASH) is the abbreviation of Dynamic Adaptive Streaming Over HTTP. It is the technical standards launched by the International Standard Group MPEG 2014. The main goal is to form a streaming of the IP network to carry a single format and provide an efficient and high quality service unified program. Solve the storage and service capacity waste of multi-formal transmission scheme (http dynamic streaming), operating high cost and complexity, inter-system inter-system interoperability.

Dash is based on HTTP-based dynamic adaptive bit rate flow technologies, and HLS, HDS technology is similar, and the video is divided into a small segment, transmitted through the HTTP protocol, and the client is played; the difference is MPEG-DASH Support MPEG-2 TS, MP4 and other formats, you can cut a video according to a variety of coded cuts, downloading the media format can be either a TS file or an MP4 file, so when the client loads the video, according to the current network speed And supported encoding the corresponding video clip to play.

Domestic Á¨ Á¨ uses the Dash protocol.

Similar to the M3U8 index file of the HLS protocol, the Dash protocol also provides the MPD (Media Presentation Description) description file (XML format) to download the download address, resolution, code rate, time long, individual shards, coding methods, etc. The regulations.

Dash’s Split (SEGMENT) can include MP4 files (FMP4, file extensions are typically .m4s or direct use .mp4), or include TS files, but the entire standard is more prefer to use Split MP4 files.