HLS 介绍,M3U8 格式分析

HLS 是什么

维基百科的介绍:

HTTP Live Streaming,缩写为HLS,是由苹果公司提出基于HTTP的流媒体网络传输协议。是苹果公司QuickTime X和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包含元数据的扩充 M3U (m3u8) 播放列表文件,用于寻找可用的媒体流。

HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。

苹果公司把HLS协议作为一个互联网草案(逐步提交),在第一阶段中已作为一个非正式的标准提交到IETF。2017年8月,RFC 8216发布,描述了HLS协议第7版的定义。

苹果自己的介绍:

Send live and on‐demand audio and video to iPhone, iPad, Mac, Apple Watch, Apple TV, and PC with HTTP Live Streaming (HLS) technology from Apple. Using the same protocol that powers the web, HLS lets you deploy content using ordinary web servers and content delivery networks. HLS is designed for reliability and dynamically adapts to network conditions by optimizing playback for the available speed of wired and wireless connections.

总结一下:

  1. 基于HTTP的流媒体网络传输协议,支持安全通信(通过https, 媒体数据加密等)
  2. 工作原理是把整个流分成一个个小的基于HTTP的文件来下载, 使用m3u8描述播放列表。
  3. 支持直播和点播(VOD, video on demand)
  4. 当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率

HLS 流媒体架构

Conceptually, HTTP Live Streaming consists of three parts: the server component, the distribution component, and the client software.

In a typical configuration, a hardware encoder takes audio-video input, encodes it as HEVC video and AC-3 audio, and outputs a fragmented MPEG-4 file or an MPEG-2 transport stream. A software stream segmenter then breaks the stream into a series of short media files, which are placed on a web server. The segmenter also creates and maintains an index file containing a list of the media files. The URL of the index file is published on the web server. Client software reads the index, then requests the listed media files in order and displays them without any pauses or gaps between segments.

概念上,HLS 包含

  • 服务端组件
  • 分发组件
  • 客户端软件

步骤:

  1. 服务端硬件编码器,将音视频输入编码(视频编码成HEVC,H264, 音频编码成AAC,AC-3), 输出ts流(也可以生成分段的mp4文件, 很少用)
  2. 分段软件会将ts流分割,打包成ts文件,同时生成索引文件(m3u8),索引文件保存了媒体文件的信息。
  3. 客户端读取m3u8索引文件,下载对应的ts文件,将内容播放给用户

苹果提供了一套用于HLS的工具集,参考:

Using Apple’s HTTP Live Streaming (HLS) Tools

  • Media File Segmenter (mediafilesegmenter), 媒体文件分割器
  • Media Subtitle Segmenter (mediasubtitlesegmenter), 字幕分割器
  • Media Stream Segmenter (mediastreamsegmenter) 媒体流分割器
  • Variant Playlist Creator (variantplaylistcreator) 可变播放列表生成器

M3U8

The Unicode version of M3U is M3U8, which uses UTF-8-encoded characters. M3U8 files are the basis for the HTTP Live Streaming (HLS) format originally developed by Apple to stream video and radio to iOS devices, and which is now a popular format for adaptive streaming in general.

M3U (MP3 URL or Moving Picture Experts Group Audio Layer 3 Uniform Resource Locator in full) is a computer file format for a multimedia playlist. One common use of the M3U file format is creating a single-entry playlist file pointing to a stream on the Internet.

Although originally designed for audio files, such as MP3, it is commonly used to point media players to audio and video sources, including online sources.

总结一下:

  1. M3U8 是M3U的Unicode版本,使用 UTF-8 编码, 是 HLS 的基础
  2. M3U 最初是为音频文件设计的,例如MP3, 现在不再局限于音频文件,可以用来索引音频和视频资源,网络资源
  3. 下面分析M3U 文件格式

M3U

An M3U file is a plain text file that specifies the locations of one or more media files. The file is saved with the “m3u” filename extension if the text is encoded in the local system’s default non-Unicode encoding (e.g., a Windows codepage), or with the “m3u8” extension if the text is UTF-8 encoded.

总结一下:

  1. M3U 文件是纯文本文件,指定了资源文件的位置
  2. .m3u为文件后缀
  3. 使用 UTF-8 编码的版本,以 .m3u8为后缀

Extended M3U

看一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.0,
http://example.com/movie1/fileSequenceA.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceB.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceC.ts
#EXTINF:9.0,
http://example.com/movie1/fileSequenceD.ts
#EXT-X-ENDLIST

语法:

  • Tags: begin with ‘#EXT’
  • Comments: only begin with ‘#’
  • 标签后面可以有参数,例如#EXTINF:10.0
  • 标签内容可以是:
    1. an absolute local pathname
    2. a local pathname relative to the M3U file location; e.g. Heavysets.mp3
    3. a URL

表格参考:https://en.wikipedia.org/wiki/M3U

例子分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.0,
http://example.com/movie1/fileSequenceA.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceB.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceC.ts
#EXTINF:9.0,
http://example.com/movie1/fileSequenceD.ts
#EXT-X-ENDLIST

EXTM3U

表明是extended M3U文件, 区别于基本的M3U文件。m3u8文件必须以该标签开始。

EXT-X-PLAYLIST-TYPE

类型可以是:

  • VOD, 代表点播, 结尾包含#EXT-X-ENDLIST
  • EVENT, 代表直播, 结尾不包含#EXT-X-ENDLIST

EXT-X-TARGETDURATION

单个媒体文件的最大时长。每一个#EXTINF:10.0标签指定的时长<=EXT-X-TARGETDURATION

EXT-X-MEDIA-SEQUENCE

Indicates the sequence number of the first URL that appears in a playlist file. Each media file URL in a playlist has a unique integer sequence number. The sequence number of a URL is higher by 1 than the sequence number of the URL that preceded it. The media sequence numbers have no relation to the names of the files.

m3u8文件中第一个URL文件的编号,与文件名字无关,每次比前面的+1。

EXT-X-VERSION

The EXT-X-VERSION tag indicates the compatibility version of the Playlist file. This file, its associated media, and its server must comply with all provisions of the IETF Internet-Draft of “HTTP Live Streaming 2nd Edition” (or earlier specifications) describing the protocol version indicated by the tag value. A Playlist file that doesn’t contain an EXT-X-VERSION tag must comply with version 1 of this protocol.

表明该m3u8文件的版本兼容性。该m3u8文件,关联的媒体,服务器必须服从于该版本特性。如果不包含该字段,必须服从与版本1的规定。

参考: About the EXT-X-VERSION tag

EXTINF

  • 描述资源文件时长,URL
  • 时长: 版本小于3是int, 版本大于等于3是float, 单位秒

EXT-X-ENDLIST

结束标志,表明没有更多资源了。vod有,event可能没有。

VOD Playlist 点播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.0,
http://example.com/movie1/fileSequenceA.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceB.ts
#EXTINF:10.0,
http://example.com/movie1/fileSequenceC.ts
#EXTINF:9.0,
http://example.com/movie1/fileSequenceD.ts
#EXT-X-ENDLIST

live Playlist(Sliding Window) 直播滑动窗口

In live sessions, the index file is updated by removing media URIs from the file as new media files are created and made available. The EXT-X-ENDLIST tag isn’t present in the live playlist, indicating that new media files will be added to the index file as they become available.

直播流,m3u8文件会更新,前面的资源会被移除,后面的资源会被追加。
没有EXT-X-ENDLIST标签,表示还会有新的资源被添加。

看一个例子,开始时文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:1
#EXTINF:10.0,
fileSequence1.ts
#EXTINF:10.0,
fileSequence2.ts
#EXTINF:10.0,
fileSequence3.ts
#EXTINF:10.0,
fileSequence4.ts
#EXTINF:10.0,
fileSequence5.ts

旧资源被删除,新资被添加,看到EXT-X-MEDIA-SEQUENCE也发生了变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:2
#EXTINF:10.0,
fileSequence2.ts
#EXTINF:10.0,
fileSequence3.ts
#EXTINF:10.00,
fileSequence4.ts
#EXTINF:10.00,
fileSequence5.ts
#EXTINF:10.0,
fileSequence6.ts

再次更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:4
#EXTINF:10.00,
fileSequence4.ts
#EXTINF:10.00,
fileSequence5.ts
#EXTINF:10.0,
fileSequence6.ts,
#EXTINF:10.0,
fileSequence7.ts,
#EXTINF:10.0,
fileSequence8.ts,
#EXTINF:10.0,
fileSequence9.ts

Event Playlist

An event playlist is specified by the EXT-X-PLAYLIST-TYPE tag with a value of EVENT. It doesn’t initially have an EXT-X-ENDLIST tag, indicating that new media files will be added to the playlist as they become available.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#EXTM3U
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.00,
fileSequence0.ts
#EXTINF:10.0,
fileSequence1.ts
#EXTINF:10.0,
fileSequence2.ts
#EXTINF:10.0,
fileSequence3.ts
#EXTINF:10.0,
fileSequence4.ts

观察到#EXT-X-PLAYLIST-TYPE:EVENT

If the tag is present and has a value of EVENT, the server must not change or delete any part of the playlist file (although it may append lines to it). If the tag is present and has a value of VOD, the playlist file must not change.

You can’t remove anything from the playlist when using the EVENT tag; you may only append new segments to the end of the file. New segments are added to the end of the file until the event has concluded, at which time the EXT-X-ENDLIST tag is appended.

如果类型为EVENT,更新m3u8文件,只能追加,不能删除。
如果类型为VOD,不能更改m3u8播放列表。

更新后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#EXTM3U
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:4
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.0,
fileSequence0.ts
#EXTINF:10.0,
fileSequence1.ts
#EXTINF:10.0,
fileSequence2.ts
#EXTINF:10.0,
fileSequence3.ts
#EXTINF:10.0,
fileSequence4.ts

// List of files between 4 and 120 go here.

#EXTINF:10.0,
fileSequence120.ts
#EXTINF:10.0,
fileSequence121.ts
#EXT-X-ENDLIST

Creating a Multivariant Playlist

Offer multiple playlist files to provide different encodings of the same content.

为同一份内容,提供多个m3u8索引,每个m3u8采用不同的编码参数。

The Multivariant Playlist describes all of the available variants for your content. Each variant is a version of the stream at a particular bit rate and is contained in a separate playlist. The client switches to the most appropriate variant based on the measured network bit rate. The client’s player is tuned to minimize stalling of playback, to give the user the best possible streaming experience.

为同一份内容,提供多个码率的m3u8,方便客户端播放器选择适应自身网络状况的最佳码率资源。

结构图:

文件示例

1
2
3
4
5
6
7
8
9
10
11
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=150000,RESOLUTION=416x234,CODECS="avc1.42e00a,mp4a.40.2"
http://example.com/low/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=240000,RESOLUTION=416x234,CODECS="avc1.42e00a,mp4a.40.2"
http://example.com/lo_mid/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=440000,RESOLUTION=416x234,CODECS="avc1.42e00a,mp4a.40.2"
http://example.com/hi_mid/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=640000,RESOLUTION=640x360,CODECS="avc1.42e00a,mp4a.40.2"
http://example.com/high/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=64000,CODECS="mp4a.40.5"
http://example.com/audio/index.m3u8

EXT-X-STREAM-INF
Indicates that the next URL in the playlist file identifies another playlist file.

表明它的URL内容是一个m3u8文件。

The EXT-X-STREAM-INF tag has the following parameters:

AVERAGE-BANDWIDTH

(Optional, but recommended) An integer that represents the average bit rate for the variant stream.

平均码率,可选。

BANDWIDTH

(Required) An integer that is the upper bound of the overall bit rate for each media file, in bits per second. The upper bound value is calculated to include any container overhead that appears or will appear in the playlist.

带宽。

FRAME-RATE

(Optional, but recommended) A floating-point value that describes the maximum frame rate in a variant stream.

帧率,可选, 表示该流的最大帧率。

HDCP-LEVEL

(Optional) Indicates the type of encryption used. Valid values are TYPE-0 and NONE. Use TYPE-0 if the stream may not play unless the output is protected by HDCP.

加密方式,可选。

RESOLUTION

(Optional, but recommended) The optional display size, in pixels, at which to display the video in the playlist. This parameter should be included for any stream that includes video.

视频分辨率。

VIDEO-RANGE

(Required depending on encoding) A string with valid values of SDR or PQ. If transfer characteristic codes 1, 16, or 18 aren’t specified, then this parameter must be omitted.

CODECS

(Optional, but recommended) A quoted string containing a comma-separated list of formats, where each format specifies a media sample type that’s present in a media segment in the playlist file. Valid format identifiers are those in the ISO file format name space defined by RFC 6381.

编码器相关参数。

Adding Alternate Media to a Playlist

Specify Rendition Playlists that can override the main presentation.

Adding alternate media to a Multivariant Playlist allows a provider to specify one of a set of variant playlists as an override of the main presentation. The client plays only the override media (audio or video), and suppresses any media of the same type from the main presentation, if present. This allows a presentation to offer multiple versions of the media without requiring the provider to store duplicate media, or requiring the client to download all variants when it only needs one. It also allows additional media to be offered subsequently without remastering the original content.

在主音频视频资源外,提供多路备选资源。
选择备选的音视频,客户端只会播放备选音视频资源。

下面的例子,提供备选的多语言音频播放列表

1
2
3
4
5
6
7
8
9
#EXTM3U
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",LANGUAGE="eng",NAME="English",AUTOSELECT=YES, DEFAULT=YES,URI="eng/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",LANGUAGE="fre",NAME="Français",AUTOSELECT=YES, DEFAULT=NO,URI="fre/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",LANGUAGE="sp",NAME="Espanol",AUTOSELECT=YES, DEFAULT=NO,URI="sp/prog_index.m3u8"

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=195023,CODECS="avc1.42e00a,mp4a.40.2",AUDIO="audio"
lo/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=591680,CODECS="avc1.42e01e,mp4a.40.2",AUDIO="audio"
hi/prog_index.m3u8

The tags used in this Multivariant Playlist example include:

EXT-X-MEDIA
Identifies an element of a media selection group. All the elements of a media selection group must have similar characteristics, for example, the same codecs and the same maximum bandwidth.

表示媒体选项分组中的一个元素,同一个分组中的媒体具有相同的特征,如相同的编解码或带宽。

EXT-X-STREAM-INF
indicates that the next URL in the Multivariant Playlist identifies a Rendition Playlist. See Creating a Multivariant Playlist for the basic parameters.

表明它的内容是一个索引, 指向一个m3u8文件

The EXT-X-MEDIA tag has the following parameters:

TYPE
(Required) A string indicating the type of media. Valid values are AUDIO, VIDEO, SUBTITLES, and CLOSED-CAPTIONS.

媒体类型

  • AUDIO
  • VIDEO
  • SUBTITLES
  • CLOSED-CAPTIONS

GROUP-ID
(Required) A string specifying the group that the media selection belongs to.

分组标识,字符串,必须有。

LANGUAGE
(Optional) A string that identifies the primary language used in the media selection.

该选项中媒体文件对应的语言,可选。

NAME
(Required) A string that describes the primary language used in the media selection.

该选项中媒体文件对应的语言的字符串标识,必须。

AUTOSELECT
(Optional) A string that indicates that the client may play the media selection in the absence of explicit user preference. Valid values are YES and NO. If the value of DEFAULT is YES, this value must also be YES.

是否自动选择,可选。在用户偏好缺省的时候,指示客户端是否应该自动选择该选项。合法的值是YES和NO,
如果DEFAULT是YES, 该字段的值必须也是YES.

DEFAULT
(Optional) A string indicating that the media selection should be played if the user hasn’t selected another option. Valid values are YES and NO.

是否默认播放该选项,可选。用户没有选择其他选项的时候,是否默认播放该选项。

INSTREAM-ID
(Required for closed captions) A string that specifies a rendition within the segments in the media playlist. When the TYPE attribute is CLOSED-CAPTIONS, the INSTREAM-ID must have one of the following values: CC1, CC3, CC3, CC4, or SERVICEn, where n is between 1 and 63.

ASSOC-LANGUAGE
(Optional) A string containing a language tag for the rendition. An associated language is often different from the language specified in the LANGUAGE attribute.

关联的语言,可选。

CHANNELS
(Required when two renditions have the same codec but a different number of channels) An ordered string that indicates the maximum number of independent, simultaneous audio channels present in a media segment.

表明该音频文件有几个声道

URI
(Optional) A string containing a URI that identifies the media playlist file. If the TYPE is CLOSED-CAPTIONS, this attribute must be omitted. When this attribute is omitted, the media content is in the original variant.

媒体文件地址,可选。

When its URI attribute is omitted, the EXT-X-MEDIA tag can indicate that the media described is included in the URI of the EXT-X-STREAM-INF tag.

如果URI字段不存在,EXT-X-MEDIA描述的的媒体文件存在于EXT-X-STREAM-INF标签中。

The EXT-X-STREAM-INF tag has the following parameters:

AUDIO
(Optional) A quoted string that indicates the set of audio streams that may be used when playing the presentation. This value must match the value of the GROUP-ID attribute of an EXT-X-MEDIA tag elsewhere in the Multivariant Playlist whose TYPE attribute is AUDIO.

VIDEO
(Optional) A quoted string that indicates the set of video streams that may be used when playing the presentation. This value must match the value of the GROUP-ID attribute of an EXT-X-MEDIA tag elsewhere in the Multivariant Playlist whose TYPE attribute is VIDEO.

SUBTITLES
(Optional) A quoted string that indicates the set of subtitle renditions that can be used. This value must match the value of the GROUP-ID attribute of an EXT-X-MEDIA tag elsewhere in the Multivariant Playlist whose TYPE attribute is SUBTITLE.

CLOSED-CAPTIONS
(Optional) Either a quoted string that indicates the set of closed captions that can be used or an enumerated string with the value of NONE. When this value is a quoted string, it must match the value of the GROUP-ID attribute of an EXT-X-MEDIA tag elsewhere in the Multivariant Playlist whose TYPE attribute is CLOSED-CAPTIONS. If the enumerated value is NONE, all EXT-X-STREAM-INF tags must have this attribute with a value of NONE.

总结一下:

通过EXT-X-MEDIA可以定义资源组,可以有多个组,用GROUP-ID来区分, 有四种组类型

  • AUDIO
  • VIDEO
  • SUBTITLES
  • CLOSED-CAPTIONS

每个分组中流属性是相同,其参数可以通过关联的EXT-X-STREAM-INF指定
通过EXT-X-STREAM-INF定义资源组的流参数,带宽,编解码器信息。 它关联一个组ID,这个组内的所有备选媒体都具有相同的性质。

You can have multiple audio groups to allow changes in codes or bit rate. However, each audio group in a variant must have the same alternates in it. For example, you can’t have English in one audio group and leave it out of the other group. The following example defines two audio groups, one for low bit rates and one for high bit rates. Both audio groups define the same set of languages but are called based on the available bandwidth.

