Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mbrammer/a233c836105eeaa1c95f824d2f6fc601 to your computer and use it in GitHub Desktop.
Save mbrammer/a233c836105eeaa1c95f824d2f6fc601 to your computer and use it in GitHub Desktop.
Crop section from RTSP live stream and provide result as new RTSP live stream

Create croped RTSP live stream from existing RTSP live stream

I needed a croped section of a RTSP live stream from a UniFi Protect camera, since I am filming my yard from a shed and wanted to see a cutout of the front door from my 4K camera.
This article will show you how to enable the RTSP live stream on the UniFi Protect camera, how to setup a streaming server and how to actually execute the processing of the live stream.

Enable UniFi Protect RTSP live stream

The following screenshots are made from UniFi Protect application on version 2.7.18

  1. Go to "UniFi devices"
  2. Click on the desired camera in the camera list
  3. Go to "Settings"
  4. Enable a stream of a resolution you need.
  5. For our scenario we need the non-secure URL.
    Just replace rtsps with rtsp, 7441 with 7447 and remove ?enableSrtp
    rtsps://172.2.4.1:7441/my-stream?enableSrtp (secure)
    rtsp://172.2.4.1:7447/my-stream (non-secure)

Setup streaming server

I did use MediaMTX as a simple RTSP server within a Docker container.
You will find the installation notes here: https://github.com/aler9/rtsp-simple-server#installation
Once it's running, you bascally do not have to touch it anymore.

Execute live stream processing

Here is the command I execute every time my NAS boots:

ffmpeg -rtsp_transport tcp -i rtsp://172.2.4.1:7447/my-stream -nostdin -loglevel panic -filter:v "crop=362:204:437:192,fps=20" -vcodec libx264 -preset:v ultrafast -tune zerolatency -crf 23 -an -f rtsp rtsp://172.2.4.2:8554/my-new-stream 2> /dev/null &

What this does:

-rtsp_transport tcp -i rtsp://172.2.4.1:7447/my-stream
The source (input). You can also use udp as transport, but I (and also others on the web) experienced best (stable) results with tcp.
If you need to provide credentials, you can do it like this: rtsp://username:password@172.2.4.1:7447/my-stream

-nostdin -loglevel panic
This will prevent stdin, which we need to run the process permanently in background. Log will also provide as less as possible output.

-filter:v "crop=362:204:437:192,fps=20"
This will crop a part from the original RTSP live stream 362(width):204(height):437(left):192(top).
FPS is set to 20. This was enought for my needs. Adjust this to your needs.

-vcodec libx264
Providing a video codec. libx264 worked best for me and was also mostly recommended on the web.

-preset:v ultrafast -tune zerolatency
ultrafast video preset will do less rendering and may results in higher bandwith, but at lower CPU usage.
zerolatency is always good for live streams 😅

-crf 23
This will define the image quality. 23 was a good value for me. You can choose between 0 (best) and 51 (worst).

-an
Will disable audio. If you need audio, you could copy it from the original stream with -c:a copy instead.

-f rtsp rtsp://172.2.4.2:8554/my-new-stream
Defining the filetype rtsp and the destination where we will send our new live stream to.

2> /dev/null &
Will dump the output and put the process in background.

Issues I came around

ffmpeg build insufficient

On my Synology NAS, ffmpeg was already on board, but it seemd like it was just some kind of a light build.
This resulted in a Unrecognized option 'rtsp_transport' on my end.
I had to install ffmpeg from the package center. Think this was coming from http://packages.synocommunity.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment