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_key

Parameter Breakdown

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.mp4

Note: 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.