When using FFmpeg for HTTP live streaming, efficiency improvements can be achieved through various aspects, including encoding settings, transmission protocol selection, resource allocation, and optimization. Below, I will detail how to enhance live streaming efficiency by modifying FFmpeg commands.
1. Selecting the Appropriate Encoder and Parameters
During live streaming, selecting the appropriate encoder is crucial as it directly impacts the compression efficiency and quality of the video stream. For example, using x264 (H.264 encoder) or x265 (H.265 encoder) typically provides better compression.
Example:
When using the H.264 encoder, adjusting the preset can balance encoding speed and compression rate. For instance, using -preset veryfast increases encoding speed, suitable for live streaming scenarios.
bashffmpeg -i input.mp4 -c:v libx264 -preset veryfast -b:v 2500k -maxrate 2500k -bufsize 5000k -g 50 -c:a aac -b:a 128k -ar 44100 -f flv rtmp://live-streaming-server/live
2. Adjusting Keyframe Intervals
The setting of keyframe (I-frame) intervals has a significant impact on both the efficiency and quality of live streaming. Appropriately reducing the keyframe interval can improve the video's error recovery capability and make the viewing experience smoother when switching streams.
Example:
In FFmpeg commands, setting the keyframe interval to two keyframes per second.
bashffmpeg -i input.mp4 -c:v libx264 -g $(($(ffmpeg -i input.mp4 2>&1 | grep "Duration" | cut -d ' ' -f 4 | tr -d , | cut -d ':' -f 3 | cut -d '.' -f 1)*2)) -c:a aac -f flv rtmp://live-streaming-server/live
3. Using More Efficient Transmission Protocols
Although RTMP is the most commonly used live streaming transmission protocol, it is based on TCP, which may increase latency due to TCP congestion control mechanisms. Consider using UDP-based transmission protocols such as SRT or RIST, which better handle network instability and reduce latency.
Example:
Using the SRT protocol instead of RTMP for live streaming.
bashffmpeg -i input.mp4 -c:v libx264 -preset veryfast -g 50 -c:a aac -f mpegts srt://live-streaming-server:1234?mode=caller
4. Optimizing Audio Encoding Settings
For live content, audio typically does not require a high bitrate. Appropriately lowering the audio bitrate can save significant bandwidth while having minimal impact on the viewing experience.
Example:
Setting the audio bitrate to 96k.
bashffmpeg -i input.mp4 -c:v libx264 -preset veryfast -b:v 2500k -c:a aac -b:a 96k -f flv rtmp://live-streaming-server/live