This page describes how to record RTSP streams from an IP camera to an MP4 file. This includes scripts to record segments every N minutes from an IP Camera Real Time Streaming Protocol (RTSP) streams.


This solution relies upon FFMPEG.  Search "download ffmpeg" to download the command line.  Its sometimes a little tricky to find the binary package but look for "binary" or "build".


FFMPEG Command

Following is the FFMPEG command to record an RTSP stream from camera do disk:

ffmpeg -i "rtsp://user:[email protected]/axis-media/media.amp?camera=1" -c copy -reset_timestamps 1 -metadata title=\\"Cafe East\\" -map 0 -f segment -segment_time 15 -segment_format mp4 "outputfolder/recording-%03d.mp4"
  • where:
    • -i is input URL with user/pwd and path
    • -c indicates stream should saved to disk as-is (not re-encoded)
    • -reset_timestamps is needed so that timestamps are from 0
    • -metadata adds title (this is shown as overlay in VLC media player)
    • -map 0 indicates to pull 1st stream (if for some reason this is not the video stream, then try 1 or 2 or look at output of ffmpeg to see what # the video stream is
    • -f indicates chunks of the file should be split
    • -segment_time is how big a chunk.  Its in minutes.  I think you can use seconds as 5:30 or 0:30 if you needed
    • the output file is specified with a %03d which is where an incrementer (01, 02, etc) is added.  The "outputfolder" can be any path - absolute or relative.


Bash Script

To record RTSP streams on Linux or macOS machines, run the attached record_segments.sh script as shown below:

Usage:
record_segments.sh -i "RTSP_URL" "TITLE" SEGMENT_DURATION FOLDER FILENAME
Example:
record_segments.sh -i "rtsp://user:[email protected]/axis-media/media.amp" "My title" 300 recordings video


  • RTSPURL should be fully formatted URL for playback of the live stream.
  • TITLE is added as metadata inside the file. (Media players will show this.)
  • FOLDER and FILENAME are where the recordings are stored. FILENAME will be appended with 001.mp4, 002.mp4, etc.


Windows Batch Script

To record RTSP streams on Windows machines, you must first set up ffmpeg on your machine:


  1. Install FFMPEG and unzip it to C:\Program Files\ffmpeg\bin. 
  2. Create a PATH entry.
    1. Open the Windows menu and type "Path".
    2. Click "Edit System Environment path".
    3. Click "Edit variables".
    4. Add a new entry to "Path" under "User variables". (e.g. "C:\Program Files\ffmpeg\bin")


You can now record RTSP streams by running the attached record_segments.bat script as shown below:


Usage:
record_segments.bat -i "RTSP_URL" "TITLE" SEGMENT_DURATION FOLDER FILENAME
Example:
record_segments.bat -i "rtsp://user:[email protected]/axis-media/media.amp" "My title" 300 recordings video


  • RTSPURL should be fully formatted URL for playback of the live stream.
  • TITLE is added as metadata inside the file. (Media players will show this.)
  • FOLDER and FILENAME are where the recordings are stored. FILENAME will be appended with 001.mp4, 002.mp4, etc.
  • Make sure to use double quotes. (Single quotes don't work.)