Use FFmpeg to convert RSTP stream to MP4

I just realized you might need to force tcp connection.
In your command, try adding -rtsp_transport tcp before the -i

1 Like

I also have the misfortune of owning two Reolink RLC-420 5MP cameras and they (among a few other models) have issues with their RTSP stream and mostly only work on the Reolink app. It likely does not help in this situation but I just recently learned the cameras also support RTMP streams and can do that without the issues you described.

On my Android phone and tablet for example I use tinyCam PRO which has handled every other camera I got (5 of different brand and model) but the Reolink cameras I have to view in snapshot mode as the app does not support RTMP and the RTSP stream smears after few seconds of streaming.

Kevin, thank a lot. This transport option has fixed the issue. The mp4 file is now clear and I tested it on my RLC-522, RLC-410 and E1 Zoom and all work fine. How did you figure this out?

For those looking at this discussion in the future, this is the full command that works for my Reolink cameras. The below command will create a 1 minute long (-t 60) video of the live stream:

ffmpeg -rtsp_transport tcp -i rtsp://username:password@192.168.1.56:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y /home/pi/video.mp4

4 Likes

Check my post above, it is working now. Btw the feed is clear for example in VLC. So I was confident that there will be a solution eventually.

1 Like

It might not be a coincidence then that the only software outside of Reolink's own app where I've got the RTSP stream working has been Shinobi (NVR software) in which Kevin has been involved. It did support RTMP also but for some reason RTSP worked better on the Raspberry docker image I've been testing it with.

This might actually solve the final piece in my quest to setup a light weight Node-RED based NVR solution using object detection (YOLOV4 inference running on a Jetson Nano). Awesome!

@kevinGodell we've been so lucky to have you appear on the forum out of the blue! There's been so many threads with people being more or less clueless with ffmpeg :smile:.

3 Likes

(Being a specialist in off topic :laughing:) here are the links of the PTZ (pan tilt zoom) camera I have which are of the same type (Chinese manufacturer xiongmai technology) as @kevinGodell

My actual ptz cam : https://www.aliexpress.com/item/4000096231458.html?spm=a2g0s.9042311.0.0.27424c4d865FtM
My wish of purchase : https://www.aliexpress.com/item/4000260050022.html?spm=a2g0o.cart.0.0.4c503c00brYTBF&mp=1 you can choose with fix focus or 4x optical zoom and auto tracking :star_struck: (see video).

All use icsee app (my favorite) include human detection. My actual cam is fully controllable with Node red.

4 Likes

So simple setup of the first camera, really easy, thats very clever!

The second camera is so amazingly impressive!!! How can they pack all those features into that little device, unbelievable. I really like the auto-tracking feature, the picture quality, the very fast ptz, object detection, two way audio, wow, I now got appetite to upgrade everything I have, what a solution with YOLO object analyze and detection running in my Jetson Nano that could be!!!

1 Like

I agree this is very cool. This is probably going to be the next norm, object detection. A custom model training would be great, so my camera would be able to tell if it is the UPS, DPD, or other delivery trucks are at my front door. Or the postman who comes in motorbike :slight_smile:

Nice to see the issue is resolved, but in case it's of use to anyone searching, here is my bodged script (worked for years, so never cleaned it up), with stream key removed, of how I both save a feed locally and stream to YouTube live - this is with old Geovision BL2410 cam, but just got a new Annke (Hikvision's cheap brand) 4k camera which will be replacing it - probably through Shinobi with HLS to YT instead.

Worth noting the Geovision provides no audio stream, which was my early problems and this injects a fake aac stream. May be required on some cams.

The exiftool part moved the files into a dated sub-directory every time the stream restarts (or is killed by a daily crontab job) - again primitive, but works.

#!/bin/sh

RTSPNAME=camera1
RTSPIP=192.168.111.111
RTSPPORT=8554

CONVTOOL=ffmpeg
DIR=$HOME/cctv
URL=rtmp://a.rtmp.youtube.com/live2
STREAM=XXXX
#LOGLEVEL="-loglevel fatal -hide_banner"
LOGLEVEL="-hide_banner"

