How to Generate SDP File for FFmpeg RTP Stream
To play back an RTP (Real-time Transport Protocol) stream using FFmpeg or media players like VLC, you need a Session Description Protocol (SDP) file that describes the stream’s format, codecs, destination, and port numbers. Because RTP does not transmit this metadata in-band, the receiver relies on the SDP file to understand how to decode the incoming network packets. This guide provides a straightforward, step-by-step process to generate and save this local SDP file directly using FFmpeg.
The Standard
Method: Using the -sdp_file Flag
The easiest and most reliable way to generate an SDP file is to use
FFmpeg’s built-in -sdp_file option during the stream
creation process. This flag automatically writes the required SDP
metadata to a local file on your disk while the stream is
initialized.
Run the following command to start an RTP stream and save the SDP file:
ffmpeg -re -i input.mp4 -vcodec libx264 -an -f rtp rtp://127.0.0.1:5004 -sdp_file stream.sdpParameter Breakdown: * -re: Reads the
input file at its native frame rate (essential for real-time streaming
simulation). * -i input.mp4: Specifies your source video
file. * -vcodec libx264: Encodes the video using the H.264
codec. * -an: Disables audio (simplifies the stream for
testing; omit this if you want to include audio). * -f rtp:
Sets the output format to RTP. * rtp://127.0.0.1:5004: The
destination IP address and port for the RTP stream. *
-sdp_file stream.sdp: Generates and writes the SDP
configuration to a local file named stream.sdp.
The Alternative Method: Redirecting Console Output
If you are using an older version of FFmpeg that does not support the
-sdp_file option, you can redirect the SDP information
printed to the command line during stream startup into a file.
By default, when you start an RTP stream without specifying an SDP
file path, FFmpeg outputs the SDP text to the standard output (stdout).
You can redirect this output using the > operator:
ffmpeg -re -i input.mp4 -vcodec libx264 -an -f rtp rtp://127.0.0.1:5004 > stream.sdpThis command creates a stream.sdp file containing the
necessary configuration parameters. Note that this method may
occasionally include unwanted console printouts in the file, so using
the -sdp_file flag is highly recommended.
How to Play Back the Stream Using the SDP File
Once you have saved the stream.sdp file locally, the
receiver can use it to play back the stream. Make sure the SDP file is
located on the machine attempting to view the stream.
To play the stream using FFplay, run:
ffplay -protocol_whitelist file,rtp,udp stream.sdpTo play the stream using VLC, simply open the VLC
player and drag and drop the stream.sdp file into the
player window, or open it via Media > Open File.