6月1日 23:59
How to use FFmpeg for RTMP streaming and HLS/DASH streaming media generation?
FFmpeg is an important tool for streaming media processing, supporting various streaming protocols such as RTMP, HLS, and DASH.
Push to RTMP Server
Basic Push Command
bash# Push local file to RTMP server ffmpeg -re -i input.mp4 -c copy -f flv rtmp://server/live/stream_key # Real-time encoding push ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 1500k -c:a aac -b:a 128k -f flv rtmp://server/live/stream_key
Camera/Microphone Real-time Push
bash# macOS camera push ffmpeg -f avfoundation -i "0" -c:v libx264 -preset veryfast -b:v 1500k -f flv rtmp://server/live/stream_key # Camera + microphone push ffmpeg -f avfoundation -i "0:0" -c:v libx264 -preset veryfast -b:v 1500k -c:a aac -b:a 128k -f flv rtmp://server/live/stream_key
HLS Streaming Generation
Generate HLS Stream
bash# Generate HLS playlist ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 0 output.m3u8 # Generate multi-bitrate HLS ffmpeg -i input.mp4 \ -vf scale=1920:1080 -c:v libx264 -b:v 5000k -f hls -hls_time 10 hls_1080p.m3u8 \ -vf scale=1280:720 -c:v libx264 -b:v 2500k -f hls -hls_time 10 hls_720p.m3u8 \ -vf scale=854:480 -c:v libx264 -b:v 1000k -f hls -hls_time 10 hls_480p.m3u8
HLS Parameters
-hls_time: Duration of each segment (seconds)-hls_list_size: Number of segments to keep in playlist (0 means keep all)-hls_segment_filename: Segment file naming template-hls_flags: HLS special flags (e.g., delete_segments, append_list)
DASH Streaming Generation
Generate DASH Stream
bash# Generate DASH manifest ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f dash -seg_duration 10 output.mpd # Generate multi-bitrate DASH ffmpeg -i input.mp4 \ -vf scale=1920:1080 -c:v libx264 -b:v 5000k -f dash -seg_duration 10 dash_1080p.mpd \ -vf scale=1280:720 -c:v libx264 -b:v 2500k -f dash -seg_duration 10 dash_720p.mpd
Pull Stream Processing
Pull from RTMP
bash# Pull RTMP stream and save ffmpeg -i rtmp://server/live/stream -c copy output.mp4 # Pull RTMP stream and transcode ffmpeg -i rtmp://server/live/stream -c:v libx264 -preset fast -b:v 1500k -c:a aac -b:a 128k output.mp4
Pull from HLS
bash# Download HLS stream ffmpeg -i http://server/live/stream.m3u8 -c copy output.mp4 # Play HLS stream in real-time ffmpeg -i http://server/live/stream.m3u8 -f null -
Streaming Optimization
Latency Optimization
bash# Low latency push ffmpeg -re -i input.mp4 -c:v libx264 -preset ultrafast -tune zerolatency -b:v 1500k -c:a aac -b:a 128k -f flv rtmp://server/live/stream_key # Use GOP cache ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -g 25 -keyint_min 25 -b:v 1500k -c:a aac -b:a 128k -f flv rtmp://server/live/stream_key
Bitrate Control
bash# CBR (Constant Bitrate) ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 1500k -maxrate 1500k -minrate 1500k -bufsize 3000k -f flv rtmp://server/live/stream_key # VBR (Variable Bitrate) ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -crf 23 -maxrate 3000k -bufsize 6000k -f flv rtmp://server/live/stream_key
Common Streaming Protocols
| Protocol | Use | Features |
|---|---|---|
| RTMP | Real-time push | Low latency, widely used for live streaming |
| HLS | HTTP Live Streaming | Good compatibility, supports adaptive bitrate |
| DASH | HTTP Live Streaming | Open standard, cross-platform support |
| RTSP | Real-time Streaming Protocol | Used for surveillance and IPC |
| UDP/RTP | Real-time Transport | Low latency, suitable for LAN |
When processing streaming media, you need to select appropriate protocols and encoding parameters based on network conditions, latency requirements, and target platforms.