Capture RTSP IP Camera to MP4 with FFmpeg

This article provides a straightforward guide on how to capture a live video stream from an IP camera using the Real-Time Streaming Protocol (RTSP) and save it directly as an MP4 file using FFmpeg. You will learn the exact command-line syntax required to establish the connection, copy the stream efficiently without consuming excess CPU resources, and handle practical scenarios like segmenting files or setting capture durations.

The Basic Command

To capture an RTSP stream and save it as an MP4 file without re-encoding, use the following FFmpeg command:

ffmpeg -rtsp_transport tcp -i "rtsp://username:password@ip_address:554/stream_path" -c copy output.mp4

Parameter Breakdown

  • -rtsp_transport tcp: Forces FFmpeg to use TCP instead of UDP. TCP is highly recommended for RTSP streams because it prevents packet loss, which causes video corruption and gray artifacts in the saved file.
  • -i "rtsp://...": Specifies the input URL of your IP camera's RTSP stream. Be sure to wrap the URL in quotation marks to prevent command-line shell errors caused by special characters in passwords.
  • -c copy: Stream copies the video and audio codecs directly from the camera without re-encoding. This process requires virtually no CPU usage and preserves the exact original quality of the camera feed.
  • output.mp4: The name of the resulting video file.

Advanced Usage Examples

1. Set a Specific Capture Duration

If you want to record the stream for a specific amount of time (for example, 10 minutes), use the -t parameter followed by the duration in seconds or in HH:MM:SS format:

ffmpeg -rtsp_transport tcp -i "rtsp://username:password@ip_address:554/stream" -c copy -t 600 output.mp4

2. Split the Recording into Automatically Segmented Files

To record continuously and split the video into 1-hour chunks (3600 seconds) without losing frames between segments, use FFmpeg's segment muxer:

ffmpeg -rtsp_transport tcp -i "rtsp://username:password@ip_address:554/stream" -c copy -f segment -segment_time 3600 -segment_format mp4 -reset_timestamps 1 capture_%03d.mp4

This command outputs files named capture_001.mp4, capture_002.mp4, and so on.

3. Automatically Reconnect on Stream Drop

IP camera streams can be unstable. If the connection drops, you can instruct FFmpeg to attempt to reconnect by adding input flags:

ffmpeg -rtsp_transport tcp -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -i "rtsp://username:password@ip_address:554/stream" -c copy output.mp4