- 部落格文章
- Coming Soon
Coming Soon
Editorial Team
9/17/2025
Article
M3U8
FFmpeg M3U8 Practical Guide: 50 Essential Commands Explained
FFmpeg is the Swiss Army knife for M3U8/HLS streaming. Whether downloading, converting, segmenting, or streaming, FFmpeg can handle it all. This article presents 50 most practical FFmpeg commands to help you become an M3U8 processing expert.
FFmpeg Fundamentals
Installing FFmpeg
# macOS
brew install ffmpeg
# Ubuntu/Debian
sudo apt update && sudo apt install ffmpeg
# CentOS/RHEL
sudo yum install epel-release
sudo yum install ffmpeg
# Windows - Using Chocolatey
choco install ffmpeg
# Compile from source (more encoder support)
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --enable-gpl --enable-libx264 --enable-libx265 --enable-libvpx
make && sudo make install
Verify Installation
# Check version
ffmpeg -version
# Check supported encoders
ffmpeg -encoders | grep -E "264|265|vp9"
# Check supported formats
ffmpeg -formats | grep m3u8
Part 1: M3U8 Download Commands (10)
1. Basic Download
ffmpeg -i "https://example.com/video.m3u8" -c copy output.mp4
2. Set Timeout and Reconnect
ffmpeg -timeout 5000000 \
-reconnect 1 \
-reconnect_at_eof 1 \
-reconnect_streamed 1 \
-reconnect_delay_max 2 \
-i "URL" -c copy output.mp4
3. Add Request Headers
ffmpeg -headers $'Referer: https://example.com\r\nCookie: session=abc123' \
-i "URL" -c copy output.mp4
4. Use Proxy
ffmpeg -http_proxy http://proxy.example.com:8080 \
-i "URL" -c copy output.mp4
5. Rate-Limited Download
ffmpeg -i "URL" -c copy -re output.mp4
6. Download Specific Time Range
# Start from 10 minutes, download 5 minutes
ffmpeg -ss 00:10:00 -t 00:05:00 -i "URL" -c copy output.mp4
7. Download and Transcode
ffmpeg -i "URL" \
-c:v libx264 -preset fast -crf 23 \
-c:a aac -b:a 128k \
output.mp4
8. Download Multiple Streams
# Download video and audio streams
ffmpeg -i "video.m3u8" -i "audio.m3u8" \
-map 0:v -map 1:a -c copy output.mp4
9. Handle Encrypted M3U8
ffmpeg -allowed_extensions ALL \
-protocol_whitelist "file,http,https,tcp,tls,crypto" \
-i "encrypted.m3u8" -c copy output.mp4
10. Batch Download Script
#!/bin/bash
while IFS= read -r url; do
filename=$(basename "$url" .m3u8).mp4
ffmpeg -i "$url" -c copy "$filename"
done < urls.txt
Part 2: M3U8 Conversion Commands (10)
11. M3U8 to MP4 (Preserve Quality)
ffmpeg -i input.m3u8 -c copy -bsf:a aac_adtstoasc output.mp4
12. Convert and Compress
ffmpeg -i input.m3u8 \
-c:v libx264 -crf 28 \
-c:a aac -b:a 96k \
-movflags +faststart \
output.mp4
13. Convert to WebM
ffmpeg -i input.m3u8 \
-c:v libvpx-vp9 -crf 30 -b:v 0 \
-c:a libopus -b:a 128k \
output.webm
14. Extract Audio
# Extract to MP3
ffmpeg -i input.m3u8 -vn -c:a libmp3lame -b:a 192k output.mp3
# Extract to AAC
ffmpeg -i input.m3u8 -vn -c:a copy output.aac
15. Change Resolution
# Convert to 720p
ffmpeg -i input.m3u8 \
-vf scale=1280:720 \
-c:v libx264 -crf 23 \
-c:a copy \
output.mp4
16. Add Watermark
ffmpeg -i input.m3u8 \
-i watermark.png \
-filter_complex "overlay=10:10" \
-c:v libx264 -crf 23 \
-c:a copy \
output.mp4
17. Crop Video
# Crop to 16:9
ffmpeg -i input.m3u8 \
-vf "crop=1920:1080:0:0" \
-c:v libx264 -crf 23 \
-c:a copy \
output.mp4
18. Adjust Frame Rate
# Convert to 30fps
ffmpeg -i input.m3u8 \
-r 30 \
-c:v libx264 -crf 23 \
-c:a copy \
output.mp4
19. Merge Multiple M3U8
# Create file list
echo "file 'part1.m3u8'" > list.txt
echo "file 'part2.m3u8'" >> list.txt
# Merge
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
20. Convert to GIF
ffmpeg -i input.m3u8 \
-vf "fps=10,scale=320:-1:flags=lanczos" \
-t 10 \
output.gif
Part 3: Create M3U8 Commands (10)
21. Basic Segmentation
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-hls_time 10 \
-hls_list_size 0 \
-f hls \
output.m3u8
22. Multi-bitrate HLS
# 1080p
ffmpeg -i input.mp4 \
-c:v libx264 -b:v 5000k -s 1920x1080 \
-c:a aac -b:a 192k \
-hls_time 10 -hls_list_size 0 \
-f hls stream_1080p.m3u8
# 720p
ffmpeg -i input.mp4 \
-c:v libx264 -b:v 2800k -s 1280x720 \
-c:a aac -b:a 128k \
-hls_time 10 -hls_list_size 0 \
-f hls stream_720p.m3u8
# Create master playlist
cat > master.m3u8 << EOF
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=5192000,RESOLUTION=1920x1080
stream_1080p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2928000,RESOLUTION=1280x720
stream_720p.m3u8
EOF
23. Encrypted HLS Stream
# Generate key
openssl rand 16 > encrypt.key
# Create key info file
cat > encrypt.keyinfo << EOF
https://example.com/encrypt.key
encrypt.key
$(openssl rand -hex 16)
EOF
# Segment and encrypt
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-hls_time 10 \
-hls_key_info_file encrypt.keyinfo \
-hls_playlist_type vod \
-hls_segment_filename "segment_%03d.ts" \
output.m3u8
24. Set Segment Naming
ffmpeg -i input.mp4 \
-c copy \
-hls_time 10 \
-hls_segment_filename "video_%Y%m%d_%03d.ts" \
-strftime 1 \
output.m3u8
25. Add Subtitles
ffmpeg -i input.mp4 -i subtitles.srt \
-c:v libx264 -c:a aac \
-c:s webvtt \
-hls_time 10 \
-var_stream_map "v:0,a:0,s:0" \
output.m3u8
26. Generate Thumbnails
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-hls_time 10 \
-vf "fps=1/10,scale=160:90" \
-hls_segment_filename "segment_%03d.ts" \
-f hls \
-hls_flags program_date_time+append_list \
output.m3u8
27. Set Maximum Segments
ffmpeg -i input.mp4 \
-c copy \
-hls_time 10 \
-hls_list_size 5 \
-hls_flags delete_segments \
output.m3u8
28. Create VOD Playlist
ffmpeg -i input.mp4 \
-c copy \
-hls_time 10 \
-hls_playlist_type vod \
-hls_segment_type mpegts \
output.m3u8
29. Use fMP4 Container
ffmpeg -i input.mp4 \
-c:v libx264 -c:a aac \
-hls_time 10 \
-hls_segment_type fmp4 \
-hls_fmp4_init_filename "init.mp4" \
output.m3u8
30. Custom HLS Tags
ffmpeg -i input.mp4 \
-c copy \
-hls_time 10 \
-hls_flags independent_segments \
-hls_segment_options "mpegts_flags=+initial_discontinuity" \
output.m3u8
Part 4: Live Streaming Commands (10)
31. Stream to RTMP
ffmpeg -re -i input.m3u8 \
-c copy \
-f flv rtmp://live.example.com/live/stream_key
32. Screen Recording Stream
# macOS
ffmpeg -f avfoundation -i "1:0" \
-c:v libx264 -preset fast -crf 22 \
-c:a aac -b:a 128k \
-f hls -hls_time 4 \
output.m3u8
# Windows
ffmpeg -f gdigrab -i desktop \
-c:v libx264 -preset fast \
-f hls output.m3u8
33. Webcam Live Stream
# Linux
ffmpeg -f v4l2 -i /dev/video0 \
-c:v libx264 -preset fast \
-f hls -hls_time 2 \
-hls_list_size 3 \
-hls_flags delete_segments \
live.m3u8
34. Loop Playback
ffmpeg -stream_loop -1 -re -i video.mp4 \
-c copy \
-f hls \
-hls_time 4 \
-hls_flags delete_segments+append_list \
loop.m3u8
35. Multi-stream Mixing
ffmpeg -i input1.m3u8 -i input2.m3u8 \
-filter_complex "[0:v][1:v]hstack=inputs=2[v]" \
-map "[v]" -map 0:a \
-c:v libx264 -c:a aac \
-f hls output.m3u8
36. Add Live Subtitles
ffmpeg -i input.m3u8 \
-vf "drawtext=text='%{localtime}':fontsize=24:fontcolor=white:box=1:boxcolor=black" \
-c:v libx264 -c:a copy \
-f hls output.m3u8
37. Audio Visualization
ffmpeg -i audio.mp3 \
-filter_complex "[0:a]showwaves=s=1280x720:mode=line:rate=25[v]" \
-map "[v]" -map 0:a \
-c:v libx264 -c:a aac \
-f hls visualization.m3u8
38. Delayed Live Stream
ffmpeg -i live_input.m3u8 \
-c copy \
-muxdelay 30 \
-f hls delayed_output.m3u8
39. Record Live Stream
ffmpeg -i "https://live.example.com/stream.m3u8" \
-c copy \
-f segment \
-segment_time 3600 \
-segment_format mp4 \
-strftime 1 \
"recording_%Y%m%d_%H%M%S.mp4"
40. Forward to Multiple Targets
ffmpeg -i input.m3u8 \
-c copy -f hls output1.m3u8 \
-c copy -f hls output2.m3u8 \
-c copy -f flv rtmp://server/live/stream
Part 5: Advanced Processing Commands (10)
41. GPU Hardware Acceleration (NVIDIA)
ffmpeg -hwaccel cuda -hwaccel_output_format cuda \
-i input.m3u8 \
-c:v h264_nvenc -preset fast \
-c:a copy \
output.mp4
42. Intel Quick Sync Acceleration
ffmpeg -hwaccel qsv \
-i input.m3u8 \
-c:v h264_qsv -preset fast \
-c:a copy \
output.mp4
43. Batch Processing Script
#!/bin/bash
find . -name "*.m3u8" | while read file; do
output="${file%.m3u8}.mp4"
ffmpeg -i "$file" -c copy "$output" &
done
wait
44. Quality Analysis
# PSNR and SSIM analysis
ffmpeg -i original.mp4 -i compressed.mp4 \
-lavfi "psnr=stats_file=psnr.log;[0:v][1:v]ssim=stats_file=ssim.log" \
-f null -
45. Audio-Video Sync Repair
# Delay audio by 100ms
ffmpeg -i input.m3u8 \
-itsoffset 0.1 -i input.m3u8 \
-map 0:v -map 1:a \
-c copy \
output.mp4
46. Extract Keyframes
ffmpeg -i input.m3u8 \
-vf "select='eq(pict_type,I)'" \
-vsync vfr \
keyframe_%04d.png
47. Create Video Wall
ffmpeg -i input1.m3u8 -i input2.m3u8 -i input3.m3u8 -i input4.m3u8 \
-filter_complex \
"[0:v]scale=640:360[v0]; \
[1:v]scale=640:360[v1]; \
[2:v]scale=640:360[v2]; \
[3:v]scale=640:360[v3]; \
[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|640_0|0_360|640_360[v]" \
-map "[v]" \
-c:v libx264 \
-f hls \
mosaic.m3u8
48. Color Space Conversion
ffmpeg -i input.m3u8 \
-vf "colorspace=all=bt709:iall=bt601-6-625:fast=1" \
-c:v libx264 -crf 18 \
-c:a copy \
output.mp4
49. Denoise Processing
ffmpeg -i input.m3u8 \
-vf "hqdn3d=4.0:3.0:6.0:4.5" \
-c:v libx264 -crf 20 \
-c:a copy \
output.mp4
50. Generate Playback Statistics
ffmpeg -i input.m3u8 \
-vf "signalstats,metadata=print:file=stats.txt" \
-f null -
Performance Optimization Tips
CPU Optimization
# Use all CPU cores
ffmpeg -i input.m3u8 -threads 0 -c:v libx264 output.mp4
# Specify thread count
ffmpeg -i input.m3u8 -threads 8 -c:v libx264 output.mp4
# Set encoding preset (ultrafast to veryslow)
ffmpeg -i input.m3u8 -c:v libx264 -preset fast output.mp4
Memory Optimization
# Limit buffer size
ffmpeg -i input.m3u8 \
-max_muxing_queue_size 1024 \
-c copy output.mp4
# Set analysis duration
ffmpeg -analyzeduration 10M -probesize 10M \
-i input.m3u8 -c copy output.mp4
Network Optimization
# Set buffer size
ffmpeg -rtbufsize 100M -i input.m3u8 -c copy output.mp4
# Set TCP options
ffmpeg -rtsp_transport tcp -i input.m3u8 -c copy output.mp4
Common Problem Handling
Problem 1: Timestamp Errors
# Regenerate timestamps
ffmpeg -i input.m3u8 -fflags +genpts -c copy output.mp4
Problem 2: Audio-Video Out of Sync
# Use async parameter
ffmpeg -i input.m3u8 -async 1 -c copy output.mp4
Problem 3: Missing Segments
# Ignore errors and continue
ffmpeg -err_detect ignore_err -i input.m3u8 -c copy output.mp4
Problem 4: Encoding Errors
# Force encoding format
ffmpeg -i input.m3u8 \
-c:v libx264 -profile:v baseline -level 3.0 \
-c:a aac -strict experimental \
output.mp4
Debugging and Analysis
Display Detailed Information
# Show stream info
ffmpeg -i input.m3u8
# Show detailed logs
ffmpeg -v debug -i input.m3u8 -c copy output.mp4
# Analyze only, no processing
ffmpeg -i input.m3u8 -f null -
Performance Analysis
# Show processing speed
ffmpeg -i input.m3u8 -c copy -progress pipe:1 output.mp4
# Benchmark
ffmpeg -i input.m3u8 -c:v libx264 -preset fast -f null - -benchmark
FFmpeg Configuration File
Create ~/.ffmpeg/ffmpeg.conf
:
# Default parameters
-hide_banner
-loglevel warning
-stats
# Hardware acceleration
-hwaccel auto
# Network settings
-timeout 5000000
-reconnect 1
-reconnect_at_eof 1
Practical Script Examples
Batch Conversion Script
#!/bin/bash
# batch_convert.sh
INPUT_DIR="./input"
OUTPUT_DIR="./output"
THREADS=4
mkdir -p "$OUTPUT_DIR"
export -f convert_file
convert_file() {
local input="$1"
local output="$2"
ffmpeg -i "$input" \
-c:v libx264 -preset fast -crf 23 \
-c:a aac -b:a 128k \
-movflags +faststart \
"$output"
}
find "$INPUT_DIR" -name "*.m3u8" | \
parallel -j "$THREADS" convert_file {} "$OUTPUT_DIR/{/.}.mp4"
Monitor Download Script
#!/bin/bash
# monitor_download.sh
URL="$1"
OUTPUT="$2"
while true; do
ffmpeg -i "$URL" \
-c copy \
-t 3600 \
-f segment \
-segment_time 300 \
-strftime 1 \
"${OUTPUT}_%Y%m%d_%H%M%S.mp4"
sleep 1
done
Conclusion
FFmpeg is a powerful tool for M3U8/HLS processing. Master these 50 commands, and you can handle most scenarios:
- Download Scenarios - Use appropriate timeout and reconnect parameters
- Conversion Scenarios - Choose encoding parameters based on requirements
- Creation Scenarios - Set segment time and list size reasonably
- Live Streaming - Pay attention to buffer and delay settings
- Optimization Scenarios - Fully utilize hardware acceleration
Remember, FFmpeg's power lies in its flexibility. These commands can be combined and adjusted according to specific needs. If you just need simple M3U8 video playback, using M3U8 Player would be more convenient.