Pulling (unsupported) video streams into your NVR with RTSP

moped

Getting the hang of it
Joined
Mar 11, 2018
Messages
20
Reaction score
41
Before I purchased a hardware based NVR, I was using some enterprise NVR software and an older MJPEG camera as well as a 720p USB webcam. MJPEG = Motion JPEG. MJPEG was pretty common on older cameras / cloud cameras / consumer items. My Hik NVR only supports RTSP streams though - so these cameras aren't compatible (out of the box at least).

Transcoding (realtime conversion from one video format to another) was the only way I saw around this. I spent a good portion of the weekend researching potential options. What I found is that my use case is probably a little unique, generally speaking.

I was looking for software do do three basic things:
  1. Pull the MJPEG stream (or directshow device video)
  2. Transcode it to RTSP/h264
  3. Serve it to the network/NVR
I found several solutions that would convert RTSP to MJPEG (the opposite of what I wanted to do), but nothing outside of dedicated NVR software that would cover all three steps. I tried using NVR software to do this originally, but the CPU usage was higher than I would have liked and the output quality was extremely poor. There were lots of references online to Adobe Streaming Media Server or Wowza, but both of these carry licensing costs.

It was pretty clear that I was probably looking at one piece of software to transcode and a separate software solution to serve the streams to clients. FFMPEG is very popular for transcoding, supports all major formats, and is actually the backend used by several NVR software vendors (including some enterprise NVR sw). I was also able to find a basic RTSP media server (opensource perl script).

I went with:
  • FFMPEG - pull video streams (url or local), transcode to proper format, push to media server
    FFmpeg
  • RTSP-Server - receive data from FFMPEG and serve the content to clients on the network
    revmischa/rtsp-server

FFMPEG is relatively easy to get going - download the latest zip file, extract, and ffmpeg.exe can be found in the bin folder. If you are using Linux, you can do the same (or just install ffmpeg with whatever pacakge management app your OS uses).

RTSP-Server is linux only. I was not able to find anything suitable (or free) for the Windows platform. RTSP server is super easy to build and required zero configuration - literally install it and run the script. Script can be launched as a bg process with the usual & at the end of the command. I wrote my own init script so that it spawns the RTSP-Server (media server) at startup.

I used a general purpose linux machine to host the RTSP-Server (media server) and I used a Windows VM (w/usb camera attached) for FFMPEG (transcoding). RTSP-Server listens for media (from the transcoder) on port 5545 and listens for clients (NVR, etc) on the standard RTSP port (554).

Commandline for transcoding MJPEG (url) to RTSP:
Code:
ffmpeg -i http://admin:admin@camera_ip_address/goform/video -pix_fmt yuv420p -vcodec libx264 -maxrate 1M -bufsize 2M -tune zerolatency -preset faster -r 5 -f rtsp rtsp:/rtsp_server_address:5545/garage
Commandline for transcoding directshow (USB device/webcam) to RTSP:
Code:
ffmpeg -f dshow -framerate 5 -video_size 1280x720 -i video="B525 HD Webcam" -pix_fmt yuv420p -vcodec libx264 -maxrate 1M -bufsize 3M -tune zerolatency -preset faster -f rtsp rtsp:/rtsp_server_address:5545/livingroom
Thinking a little more broadly, it seems that you could likely use similar methods to have FFMPEG transcode video from a video capture device, TV tuner, etc. It's probably worth noting that VLC can also be used to transcode to rtsp. I am not transcoding audio from the webcam - but it could be done with a few additional commandline options.

The USB camera transcoding process uses ~10% of CPU and transcoding MJPEG uses around 5%. Combined total is less than half the CPU usage I saw when using NVR software to do the work. A fair amount of tweaking was required with FFMPEG commands to optimize the stream for the NVR. Initially I was seeing huge delays (1-2m) when the cameras were pulled up on the live view and they wouldn't stream through hik-connect at all.

I also ran into an issue with the MJPEG camera. Apparently it doesn't completely adhere to MJPEG standards. Probably not an issue with all cameras, but the result was that FFMPEG would randomly die due to "invalid stream data". I ended up creating a batch file loop to respawn the transcoder automatically.

