Fix RTP Missed Packet Warnings in FFmpeg
When capturing RTSP video streams using FFmpeg, the "RTP: missed packet" warning indicates dropped data packets between the media source and the demuxer. This issue typically stems from packet loss over unreliable UDP transport, insufficient buffer sizes causing system-level packet drops, or processing bottlenecks where FFmpeg cannot consume the incoming stream quickly enough. Resolving this error involves forcing reliable network protocols like TCP, expanding system and input buffers, or optimizing hardware resource allocation to prevent dropped frames and stream corruption.
1. Switch to TCP Transport
By default, RTSP often streams over UDP, which discards packets if network jitter occurs or packets arrive out of order. Switching the transport protocol to TCP forces packet retransmission and prevents missed packet errors.
Add the -rtsp_transport tcp flag before your input
source:
ffmpeg -rtsp_transport tcp -i rtsp://username:password@camera_ip:554/stream -c copy output.mp4Using TCP is the most effective solution for resolving missed packets, provided the network has sufficient bandwidth to handle retransmissions.
2. Increase the UDP Buffer Size
If your environment strictly requires UDP (due to low-latency demands or camera firmware restrictions), you must increase FFmpeg’s socket receive buffer. The default buffer is often too small to handle sudden bursts of video keyframes.
Use the -buffer_size option to increase the socket
buffer (e.g., to 10MB):
ffmpeg -buffer_size 10240k -i rtsp://username:password@camera_ip:554/stream -c copy output.mp4On Linux systems, you may also need to increase the OS-level maximum socket receive buffer size. You can expand it temporarily by running:
sudo sysctl -w net.core.rmem_max=26214400
sudo sysctl -w net.core.rmem_default=262144003. Increase the Real-Time Buffer
If FFmpeg falls behind while processing, decoding, or remuxing
frames, the internal capture queue overflows. You can increase the input
queue memory via the -rtbufsize flag:
ffmpeg -rtsp_transport udp -rtbufsize 100M -i rtsp://username:password@camera_ip:554/stream -c copy output.mp4This allocates a larger chunk of memory to temporarily queue incoming frames during CPU usage spikes.
4. Optimize Stream Demuxing and Probing
Lengthy stream analysis can cause incoming RTP packets to queue and drop before processing begins. Reduce the probe size and analysis duration to start consumption immediately:
ffmpeg -probesize 32 -analyzeduration 0 -rtsp_transport tcp -i rtsp://camera_ip:554/stream -c copy output.mp45. Reduce CPU Bottlenecks
If the warnings persist while re-encoding, FFmpeg may be dropping packets because the CPU cannot process the video in real time. To resolve this:
- Use stream copying: Use
-c:v copy -c:a copywhenever possible instead of re-encoding. - Enable hardware acceleration: If transcoding is
necessary, offload the workload using hardware decoders such as
-hwaccel cudaor-hwaccel vaapi. - Lower source complexity: Reduce the camera's output bitrate, resolution, or frame rate to match the network and processing capacity.