Stream Media Over RTP with FFmpeg
This guide provides a straightforward tutorial on how to stream audio and video media over the Real-time Transport Protocol (RTP) using FFmpeg. You will learn the essential command-line configurations, how to stream video and audio components, and how to generate the required Session Description Protocol (SDP) file so that media players can successfully receive and decode your stream.
Basic RTP Video Streaming
To stream a video file over RTP, you must instruct FFmpeg to read the input file in real-time and output it to a specific destination IP address and port using the RTP format.
Run the following command to stream only the video portion of a file:
ffmpeg -re -i input.mp4 -an -vcodec libx264 -f rtp rtp://127.0.0.1:5004-re: Forces FFmpeg to read the input file at the native frame rate (real-time). This is required for live streaming.-i input.mp4: Specifies your source media file.-an: Disables audio processing.-vcodec libx264: Encodes the video using the H.264 codec, which is highly compatible with RTP.-f rtp: Forces the output format to RTP.rtp://127.0.0.1:5004: The destination IP address (in this case, localhost) and the port number.
Streaming Audio and Video Together
RTP requires separate ports for audio and video streams. When you stream both, FFmpeg will output the configuration details needed by the receiver.
To stream both audio and video, use this command:
ffmpeg -re -i input.mp4 -vcodec libx264 -acodec aac -f rtp rtp://127.0.0.1:5004 > stream.sdpBy redirecting the output to stream.sdp, FFmpeg
automatically writes the session details to a file named
stream.sdp. The receiver needs this file to understand how
to bind the separate audio and video tracks.
How to Receive and Play the RTP Stream
Because RTP does not contain metadata describing the stream contents, the receiving player (such as VLC Media Player) cannot play the stream using the URL alone. It requires the SDP file generated by FFmpeg.
- Locate the
stream.sdpfile generated by your FFmpeg command. - Transfer this file to the receiving machine if it is different from the streaming source.
- Open the
stream.sdpfile with a compatible media player like VLC:
vlc stream.sdpThe player will read the parameters inside the SDP file, connect to the specified ports, and begin playing the live stream.