FFmpeg is a powerful multimedia framework that can be used to record, convert, and stream audio and video. It supports many different protocols, including RTSP (Real-Time Streaming Protocol), which is commonly used for streaming video from IP cameras or other sources.

How to Use FFmpeg with RTSP

FFmpeg can be used to stream, record, or transcode RTSP streams. Below are some common use cases and examples:

1. Viewing an RTSP Stream

If you want to simply view an RTSP stream using FFmpeg, you can do so using the

ffmpeg -i rtsp://username:password@IP_address:port/path -f sdl “RTSP Stream”

This command:

  • -i: Specifies the input source, in this case, an RTSP stream.
  • The URL (rtsp://username:password@IP_address:port/path) should be replaced with your camera’s RTSP URL.
  • -f sdl: Opens a window to display the stream using the SDL framework.

2. Recording RTSP Stream to a File

To record a live RTSP stream to a file, use the following command:

ffmpeg -i rtsp://username:password@IP_address:port/path -c copy output.mp4

  • -i rtsp://…: This is the RTSP URL of your camera or stream.
  • -c copy: This tells FFmpeg to copy the stream as-is without re-encoding (saves processing power).
  • output.mp4: The name of the file you want to save the stream to. You can change the format to .mkv, .flv, or others.

3. Transcoding RTSP Stream to Another Format

If you want to transcode (change the format or codec) of the RTSP stream, you can use FFmpeg’s built-in codecs. For example, to transcode an RTSP stream to H.264 and save it as an MP4 file:

ffmpeg -i rtsp://username:password@IP_address:port/path -c:v libx264 -preset fast -crf 23 -c:a aac -b:a 128k output.mp4

  • -c:v libx264: Specifies the video codec to use (H.264).
  • -preset fast: Sets the encoding speed/quality ratio.
  • -crf 23: Sets the quality of the video (lower is better, but larger file size).
  • -c:a aac: Specifies the audio codec to use (AAC).
  • -b:a 128k: Sets the audio bitrate.

4. Streaming RTSP to Another Destination

To re-stream an RTSP stream to another server or RTMP (commonly used by streaming platforms like YouTube or Twitch), you can use:

ffmpeg -i rtsp://username:password@IP_address:port/path -c:v copy -f flv rtmp://destination_URL

This command:

  • -c:v copy: Copies the video codec without re-encoding.
  • -f flv: Outputs the stream in the FLV format (which is compatible with RTMP).
  • rtmp://destination_URL: The destination URL of the RTMP server.

5. Capturing Snapshots from an RTSP Stream

If you want to grab still images from an RTSP stream every few seconds, you can use FFmpeg for that as well:

ffmpeg -i rtsp://username:password@IP_address:port/path -vf fps=1 image_%04d.jpg

  • -vf fps=1: Captures one frame per second (adjust this value to change the frequency of the snapshots).
  • image_%04d.jpg: Saves the images as image_0001.jpg, image_0002.jpg, etc.

6. Streaming RTSP to HLS (HTTP Live Streaming)

If you want to convert an RTSP stream into HLS format for browser-based viewing, you can use:

ffmpeg -i rtsp://username:password@IP_address:port/path -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -f hls stream.m3u8

  • -start_number 0: Starts the segment numbering at 0.
  • -hls_time 10: Splits the stream into 10-second segments.
  • -hls_list_size 0: Keeps all segments in the playlist (set to a higher number for limited-length streams).
  • stream.m3u8: The output file that will reference the segments for the HLS stream.

Common Issues with FFmpeg and RTSP

  • Network Latency: RTSP streams might have inherent delays depending on network quality. You can reduce the latency by adjusting buffer sizes:
    • ffmpeg -rtsp_transport tcp -i rtsp://username:password@IP_address:port/path -fflags nobuffer -flags low_delay -framedrop -strict experimental output.mp4
      • -rtsp_transport tcp: Forces FFmpeg to use TCP instead of UDP, which can be more stable but slightly slower.
      • -fflags nobuffer: Reduces buffering delays.
  • Unsupported Codecs: Some cameras stream in proprietary or unusual codecs. In such cases, you might need to transcode the video or find additional FFmpeg libraries to support the codec.