.m3u8 playlist file (index format file), HLS (HTTP Live Streaming) protocol introduction

The M3U8 file format was developed to support the HLS (HTTP Live Streaming) protocol. HLS is an HTTP-based streaming media transmission protocol designed by Apple for transmitting audio and video data.

In HLS, media content is divided into a series of small fragments, each fragment has its own URL. These URLs are stored in an M3U8 playlist file. When a client device plays an HLS stream, it downloads and continuously plays individual media segments based on the information in the M3U8 file, allowing for seamless streaming.

Therefore, it can be said that the M3U8 file format is designed to meet the needs of HLS technology. However, since M3U8 is essentially just an index file format, it can also be used in other scenarios, such as creating audio or video playlists.

But for the most part, when we talk about the M3U8, we're usually talking about HLS-related applications.

Article directory

M3U8 Introduction

M3U8 is a media playlist format based on HTTP Live Streaming (HLS) technology. This is a very important technology as it enables streaming content to adapt to different network conditions and device capabilities, resulting in a seamless online video experience.

HLS and M3U8 concept

HLS(HTTP Live Streaming)

HLS is a protocol proposed by Apple Inc. for real-time streaming and on-demand systems. This protocol can cut streaming media files into multiple small fragments and then transmit them through the HTTP protocol. The advantage of this is that the player can dynamically select streams of different qualities based on network conditions to avoid buffering or lagging.

M3U8

M3U8 is a UTF-8 encoded M3U file. M3U is a simple audio playlist format mainly used for MP3 lists. In HLS, M3U8 is used to describe media stream information, such as segment location, duration, sequence number, etc.

M3U8 structure

An M3U8 file usually contains the following parts:

File header

Starting with #EXTM3Uindicates that this is an M3U8 file.

media segment information

Begins with #EXTINF, followed by the duration and title of the media segment.

media segment URI

The path or URL to a media file (usually a .ts file).

Explanation of common fields and parameters of M3U8 files

1.: #EXTM3UThe header identifier of the M3U8 file, indicating that this is an M3U8 file.

2.: #EXT-X-VERSIONThis tag indicates the protocol version that the playlist is compatible with. For example: #EXT-X-VERSION:3

3.: #EXT-X-MEDIA-SEQUENCEIndicates the starting number of the media file URI list. For example: #EXT-X-MEDIA-SEQUENCE:7794

4.: #EXT-X-TARGETDURATIONMaximum duration of all media files (seconds). For example: #EXT-X-TARGETDURATION:10

5.: #EXTINFThe duration of the media segment file (seconds). The next line immediately following this tag is the corresponding media file URI. For example: fileSequence0.ts,

6. #EXT-X-ENDLIST: Playlist end tag, indicating that all media files have been listed.

7. #EXT-X-STREAM-INF: Parameters used to define the stream, such as bandwidth, resolution, etc.

8. #EXT-X-DISCONTINUITY: Insert this tag between two media files to indicate that there may be discontinuities between the two media files, such as changes in encoding parameters, jumps in timestamps, etc.

9. #EXT-X-KEY: Define the methods and keys required to decrypt media segments. This is used to encrypt the stream.

10. #EXT-X-MAP: Specifies a URI containing a media initialization fragment.

11.: #EXT-X-ALLOW-CACHEAn optional label indicating whether the client is allowed to cache media segment files

It has two values:

  1. YES:Allows clients to cache media segments.
  2. NO:Clients are not allowed to cache media segments.

For example:#EXT-X-ALLOW-CACHE:YES

It should be noted that in the latest version of the HLS protocol, this tag has been abandoned because its actual effect is not clear. The decision whether to cache is usually determined by the client itself or the HTTP cache control mechanism.

Note: "Client-side caching of media segments" refers to temporarily storing a portion of the data in a media stream on the client (e.g., the media player on your phone or computer) so that playback can continue if there is a problem with the network connection.

When you watch a video or listen to music online, data is usually not transferred from the server to your device all at once. Instead, it is cut into small segments, which are called "media segments" or "media segments". This technology is called streaming because the data flows continuously to your device like water.

When a client allows caching of media segments, it saves each media segment in local storage until they are played. Then, even if the network connection is temporarily interrupted, the player can continue playing from the local cache, providing users with a smoother playback experience.

M3U8 technical analysis

file format

An M3U8 file is essentially a text file that contains all the information needed to play the streamed media. for example:

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXTINF:9.009,
fileSequence0.ts
#EXTINF:9.009,
fileSequence1.ts
#EXTINF:3.003,
fileSequence2.ts
#EXT-X-ENDLIST

The M3U8 file in this example describes three .ts files, each of which is 9.009 seconds, 9.009 seconds, and 3.003 seconds long.

Slicing technology

In HLS, the media stream is divided into multiple small fragments, and these fragments are managed through M3U8 files. Slicing technology has the following benefits:

  • The quality of the stream can be dynamically adjusted according to network conditions to improve the playback experience.
  • Supports live streaming and on-demand content.
  • Can adapt to various network environments, including mobile networks.

The main challenge of slicing technology is how to maintain video continuity and synchronization. To solve this problem, HLS uses mechanisms such as timestamps and sequence numbers.

encoding and decoding

In HLS, media streams are usually encoded into the MPEG-2 Transport Stream (TS) format. This is a very mature format that can support a variety of audio and video encodings, including H.264, AAC, etc.

For the player, it needs to be able to parse M3U8 files and decode TS streams. This is usually achieved through some libraries (such as FFmpeg) or system-level support (such as AVPlayer for iOS).

M3U8 application cases

M3U8 is widely used in many streaming applications, such as YouTube, Netflix, Hulu, etc. In these applications, the M3U8 is used to manage and playback streams of various qualities to provide a premium playback experience.

Example

MP4 to m3u8

The following article has an example of ffmpeg converting MP4 files into m3u8 files:

Docker nginx container agent plays m3u8 video files (HLS)

other problems

In live broadcast scenarios, the update timing of m3u8 files (note: ffmpeg updates m3u8 files usually atomically, so there is no need to worry about reading partial or incomplete data when reading)

In a live broadcast scenario, the M3U8 file (playlist) is usually updated when a new TS file (a segment of the HLS stream) is generated. Whenever a new TS file is generated and ready for client download, its information is added to the M3U8 file.

Specifically, the process is roughly as follows:

  1. A new TS file is generated.
  2. The information of the TS file (including its URL, duration, etc.) is added to the M3U8 file.
  3. If the rolling window policy (i.e. parameter) is enabled -hls_list_sizeand the current number of TS files exceeds the set value, the oldest TS file information will be removed from the M3U8 file.

In addition, FFmpeg also supports hls_flagsfiner-grained control through parameters. For example, the flag can be set append_listto add new TS files in an existing list instead of a new list.

‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ
‌‍ᅟ ᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ ‌‍ᅟᅠ

Guess you like

Origin blog.csdn.net/Dontla/article/details/135352694