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.
The following screenshots are made from UniFi Protect application on version 2.7.18
- Go to "UniFi devices"
- Click on the desired camera in the camera list
- Go to "Settings"
- Enable a stream of a resolution you need.
- For our scenario we need the non-secure URL.
Just replacertsps
withrtsp
,7441
with7447
and remove?enableSrtp
rtsps://172.2.4.1:7441/my-stream?enableSrtp
(secure)
rtsp://172.2.4.1:7447/my-stream
(non-secure)
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.
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 &
-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.
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