📡 什么是 HLS 流媒体?M3U8 格式详解

📡 What Is HLS Streaming? M3U8 Explained

HLS (HTTP Live Streaming) 是目前最流行的视频流媒体协议之一。理解它的工作原理,有助于你更好地下载和处理在线视频。

HLS 协议简介

HLS (HTTP Live Streaming) 是苹果公司在 2009 年开发的流媒体协议。它的核心思想是:

  • 将视频切割成小片段(通常 2-10 秒)
  • 使用标准 HTTP 协议传输
  • 通过播放列表(M3U8)管理分片
  • 支持自适应码率(不同网速播放不同质量)
为什么 HLS 如此流行?
HLS 使用标准 HTTP,可以利用现有的 CDN 基础设施,无需特殊服务器。几乎所有现代浏览器和设备都原生支持 HLS。

HLS 工作流程

┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│  原始视频   │ →  │   编码器    │ →  │  TS 分片    │
│  (MP4等)    │    │  (FFmpeg)   │    │ (2-10秒/片) │
└─────────────┘    └─────────────┘    └─────────────┘
                                              ↓
┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   播放器    │ ←  │  HTTP/CDN   │ ←  │ M3U8 播放列表│
│  (浏览器)   │    │   (传输)    │    │ (索引文件)  │
└─────────────┘    └─────────────┘    └─────────────┘
          

M3U8 文件结构

M3U8 是一个 UTF-8 编码的文本文件,包含元数据和分片 URL 列表:

基本播放列表示例

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0

#EXTINF:10.0,
segment_0.ts
#EXTINF:10.0,
segment_1.ts
#EXTINF:10.0,
segment_2.ts
#EXTINF:8.5,
segment_3.ts

#EXT-X-ENDLIST

关键标签解释

  • #EXTM3U:文件头,表示这是 M3U8 文件
  • #EXT-X-VERSION:HLS 协议版本
  • #EXT-X-TARGETDURATION:分片最大时长(秒)
  • #EXTINF:下一个分片的实际时长
  • #EXT-X-ENDLIST:表示播放列表结束(VOD)

变体播放列表(多码率)

为了支持自适应码率,HLS 使用主播放列表指向不同质量的子播放列表:

#EXTM3U
#EXT-X-VERSION:3

#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
low/playlist.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=1280x720
mid/playlist.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1920x1080
high/playlist.m3u8

播放器会根据用户的网络状况自动选择合适的质量流。

TS 分片格式

MPEG-TS (Transport Stream) 是 HLS 使用的容器格式:

  • 为流媒体设计,支持错误恢复
  • 固定 188 字节的数据包
  • 通常包含 H.264 视频和 AAC 音频
  • 每个 .ts 文件是一个独立可播放的片段

加密与 DRM

HLS 支持内容保护:

AES-128 加密

#EXT-X-KEY:METHOD=AES-128,URI="https://example.com/key.bin"

每个分片使用 AES-128 加密,密钥从指定 URL 获取。

FairPlay DRM

苹果的专有 DRM 方案,用于保护付费内容,需要授权才能播放。

HLS vs 其他协议

协议 延迟 兼容性 使用场景
HLS 高 (10-30秒) 极好 点播、直播
DASH 中 (5-15秒) 点播
WebRTC 极低 (<1秒) 实时通信

如何处理 HLS 视频

了解了 HLS 原理后,处理这类视频就很简单了:

  1. 获取 M3U8 播放列表 URL
  2. 解析播放列表,获取所有 TS 分片 URL
  3. 下载所有 TS 分片
  4. 合并分片并转换为 MP4

我们的 M3U8 Converter 工具自动完成以上所有步骤。

如果你正在搭建自己的点播链路,需要从源视频开始打包,可查看 MP4 转 M3U8:视频转 HLS 实战指南

HLS (HTTP Live Streaming) is one of the most widely used video streaming protocols. Understanding its structure makes downloading, troubleshooting, and conversion much easier.

HLS Basics

HLS was introduced by Apple in 2009. The core design is:

  • Split video into short segments (typically 2-10 seconds)
  • Deliver content over standard HTTP
  • Use M3U8 playlists to manage segment order
  • Support adaptive bitrate for different network conditions
Why HLS became mainstream
It works with standard web/CDN infrastructure and has strong compatibility across modern browsers and devices.

How HLS Works

┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│ Source Video│ →  │   Encoder   │ →  │ TS Segments │
│ (MP4, etc.) │    │  (FFmpeg)   │    │ (2-10s each)│
└─────────────┘    └─────────────┘    └─────────────┘
                                              ↓
┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   Player    │ ←  │  HTTP / CDN │ ←  │ M3U8 Playlist│
│  (Browser)  │    │ (Delivery)  │    │  (Index File)│
└─────────────┘    └─────────────┘    └─────────────┘
          

M3U8 File Structure

M3U8 is a UTF-8 text playlist containing metadata and segment references.

Basic Media Playlist Example

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0

#EXTINF:10.0,
segment_0.ts
#EXTINF:10.0,
segment_1.ts
#EXTINF:10.0,
segment_2.ts
#EXTINF:8.5,
segment_3.ts

#EXT-X-ENDLIST

Key Tags

  • #EXTM3U: identifies the file as M3U8 playlist
  • #EXT-X-VERSION: HLS version
  • #EXT-X-TARGETDURATION: max segment duration
  • #EXTINF: duration of the next segment
  • #EXT-X-ENDLIST: marks VOD playlist end

Variant Playlists (Adaptive Bitrate)

HLS supports multiple quality levels through a master playlist:

#EXTM3U
#EXT-X-VERSION:3

#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
low/playlist.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=1280x720
mid/playlist.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1920x1080
high/playlist.m3u8

Players switch quality automatically based on current bandwidth and buffer state.

TS Segment Format

MPEG-TS is the classic container used by HLS:

  • Designed for streaming and error resilience
  • Uses fixed 188-byte packet units
  • Commonly carries H.264 video + AAC audio
  • Each segment can usually be decoded independently

Encryption and DRM

HLS can protect premium content using encryption and DRM.

AES-128 Encryption

#EXT-X-KEY:METHOD=AES-128,URI="https://example.com/key.bin"

Segments are encrypted and require key access for playback/decryption.

FairPlay DRM

Apple's DRM solution for licensed content with secure key and playback policies.

HLS vs Other Protocols

Protocol Latency Compatibility Typical Use
HLS Higher (10-30s) Excellent VOD, live streaming
DASH Medium (5-15s) Good Mainly VOD
WebRTC Ultra-low (<1s) Medium Real-time communication

How to Process HLS Video

Once you understand HLS structure, the workflow is straightforward:

  1. Get the M3U8 playlist URL
  2. Parse playlist and collect TS segment URLs
  3. Download all segments
  4. Merge and convert to MP4

M3U8 Converter automates this entire pipeline in one place.

If you are building your own HLS pipeline from source files, read MP4 to M3U8: Convert Video to HLS Step by Step.