Transcode RTSP to H.264 and AAC with FFmpeg
This article provides a practical guide on how to transcode a live Real-Time Streaming Protocol (RTSP) stream into H.264 video and AAC audio on the fly using FFmpeg. You will learn the exact command-line syntax required for this process, along with a detailed breakdown of the parameters optimized for low-latency, real-time streaming.
To transcode an RTSP stream on the fly, you need to capture the input stream, decode it, re-encode the video to H.264 and the audio to AAC, and then send it to your desired destination (such as a local file, an RTMP server, or an HLS directory).
The Basic Command
Here is the standard FFmpeg command used to transcode an RTSP stream to H.264 and AAC in real-time:
ffmpeg -rtsp_transport tcp -i rtsp://username:password@camera_ip:554/stream_path -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -c:a aac -b:a 128k -f flv rtmp://live.poster_url/live/stream_keyParameter Breakdown
-rtsp_transport tcp: Forces FFmpeg to use TCP instead of UDP. This prevents packet loss, which can cause screen tearing and green artifacts in the live stream.-i rtsp://...: Specifies the input RTSP stream URL. Replace this with your actual camera or stream credentials and IP address.-c:v libx264: Sets the video codec to H.264 using thelibx264encoder.-preset ultrafast: Instructs the encoder to use the fastest possible compression speed. This is crucial for “on the fly” transcoding to prevent CPU bottlenecks.-tune zerolatency: Adjusts inner encoder settings to eliminate frame buffering delay, ensuring the lowest possible latency for real-time viewing.-pix_fmt yuv420p: Ensures high compatibility with modern players (like web browsers and mobile devices) by setting the pixel format to YUV 4:2:0.-c:a aac: Sets the audio codec to Advanced Audio Coding (AAC), which is widely supported across all platforms.-b:a 128k: Sets the audio bitrate to 128 kbps, providing a good balance between audio quality and bandwidth.-f flv rtmp://...: Specifies the output format (FLV is standard for RTMP) and the destination URL.
Alternative Output: Saving to a Local File
If you want to transcode the RTSP stream and save it directly to a local MP4 file instead of streaming it to a server, use the following command:
ffmpeg -rtsp_transport tcp -i rtsp://username:password@camera_ip:554/stream_path -c:v libx264 -preset medium -c:a aac -b:a 128k output.mp4Note: When saving to a file, the -preset medium
option is preferred over ultrafast because it produces
better compression and higher visual quality, and real-time network
latency is not a concern.