Example of batch file loop:
Code:
@echo off
:loop
ffmpeg -i http://admin:admin@camera_ip_address/goform/video -pix_fmt yuv420p -vcodec libx264 -maxrate 1M -bufsize 2M -tune zerolatency -preset faster -r 5 -f rtsp rtsp:/rtsp_server_address:5545/garage
goto loop
pause
On the NVR side (hikvision), I setup a custom protocol for each camera - pointing to proper url/ports/etc. The RTSP-Server is UDP only, so I specified that when setting up the protocol. Outside of this, one camera had the url "/livingroom" and the other had the url "/garage". As an example, in VLC the full URL would be rtsp:/rtsp_server_address/livingroom. Once the protocols were added I added each camera, set the proper protocol, and entered the address of my RTSP server.

Everything has been working smoothly for the last 24-48 hours. I'm doing continuous recording on these two channels since it's just an RTSP stream (no ONVIF event support, for motion etc). If I have some free time (before I end up replacing these two cams), I'll probably start looking into ONVIF emulation options.

This project looks pretty interesting, maybe a good starting point:
How to Turn an USB Camera With Raspberry Pi Into an Onvif IP Camera?
 

bp2008

Staff member
Joined
Mar 10, 2014
Messages
12,666
Reaction score
14,006
Location
USA
I'm not sure there is a real mjpeg standard.

Thing is, real IP cameras are pretty cheap these days. $50 will get you a low end 4MP cam from Dahua and that will knock the socks off any USB camera I've ever seen.

Like you, I went through similar adventures years ago and eventually settled on just doing it the easy way :)
 

0blar

Getting the hang of it
Joined
May 17, 2017
Messages
281
Reaction score
34
Did you try to transcode your live stream in rtmp format ? then send it to an nginx rtmp server, you will be able to play the output stream in vlc
 

moped

Getting the hang of it
Joined
Mar 11, 2018
Messages
20
Reaction score
41
Did you try to transcode your live stream in rtmp format ? then send it to an nginx rtmp server, you will be able to play the output stream in vlc
I didn't want to have to setup nginix (really wanted to avoid having to play with or build up config files), plus I'm not sure that my NVR supports RTMP. Path of least resistance I guess - zero configuration on the server (just run a script) + figuring out proper ffmpeg switches.

I will go the easy route eventually with additional cams, but it's nice to have something in place temporarily. This gives me something to play with later - pulling weather/tv into the NVR live view with a USB TV Tuner for example. I think the desktop streaming that ffmpeg can do is possibly more interesting - I could pull up my favorite (auto refreshing) weather maps (or other web content), plus apps like Unitrunker (trunking radio scanner app) and then feed the audio output into the NVR. Using example above I could see the weather (from a provider of my choice without commercials), see what is going on with unitrunker/scanner, and hear the radio chatter from local emergency services.
 

Lucas Thom

n3wb
Joined
Mar 27, 2019
Messages
4
Reaction score
1
Location
Brazil
I'm trying to do the same but I cannot setup the stream on the NVR.

It seems a network issue cause it has the info Network unreachable/Network abnormal on the camera setup view.
The rtsp server is running on my notebook with a fixed ip 192.168.254.x, I can ping the NVR and access http://192.168.254.1

The NVR is the DS-7104NI-E1 / 4P

Can you help me?
 

moped

Getting the hang of it
Joined
Mar 11, 2018
Messages
20
Reaction score
41
I'm trying to do the same but I cannot setup the stream on the NVR.

It seems a network issue cause it has the info Network unreachable/Network abnormal on the camera setup view.
The rtsp server is running on my notebook with a fixed ip 192.168.254.x, I can ping the NVR and access http://192.168.254.1

The NVR is the DS-7104NI-E1 / 4P

Can you help me?
I would suggest testing locally using VLC. If it works, then it's probably a firewall issue (windows firewall or whatever AV you are using). If it doesn't work, you'll have to look at rtsp-server or ffmpeg as potential culprits.
 

