下载 M3U8 文件(HLS 流媒体)可以通过多种命令行工具实现,以下是几种常用方法:
1. 使用 ffmpeg
(推荐)
ffmpeg
是功能强大的多媒体工具,支持直接下载并合并 M3U8 流。
# 基本命令
ffmpeg -i "https://example.com/video.m3u8" -c copy output.mp4
# 指定超时和重试(网络不稳定时)
ffmpeg -timeout 30 -i "URL" -c copy -bsf:a aac_adtstoasc output.mp4
# 限制下载速度(避免占用带宽)
ffmpeg -i "URL" -c copy -bandwidth 1M output.mp4
2. 使用 yt-dlp
yt-dlp
是 youtube-dl
的增强版,支持更多网站和流媒体协议。
# 安装
pip install yt-dlp
# 下载 M3U8
yt-dlp -o "output.mp4" "https://example.com/video.m3u8"
# 指定格式和代理
yt-dlp -f "best" --proxy "http://127.0.0.1:1080" "URL"
3. 使用 aria2
aria2
是多线程下载工具,需手动处理 M3U8 分片。
# 下载 M3U8 文件并解析 TS 分片链接
aria2c -i video.m3u8 --dir=output_dir
# 合并 TS 文件(需手动或脚本)
cat *.ts > output.ts
4. 使用 hlsdl
专用于 HLS 下载的工具,轻量高效。
# 安装(需编译)
git clone https://github.com/selsta/hlsdl
cd hlsdl && make
# 下载
./hlsdl -o output.ts "https://example.com/video.m3u8"
5. 使用 wget
或 curl
(手动下载分片)
适合简单场景,需手动解析 M3U8 文件并下载所有 TS 分片。
# 解析 M3U8 获取 TS 链接列表
grep ".ts" video.m3u8 > ts_list.txt
# 批量下载 TS 文件
wget -i ts_list.txt -P ts_files/
# 合并 TS 文件
cat ts_files/*.ts > output.ts
注意事项
- 版权问题:确保你有权下载目标内容。
- 网络稳定性:若下载中断,可使用
-c
参数(续传功能)。 - 合并文件:部分工具自动合并,手动合并需确保 TS 文件顺序正确。
根据需求选择工具,ffmpeg
和 yt-dlp
是综合最优解。