Capture RTP Live Stream Using Custom SDP in FFmpeg
Capturing a live stream over RTP (Real-time Transport Protocol) using FFmpeg often requires a Session Description Protocol (SDP) file to define the stream’s parameters, such as IP addresses, ports, and payload formats. Because RTP does not multiplex control and media data in the same way as protocols like RTMP or RTSP, FFmpeg needs this external SDP metadata to correctly decode the incoming packets. This guide provides a straightforward, step-by-step process for creating a custom SDP file and using FFmpeg to capture and save your live RTP stream.
Step 1: Create the Custom SDP File
An SDP file is a simple text file with the extension
.sdp that describes the multimedia session. You must
configure this file to match the properties of your incoming RTP
stream.
Create a file named stream.sdp in your working directory
and populate it with the following configuration (adjust the values to
match your stream):
v=0
o=- 0 0 IN IP4 127.0.0.1
s=RTP Video Stream
c=IN IP4 239.0.0.1
t=0 0
m=video 5004 RTP/AVP 96
a=rtpmap:96 H264/90000
Key Fields Explained:
c=(Connection Information): Specifies the network type (IN for Internet), address type (IP4), and the unicast or multicast IP address where the stream is being sent (e.g.,239.0.0.1or127.0.0.1).m=(Media Announcements): Defines the media type (video), port number (5004), transport protocol (RTP/AVP), and the payload type (96).a=rtpmap(Attributes): Maps the payload type (96) to the specific codec and clock rate (e.g.,H264/90000for H.264 video at 90 kHz).
If your stream also contains audio (for example, AAC audio on port 5006), append the audio details to the same SDP file:
m=audio 5006 RTP/AVP 97
a=rtpmap:97 MPEG4-GENERIC/48000/2
a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1190
Step 2: Run the FFmpeg Command
Once your stream.sdp file is configured and the RTP
stream is actively broadcasting, run the following FFmpeg command in
your terminal to capture and save the stream.
ffmpeg -protocol_whitelist file,udp,rtp -i stream.sdp -c copy output.mp4Command Breakdown:
-protocol_whitelist file,udp,rtp: This flag is mandatory. For security reasons, modern versions of FFmpeg require you to explicitly authorize the protocols needed to read the local SDP file (file) and receive the underlying network packets over UDP and RTP (udp,rtp).-i stream.sdp: Specifies your custom SDP file as the input source.-c copy: Copies the video and audio streams directly without re-encoding them. This saves CPU resources and preserves the original quality. Change this to specific encoders (e.g.,-c:v libx264 -c:a aac) if you need to transcode the stream.output.mp4: The output file name where the live stream will be saved.
Troubleshooting Common Issues
- Binding Errors / No Packets Received: Ensure your
firewall is not blocking the specified UDP ports (e.g.,
5004). If you are using multicast, ensure your network interface is configured to join the multicast group. - Invalid Payload Type: If FFmpeg throws a “Payload
type multiplexing not supported” or similar decoding error, double-check
that the payload ID in the
m=line matches the payload ID specified in thea=rtpmapline of your SDP file.