Lucas Thom

n3wb
Joined
Mar 27, 2019
Messages
4
Reaction score
1
Location
Brazil
I've tested on two computers, both with ubuntu.

I'm running the server at (192.168.254.3 poe number 2)

Code:
sudo ./rtsp-server.pl -s 5554 -c 554 --loglevel 5
and feeding the stream with:

Code:
ffmpeg -re -y -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 640x480 -i - -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -b:v 256k -bufsize 56k -g 30 -f rtsp rtsp://127.0.0.1:5554/stream1
(i'm using stdin)

I've another notebook connected to poe 1 on NVR (192.168.254.2).
From this notebook I can view the stream on VLC. In this case I'm using the NVR as a switch to test if its NICs are working and they are. But I still get the Abnormal Network message
 
Last edited:

moped

Getting the hang of it
Joined
Mar 11, 2018
Messages
20
Reaction score
41
I've tested on two computers, both with ubuntu.

I'm running the server at (192.168.254.3 poe number 2)

sudo ./rtsp-server.pl -s 5554 -c 554 --loglevel 5

and feeding the stream with:

ffmpeg -re -y -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 640x480 -i - -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -b:v 256k -bufsize 56k -g 30 -f rtsp rtsp://127.0.0.1:5554/stream1
(i'm using stdin)

I've another notebook connected to poe 1 on NVR (192.168.254.2).
From this notebook I can view the stream on VLC. In this case I'm using the NVR as a switch to test if its NICs are working and they are. But I still get the Abnormal Network message
Can you post the config you are using for the custom protocol on the DVR side?
 

moped

Getting the hang of it
Joined
Mar 11, 2018
Messages
20
Reaction score
41
@moped Thank you for the reply.
I think the NVR is broken somehow, maybe a firmware problem idk.
Btw, what's the model of your NVR?
I'm using a DS-7608NI-E2/8P. The protocol config looks correct. The only notable difference is that I used an absolute path when configuring ("/stream1" instead of just "stream1").

You can test RTSP functionality locally on the device. If you have a working channel, add a custom protocol (RTSP) with the proper stream path (you'll need to look that info up) and set the server as 127.0.0.1 in the camera config. Or you can enable channel zero and pull an RTSP stream of that.

I pull channel zero of a remote NVR (~100mi away) via RTSP 24x7 (for monitoring + secondary/offsite video storage).
Protocol: RTSP, Automatic Transfer Prot, Port 10554, Stream path /Streaming/Channels/001
Camera config: Adding method manual, Custom protocol selected, admin/admin/admin for user/pwd
 

Lucas Thom

n3wb
Joined
Mar 27, 2019
Messages
4
Reaction score
1
Location
Brazil
I've implementend an ONVIF server to publish the RTSP address and It's working now. Thank you
 

zyklop

n3wb
Joined
Aug 8, 2019
Messages
1
Reaction score
0
Location
NRW
I've implementend an ONVIF server to publish the RTSP address and It's working now. Thank you
How did you make it working ??
The howto works only for rtsp stream in browser, but not with my nvr NBD7808T-PL (China) it should not recognize onvif login parse.
pls complain what kind of onvif server you used and what is the complete ffmpeg command ??

greetings from the wild west germany
 

xtian

n3wb
Joined
Mar 8, 2019
Messages
13
Reaction score
2
Location
Reading, PA
Kinda related. I think there should be a way to add the FaceTime camera on an iMac to stream to a dahua NVR, so I can 1) see the camera on the NVR screen along with all my other IP cameras, and 2) see the FaceTime camera through IDMMS lite.

Using FFmpeg, I am trying to stream from the FaceTime camera to the DVR ip address:
ffmpeg -f avfoundation -framerate 25 -video_size 960x540 -i "0:none" -vcodec libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv422p -f mpegts udp://dahuaNVRIPAddress:12345

I can't figure out a way to then add this stream via the NVR interface? What am I missing?
Any suggestions?
 
Top