Modern online video streaming relies heavily on HLS (HTTP Live Streaming). Instead of delivering a single large video file, HLS streams content as many small segments referenced by an m3u8 playlist. Understanding this structure makes it straightforward to download and reconstruct the original video using FFmpeg.
๐บ Understanding m3u8 and HLS #
An m3u8 file is a UTF-8 encoded text playlist that tells the video player what to download and in what order. Each entry typically points to a short .ts (MPEG Transport Stream) segment.
Typical m3u8 Components #
#EXTM3Uโ Identifies the file as an extended M3U playlist#EXTINFโ Duration of the next media segment- Segment URI โ URL of a
.tsfile #EXT-X-ENDLISTโ Marks the end of a VOD stream (absent in live streams)
Why HLS Uses m3u8 #
- Adaptive bitrate streaming โ Switches resolution dynamically (e.g., 720p โ 1080p)
- Fast seeking โ Jump to any point by loading specific segments
- Wide compatibility โ Native support across browsers, mobile OSes, and players
๐ ๏ธ Downloading m3u8 Streams with FFmpeg #
FFmpeg can consume an m3u8 playlist directly, download all referenced segments, and mux them into a single MP4 file automatically.
Basic Command #
ffmpeg -i "https://example.com/video.m3u8" -c copy output.mp4
Parameter Explanation #
-iโ Input source (the m3u8 URL)-c copyโ Stream copy mode (no re-encoding, zero quality loss)output.mp4โ Final merged video file
This approach is extremely fast because FFmpeg simply remuxes the original streams.
๐ Finding the m3u8 URL #
Most sites do not expose the playlist directly, but it can be discovered via browser tools.
- Open Developer Tools (
F12) - Go to the Network tab
- Filter requests by
.m3u8 - Play the video or refresh the page
- Copy the m3u8 request URL
Encryption Considerations #
-
AES-128 encryption (
#EXT-X-KEY)- FFmpeg can usually fetch and decrypt the stream automatically
-
DRM-protected streams (e.g., Widevine, PlayReady)
- Not supported by FFmpeg
โ๏ธ Advanced FFmpeg Options #
Some streams require extra flags to download reliably.
| Scenario | Useful Options |
|---|---|
| Avoid 403 errors | -user_agent "Mozilla/5.0" |
| Network instability | -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 |
| AAC audio issues | -bsf:a aac_adtstoasc |
Example with Headers and Audio Fix #
ffmpeg \
-user_agent "Mozilla/5.0" \
-i "https://example.com/video.m3u8" \
-c copy \
-bsf:a aac_adtstoasc \
output.mp4
The aac_adtstoasc bitstream filter is sometimes required when remuxing TS-based AAC audio into MP4 containers.
๐งพ Summary #
Downloading m3u8 videos with FFmpeg is the cleanest and most efficient approach:
- No screen recording
- No quality loss
- No unnecessary re-encoding
By understanding how HLS playlists work and leveraging FFmpegโs stream-copy mode, you can reliably reconstruct high-quality MP4 files directly from the source stream.