SENDTOYOUTUBE="-strict experimental -bufsize 512k -ab 32k -codec:v copy -codec:a aac -f flv $URL/$STREAM -flvflags no_duration_filesize"
SAVETOFILES="-c copy -map 0:v -f segment -segment_time 300 $RTSPNAME-%03d.mp4"

mkdir $DIR/$RTSPNAME >/dev/null 2>&1
cd $DIR/$RTSPNAME
while true
do
        exiftool -m -d "%Y-%m-%d/$RTSPNAME-%Y-%m-%d-%H:%M:%S.%%e" '-filename<filemodifydate' $RTSPNAME-*.mp4
        ${CONVTOOL} ${LOGLEVEL} -rtsp_transport tcp -i rtsp://$RTSPIP:$RTSPPORT/CH001.sdp $SENDTOYOUTUBE $SAVETOFILES 2>&1 | ts '%F %T' >> /tmp/${CONVTOOL}-$RTSPNAME.log
        sleep 5
done
2 Likes

Yeah, haven't dived into that myself but with my NVIDIA Jetson Nano and related tools I read it is possible to use that device and train your own model with provided tools. A number of images of each type in various angels, UPS, DHL etc etc and of yourself smiling at the front door position might be enough to create an acceptable working model :innocent:

1 Like

Can you make me a little tutorial on how to setup streaming to youtube mainly focusing on the youtube side of how to setup the credentials? Or maybe point me to a good tutorial that worked for you? I always wanted to try that, but never got around to tinkering.

2 Likes

Happy to, but I set it up years ago and their dashboards have changed lots since then.

The basics are to go to studio.youtube.com (with your google account) and "Go Live" - if your account is in order (not brand new etc.) then they let you create a stream and then from their you will see a "Stream Settings" tab including a hidden Stream Key you can show/copy, which goes into the STREAM env var above.

I would screen cap but the URL includes tokens and as I am currently working I haven't the time just now to edit any screen caps.

Most of my original issues - I tried other live streaming at the time - was the lightbulb moment when I realised my camera stream did not include audio and this silently (no pun intended) caused initial streams to fail.

I've had the same experience, the ones I'd recommend are all no longer available.

Particularly disappointing is one that worked well came with totally different firmware and was not very good when I id "buy it again" on Amazon a few weeks later.

Actually I'm all for bashing brands, but the fact is very few of these guys actually make anything.

I have a 5 Mpixel 4X optical motorized zoom Reolink camera I got from Amazon for ~$85. It worked very well, although its relatively gigiantic as far as these things go. My only issue is it sometimes will lose color, but it eventually recovers.

I've had really mixed results with Besder "brand" cameras. Two from aliexpress and one from Amazon.

The one from Amazon was the best and I'd highly recommend it if it were still available: BEDSERSEC HX-60R36 1080P The rtsp stream is good and Onvif snapshots give the full 1080p image with minimal latency. It'd be perfect if they lost the WiFi and used POE.

I don't see the point of WiFI cameras other than initial convenience when playing around since you still have to run a wire for power. YMMV. Also streaming multiple HD video cameras over your 2.4GHz WiFi is not likely to make the rest of your household happy. Good luck with 5GHz WiFi (if you can find it with "inexpensive" cameras), in our house, two walls between the phone and the 5GHz router means the phone drops back to 2.4GHz

The aliexpress Besder 1080p WiFi was total garbage and basically was usable only with their phone app.

The other a fixed 8mm lens 1080p works very well but the rtsp stream dies every 6-12 hours, which let me develop OpenCV rtsp stream reading tread code that reconnects automatically and has been very reliable with the auto re-connection code. Downside, no POE and only give lame D1 (704x480) Onvif snapshots. RTSP is great if you actually want video recording, but it has a lot of extra overhead if all you want are still images to feed to an AI

My code is here:
https://github.com/wb666greene/AI-Person-Detector

Its multi-threaded Python with node-red as the controller and handling all notifications. The Python you can just use, the node-red you can customize for your needs, mine on the github is just a simple starting point to show the Python interaction.

I've run variations of this system 24/7 for approaching three years now.

