Capture RTSP Video Streams to Linux with FFmpeg
This article provides a practical guide on how to capture a live Real-Time Streaming Protocol (RTSP) camera feed and save it directly to your Linux storage using FFmpeg. You will learn the exact command-line syntax required for efficient recording, how to handle common connectivity challenges, and methods for automating your security footage split into manageable files.
The Core Command for Basic Capture
To save an RTSP stream without re-encoding the video—which saves CPU power and preserves original quality—you should copy the audio and video codecs directly. Run the following command in your Linux terminal:
ffmpeg -rtsp_transport tcp -i "rtsp://username:password@camera_ip_address:554/stream_path" -c copy output.mp4Breaking Down the Command Options
-rtsp_transport tcp: Forces FFmpeg to use TCP instead of UDP. TCP ensures that no data packets are dropped, preventing grey artifacts, pixelation, and video corruption in your saved file.-i "rtsp://...": Specifies the input URL of your IP camera. Always wrap the URL in quotes if it contains special characters like?or&.-c copy: Stream-copies the payload. This tells FFmpeg to act as a receiver and write the packets straight to disk without re-compressing them, keeping CPU usage near zero.
Advanced: Segmentation and Automatic File Splitting
Leaving a network stream recording indefinitely into a single file can lead to massive, unmanageable file sizes and potential corruption if the system crashes. You can instruct FFmpeg to automatically split the video into timed segments (e.g., every 15 minutes) using the segment muxer:
ffmpeg -rtsp_transport tcp -i "rtsp://username:password@camera_ip_address:554/stream_path" \
-c copy -map 0 -f segment -segment_time 900 -segment_format mp4 \
-reset_timestamps 1 "capture_%03d.mp4"Key Parameters for Segmented Recording
-f segment: Enables the segment muxer to split the output.-segment_time 900: Sets the duration of each segment in seconds (900 seconds equals 15 minutes).-segment_format mp4: Defines the container format for the split files.-reset_timestamps 1: Resets the timestamps at the beginning of each segment so the individual files play back correctly in standard media players."capture_%03d.mp4": Creates a sequential naming pattern, resulting in files namedcapture_001.mp4,capture_002.mp4, and so on.
Running the Capture in the Background
To ensure the recording continues running even after you close your
terminal session, wrap the FFmpeg command using nohup or
run it inside a screen or tmux session. For a
quick background task, use:
nohup ffmpeg -rtsp_transport tcp -i "rtsp://username:password@camera_ip_address:554/stream_path" -c copy output.mp4 > ffmpeg.log 2>&1 &This routes the operational logs to ffmpeg.log and frees
up your terminal immediately.