Use FFmpeg to convert RSTP stream to MP4

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 tensorrt_demos/trt_yolov4_mqtt.py at yolov4-mqtt Ā· ristomatti/tensorrt_demos Ā· GitHub. 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!

Yep, it's actually been "running" since about 2016, but if you restart the stream with a long gap (like when my DSL goes down) they restart it for you.

Exactly the point, as in "why not?"

Stop nagging! I said last year I'd do it next week! :stuck_out_tongue:

Once you create the stream in Youtube you have to provide a stable data feed for a short while (like a minute) before they offer to go live. You can also set it to automatic. Once it starts, unless you set auto-end, it will stay up forever but with the caveat that the URL may change if you break the feed for a while, hence the vanity URL (which I think you can still get) that follows the real live stream around.

My 'net is 75/20 - and I'm using about 25 of that upstream for the camera. For fun. But your ISP must be accomodating/ My last one tried to gouge me to upgrade to a business service when they got a new sales team in. I sid goodbye and moved to a better provider - Zen in the UK for those reading. Real unlimited.

If you set the i-frame too low then you are effectively sending the whole image each frame, and as MPEG compressiong (in all it's iterations) relies on incremental changes between frames being compressible this would kill your b/w.

In terms of latency there is far more inside youtube, and you can select from normal, low or ultra-low latency but you lose the DVR functionality I think on the last one. The real-world delay is about 15-30 seconds. You can look at the clock to check. Also the right click "stats for nerds" shows their view of latency in real time.

It's all fun, basically. And cats.

3 Likes

If you have the bandwidth, sure :slight_smile:

Oh, just checked and it's using about 8-10% of a single core on an RPI4 (no transcoding), so pretty lightweight apart from the upstream feed.

Edit: Note that vanity URL is still the OLD camera, 1080p etc. and very grainy at night. New one looks much better, but is quite wide angle - there is no zoom on the cheap model.

Not a problem, I live in Helsinki. :slight_smile:

image

1 Like

Thanks for sharing. I just read the hls ingest docs for youtube. They dont mention about being compatible with the fragmented mp4 options of hls. I wonder if it would work if you added -hls_segment_type fmp4 to your command (default is mpegts).

Years of trial and error with trying random ffmpeg parameters to see what they would do. I wish I could say I took a more scientific approach. :laughing:

5 Likes

I tried fm4 but something didn't work. Like many ffmpeg options it may not have been an issue but I settled on the above as it just works. I read the Google pages too and just went with as many defaults as I could for now

Let me ask one more. I am also testing Bart's flow where the ffmpeg is converting the stream and passing it into an image preview node. When I do that, the image preview shows "Invalid source" and getting this in the debug screen: "Error: Invalid property expression: zero-length".

The command is this:

ffmpeg -f rtsp -i rtsp://xxx:yyy@192.168.1.56:554/h264Preview_01_main -f image2pipe pipe:1

I tried adding the -rstp_transport tcp to here as well but did not make a difference. When I execute it in the console, I can see some "garbage" text coming back in the console, so it is doing something. Any ideas?

If you are piping in jpegs that are larger than your system's buffer(possibly 65k), then you have to catch all the jpeg pieces and put them back together. Try installing an unoffical node node-red-contrib-pipe2jpeg and put that between the exec node's output and the other node.

3 Likes