One very counter-intuitive observation I've made was that since the MobilenetSSD-V2 that I use needs the imaged resized to 300x300 pixels, you might think D1 resolution would be more than good enough, but I clearly got better detection with 1080P camera, and was shocked by the improvement when I moved to 4K cameras. A 4K image in an MMS text notification makes the "friend or foe" decision quick and simple with a glance.

I strongly recommend investigating if you can get YOLOv4 (or at least YOLOv3) working on your setup instead of Mobilenet SSD. It's a lot more accurate. Even the "tiny" variants.

YOLOv2 was no better and much slower.

I've had to put my AI stuff aside for a while, but trying YOLOv4 on the Nano is on my todo list. My idea is to send the detection images from MobilenetSSD-V2 to the Nano for a final verification with YOLO or another model.

My first pass at a second different "verification" AI was with Posenet, it worked great on all the false detection images I'd saved, but turns out it needs a near "straight on" view, it rejects nearly everything if the camera angle is looking "downward" as most security cameras end up being mounted to avoid easy vandalism.

1 Like

OK, after some playing today, this is how to send 4k HEVC (H.265+) direct from a cheap IP cam (based on the HikVision G1 platform) to Youtube using their HLS ingestion:

ffmpeg -rtsp_transport tcp -i rtsp://USERNAME:PASSWORD@CAMERA:554/CH001.sdp -f lavfi -i anullsrc -c:v copy -c:a aac -map 0:v -map 1 -f hls -hls_time 4 -hls_playlist_type event -method PUT 'https://a.upload.youtube.com/http_upload_hls?cid=STREAM_KEY&copy=0&file=stream.m3u8'

Apparently the RTMP ingestion doesn't allow H.265 so you have to use DASH or HLS and DASH was a non-starter for me. On quick inspection this 4k/12fps stream used less b/w than my 1080p/20fps H.264 stream.

The command line injects silence into the stream, which as I said above, is mandatory and the cause of much head scratching until you find it.

This will replace the existing stream here in the next few days: https://www.youtube.com/c/PeterGalbavy/live :slight_smile:

Edit: I moved away from H.265+ to plain H.265 since writing this as one of the optimisations appears to be setting the GOP (I frame interval) to 25 seconds instead of the desired 2. Now more bandwidth, but better overall.

3 Likes

Interesting idea, but I'm unsure of the use case. Its says its been running since Feb 27, does Google actually have infinite bandwidth?

I'll definitely play with it just because its cool tech.

Looks like you need to mow the yard :slight_smile:

One question, how do you start the streaming? Do you log into your youTube "channel" and then launch the command to open the stream and upload? Or is that cid=STEAME_KEY mean that STREAM_KEY is replaced with your Google supplied "magic number"? If so, what do you do to get the key?

Many home Internet connections are asymmetric, often with the upload bandwidth 1/10 of the download bandwidth, how does this impact your nornal web usage?

Your edit, brings up a new question, If I can, on my cameras I set the Iframe interval to 1 in an attempt to minimize latency in the stream, which seems to be 2-4 seconds when I watch the stream in VLC and move in front of the camera while testing. Do I have this backwards?

This does not much sense as at least I had issues with SSD not detecting people rather than false detections.

Together with @krambriw we upgraded his Jetson Nano and MQTT based detection script to work with YOLOv4 and I added some bells and whistles for it to support multiple cameras and to it to allow both annotated image and a list of detected classes with confidence scores to be returned to Node-RED. If you want to take a look at the script it's here https://github.com/ristomatti/tensorrt_demos/blob/yolov4-mqtt/trt_yolov4_mqtt.py. It can do yolov4-288detection at around 5 FPS.

The script is in a forked example repository which adds performance tweaks to YOLOv4 detection to work faster on a Jetson Nano. The repository main page has information on how to install/build the required tools. If there's interest, I can post a share my project accompanied with the Node-RED flow that goes with it.

Sad to hear about the issues you've had with Posenet. I was planning on testing it but it's no use for me if it doesn't detect from downwards pointing cameras...

P.S. Sorry for the off topic tangent!

Using YouTube as a streaming proxy is an awesome idea!