FFmpeg 是一个强大的开源多媒体处理工具,可以处理几乎所有类型的音视频格式。本文将详细介绍如何使用 FFmpeg 下载和转换 M3U8/HLS 视频流,包括基础命令、高级参数和常见问题解决方案。
安装 FFmpeg
在开始之前,你需要先安装 FFmpeg:
Windows 安装
- 访问 FFmpeg 官网 下载 Windows 版本
- 解压到任意目录,如
C:\ffmpeg - 将
C:\ffmpeg\bin添加到系统环境变量 PATH - 打开命令提示符,输入
ffmpeg -version验证安装
macOS 安装
# 使用 Homebrew 安装
brew install ffmpeg
Linux 安装
# Ubuntu/Debian
sudo apt update
sudo apt install ffmpeg
# CentOS/RHEL
sudo yum install ffmpeg
基础下载命令
最简单的 M3U8 下载命令:
ffmpeg -i "https://example.com/video.m3u8" -c copy output.mp4
参数说明:
-i:指定输入文件(M3U8 链接)-c copy:直接复制流,不重新编码(速度最快)output.mp4:输出文件名
-c copy 可以保持原始质量且速度最快。只有当遇到兼容性问题时才需要重新编码。
常用命令参数详解
处理认证和 Headers
某些 M3U8 流需要特定的 HTTP Headers 才能访问:
ffmpeg -headers "Referer: https://example.com\r\nUser-Agent: Mozilla/5.0\r\n" \
-i "https://example.com/video.m3u8" \
-c copy output.mp4
设置超时和重试
ffmpeg -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 \
-i "https://example.com/video.m3u8" \
-c copy output.mp4
指定时间范围
只下载视频的特定片段:
# 下载前 60 秒
ffmpeg -i "https://example.com/video.m3u8" -t 60 -c copy output.mp4
# 从第 30 秒开始下载 60 秒
ffmpeg -ss 30 -i "https://example.com/video.m3u8" -t 60 -c copy output.mp4
重新编码为兼容格式
当直接复制不兼容时,需要重新编码:
ffmpeg -i "https://example.com/video.m3u8" \
-c:v libx264 -preset fast -crf 23 \
-c:a aac -b:a 128k \
output.mp4
| 参数 | 说明 | 推荐值 |
|---|---|---|
-preset |
编码速度/质量平衡 | fast, medium, slow |
-crf |
视频质量 (0-51) | 18-28 (越低质量越好) |
-b:a |
音频比特率 | 128k, 192k, 256k |
处理加密 M3U8
部分 M3U8 流使用 AES-128 加密。如果你有解密密钥,可以这样下载:
# FFmpeg 会自动从 M3U8 中读取 KEY 并解密
ffmpeg -i "https://example.com/encrypted.m3u8" -c copy output.mp4
-headers 参数传递相应的认证信息。FFmpeg 无法处理 DRM
保护的视频。
完整示例脚本
以下是一个完整的下载脚本,包含错误处理和进度显示:
#!/bin/bash
# M3U8 下载脚本
URL="$1"
OUTPUT="${2:-output.mp4}"
if [ -z "$URL" ]; then
echo "用法: $0 [output_file]"
exit 1
fi
ffmpeg -y \
-reconnect 1 \
-reconnect_streamed 1 \
-reconnect_delay_max 5 \
-i "$URL" \
-c copy \
-bsf:a aac_adtstoasc \
"$OUTPUT"
echo "下载完成: $OUTPUT"
常见问题解决
Q: 报错 "Protocol 'https' not on whitelist"
添加 -protocol_whitelist file,http,https,tcp,tls,crypto 参数:
ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto \
-i "video.m3u8" -c copy output.mp4
Q: 下载速度慢或中断
- 添加重连参数:
-reconnect 1 -reconnect_streamed 1 - 增加超时时间:
-timeout 30000000 - 尝试使用代理或更换网络
Q: 输出文件无法播放
尝试重新编码而非直接复制:
ffmpeg -i "video.m3u8" -c:v libx264 -c:a aac output.mp4
Q: 音视频不同步
ffmpeg -i "video.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4
更简单的替代方案
如果你觉得命令行操作太复杂,可以使用我们的在线工具,无需安装任何软件:
相关阅读
总结
FFmpeg 是处理 M3U8 视频的强大工具,虽然需要一定的命令行知识,但掌握后可以处理各种复杂场景。记住以下关键点:
- 优先使用
-c copy直接复制,速度快且无损 - 遇到认证问题时添加
-headers参数 - 网络不稳定时添加重连参数
- 兼容性问题尝试重新编码
FFmpeg is a powerful open-source multimedia processing tool that can handle almost all audio and video formats. This article will detail how to use FFmpeg to download and convert M3U8/HLS video streams, including basic commands, advanced parameters, and common troubleshooting solutions.
Installing FFmpeg
Before getting started, you need to install FFmpeg:
Windows Installation
- Visit FFmpeg official website to download the Windows version
- Extract to any directory, e.g.,
C:\ffmpeg - Add
C:\ffmpeg\binto system PATH environment variable - Open Command Prompt and type
ffmpeg -versionto verify installation
macOS Installation
# Install using Homebrew
brew install ffmpeg
Linux Installation
# Ubuntu/Debian
sudo apt update
sudo apt install ffmpeg
# CentOS/RHEL
sudo yum install ffmpeg
Basic Download Command
The simplest M3U8 download command:
ffmpeg -i "https://example.com/video.m3u8" -c copy output.mp4
Parameter explanation:
-i: Specify input file (M3U8 URL)-c copy: Copy streams directly without re-encoding (fastest)output.mp4: Output filename
-c copy preserves original quality and is the
fastest option. Only re-encode when encountering compatibility issues.
Common Command Parameters
Handling Authentication and Headers
Some M3U8 streams require specific HTTP Headers to access:
ffmpeg -headers "Referer: https://example.com\r\nUser-Agent: Mozilla/5.0\r\n" \
-i "https://example.com/video.m3u8" \
-c copy output.mp4
Setting Timeout and Retry
ffmpeg -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 \
-i "https://example.com/video.m3u8" \
-c copy output.mp4
Specifying Time Range
Download only a specific portion of the video:
# Download first 60 seconds
ffmpeg -i "https://example.com/video.m3u8" -t 60 -c copy output.mp4
# Download 60 seconds starting from 30 seconds
ffmpeg -ss 30 -i "https://example.com/video.m3u8" -t 60 -c copy output.mp4
Re-encoding to Compatible Format
When direct copy is not compatible, re-encoding is needed:
ffmpeg -i "https://example.com/video.m3u8" \
-c:v libx264 -preset fast -crf 23 \
-c:a aac -b:a 128k \
output.mp4
| Parameter | Description | Recommended |
|---|---|---|
-preset |
Speed/quality balance | fast, medium, slow |
-crf |
Video quality (0-51) | 18-28 (lower = better) |
-b:a |
Audio bitrate | 128k, 192k, 256k |
Handling Encrypted M3U8
Some M3U8 streams use AES-128 encryption. If you have the decryption key, you can download like this:
# FFmpeg will automatically read KEY from M3U8 and decrypt
ffmpeg -i "https://example.com/encrypted.m3u8" -c copy output.mp4
-headers parameter. FFmpeg cannot handle DRM-protected
videos.
Complete Example Script
Here's a complete download script with error handling and progress display:
#!/bin/bash
# M3U8 Download Script
URL="$1"
OUTPUT="${2:-output.mp4}"
if [ -z "$URL" ]; then
echo "Usage: $0 [output_file]"
exit 1
fi
ffmpeg -y \
-reconnect 1 \
-reconnect_streamed 1 \
-reconnect_delay_max 5 \
-i "$URL" \
-c copy \
-bsf:a aac_adtstoasc \
"$OUTPUT"
echo "Download complete: $OUTPUT"
Troubleshooting
Q: Error "Protocol 'https' not on whitelist"
Add -protocol_whitelist file,http,https,tcp,tls,crypto parameter:
ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto \
-i "video.m3u8" -c copy output.mp4
Q: Slow download or interruptions
- Add reconnect parameters:
-reconnect 1 -reconnect_streamed 1 - Increase timeout:
-timeout 30000000 - Try using a proxy or different network
Q: Output file won't play
Try re-encoding instead of direct copy:
ffmpeg -i "video.m3u8" -c:v libx264 -c:a aac output.mp4
Q: Audio/video out of sync
ffmpeg -i "video.m3u8" -c copy -bsf:a aac_adtstoasc output.mp4
Easier Alternative
If you find command-line operations too complex, you can use our online tool without installing any software:
Related Reading
- How to Download M3U8 Video - Complete Guide
- Ultimate M3U8 to MP4 Conversion Guide
- 3 Ways to Merge TS Files into MP4
Summary
FFmpeg is a powerful tool for handling M3U8 videos. While it requires some command-line knowledge, mastering it allows you to handle various complex scenarios. Remember these key points:
- Prefer
-c copyfor direct copying - fastest and lossless - Add
-headersparameter for authentication issues - Add reconnect parameters for unstable networks
- Try re-encoding for compatibility issues