每个组可以提供多个码率的媒体文件,其码率可以通过EXT-X-STREAM-INF指定,虽然GROUP-ID相同,但是码率是不同的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#EXTM3U
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-lo",LANGUAGE="eng",NAME="English",AUTOSELECT=YES, DEFAULT=YES,URI="englo/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-lo",LANGUAGE="fre",NAME="Français",AUTOSELECT=YES, DEFAULT=NO,URI="frelo/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-lo",LANGUAGE="es",NAME="Espanol",AUTOSELECT=YES, DEFAULT=NO,URI="splo/prog_index.m3u8"

#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-hi",LANGUAGE="eng",NAME="English",AUTOSELECT=YES, DEFAULT=YES,URI="eng/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-hi",LANGUAGE="fre",NAME="Français",AUTOSELECT=YES, DEFAULT=NO,URI="fre/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio-hi",LANGUAGE="es",NAME="Espanol",AUTOSELECT=YES, DEFAULT=NO,URI="sp/prog_index.m3u8"

#EXT-X-STREAM-INF:BANDWIDTH=195023,CODECS="mp4a.40.5", AUDIO="audio-lo"
lo/prog_index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=260000,CODECS="avc1.42e01e,mp4a.40.2", AUDIO="audio-lo"
hi/prog_index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=591680,CODECS="mp4a.40.2, avc1.64001e", AUDIO="audio-hi"
lo/prog_index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=650000,CODECS="avc1.42e01e,mp4a.40.2", AUDIO="audio-hi"
hi/prog_index.m3u8

You can have both a group and a single stream in a playlist. This is often done when you have multiple camera angles that all use the same audio. Create a group for the video streams and then declare the single audio stream. The following example shows a playlist with three camera angles and a single audio stream:

一个视频组关联单个音频的情况。

1
2
3
4
5
6
7
8
#EXTM3U
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle1",AUTOSELECT=YES,DEFAULT=YES
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle2",AUTOSELECT=YES,DEFAULT=NO, URI="Angle2/500kbs/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle3",AUTOSELECT=YES,DEFAULT=NO, URI="Angle3/500kbs/prog_index.m3u8"

#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac",LANGUAGE="en",NAME="English",AUTOSELECT=YES, DEFAULT=YES,URI="eng/prog_index.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=754857,CODECS="mp4a.40.2,avc1.4d401e", VIDEO="500kbs",AUDIO="aac"
Angle1/500kbs/prog_index.m3u8

To provide different streams for different bit rates, a different video group ID is needed for each bit rate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#EXTM3U
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="200kbs",NAME="Angle1",AUTOSELECT=YES,DEFAULT=YES
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="200kbs",NAME="Angle2",AUTOSELECT=YES,DEFAULT=NO, URI="Angle2/200kbs/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="200kbs",NAME="Angle3",AUTOSELECT=YES,DEFAULT=NO, URI="Angle3/200kbs/prog_index.m3u8"

#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle1",AUTOSELECT=YES,DEFAULT=YES
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle2",AUTOSELECT=YES,DEFAULT=NO, URI="Angle2/500kbs/prog_index.m3u8"
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="500kbs",NAME="Angle3",AUTOSELECT=YES,DEFAULT=NO, URI="Angle3/500kbs/prog_index.m3u8"

#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac",LANGUAGE="en",NAME="English",AUTOSELECT=YES, DEFAULT=YES,URI="eng/prog_index.m3u8"

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=300000,CODECS="mp4a.40.2,avc1.4d401e", VIDEO="200kbs",AUDIO="aac"
Angle1/200kbs/prog_index.m3u

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=754857,CODECS="mp4a.40.2,avc1.4d401e", VIDEO="500kbs",AUDIO="aac"
Angle1/500kbs/prog_index.m3u8

参考:
https://developer.apple.com/documentation/http_live_streaming
https://aleen42.gitbooks.io/wiki/content/summary/m3u8/m3u8.html


HLS 介绍,M3U8 格式分析
https://yxibng.github.io/2022/10/13/hls/2022-10-13-HLS-介绍/
作者
yxibng
发布于
2022年10月13日
许可协议