How to Stream UDP with RTP Encapsulation Using FFmpeg
This guide provides a straightforward tutorial on how to stream video and audio over UDP with RTP encapsulation using FFmpeg. You will learn the exact command-line syntax required, how to generate the essential Session Description Protocol (SDP) file needed for receivers to decode the stream, and how to play back the stream using a media player.
The FFmpeg Command for RTP Streaming
To stream a media file over UDP using RTP encapsulation, you must use
the FFmpeg rtp muxer. Because RTP requires the receiver to
know the stream configuration beforehand, you also need to output an SDP
file.
Run the following command in your terminal:
ffmpeg -re -i input.mp4 -an -c:v libx264 -f rtp rtp://127.0.0.1:5004 > stream.sdpCommand Breakdown:
-re: Reads the input file at its native frame rate. This is crucial for simulating a live stream.-i input.mp4: Specifies your source video file.-an: Disables audio (simplifies this initial setup to a single video track).-c:v libx264: Encodes the video to H.264, which is widely supported by RTP receivers.-f rtp: Forces the output format to RTP.rtp://127.0.0.1:5004: The destination IP address and port. Replace127.0.0.1with the receiver’s IP address if streaming across a network.> stream.sdp: Redirects the console output containing the SDP configuration into a file namedstream.sdp.
Streaming Video and Audio Together
If you want to stream both video and audio, RTP requires separate ports for each stream. FFmpeg handles this automatically by assigning the audio stream to the next available even port (e.g., if video is on 5004, audio will go to 5006).
Use this command for a combined audio and video stream:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f rtp rtp://127.0.0.1:5004 > stream.sdpHow to Play the RTP Stream
Because RTP does not have an in-band handshake, the receiving player
cannot decode the stream without the stream.sdp file
generated by your FFmpeg command.
Option 1: Playing with VLC
- Copy the generated
stream.sdpfile to the receiving computer. - Open VLC Media Player.
- Drag and drop the
stream.sdpfile into VLC, or open it via File > Open File. VLC will read the SDP file, connect to the specified UDP port, and begin playing the stream.
Option 2: Playing with FFplay
If you have FFmpeg installed on the receiving machine, you can play the stream directly from the command line using FFplay:
ffplay -protocol_whitelist file,rtp,udp stream.sdp