Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reolink standalone cams work #127

Merged
merged 16 commits into from
Jan 5, 2022

Conversation

GhostlyCrowd
Copy link
Contributor

@GhostlyCrowd GhostlyCrowd commented Dec 23, 2021

Work in progress getting Standalone Reolink cams to work more in line with how t he ReolinkNVR parts work.

What's working:

  • Motion detection
  • Spawning a stream on the main or substream and proxying it to Unifi Protect. using -s main, or -s sub argument note, is maximum 15fps so use tick_rate-30000/1001
  • Using the proper API url for snapshot grabbing

What's not working:

  • Spawning Video streams according to their main/sub ie: spawning the main stream for live view and recording and the sub stream for preview to save resources.
  • Any version of ffmpeg over v4.1.8 as it seems to not pars the h264_metatdata flags properly and crashes the streams causing a respawn loop.

Examples on how to use:

  • Standalone cameras using proper Snapshot URL and Motion-Det API url using Camera Substream (main or sub) work in progress

Main camera stream example

unifi-cam-proxy -H <NVR IP> -i <camera IP> -c client.pem -t <Adoption token> reolink -u <username> -p <password> -s main --ffmpeg-args='-c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k'

Sub camera stream example

unifi-cam-proxy -H <NVR IP> -i <camera IP> -c client.pem -t <Adoption token> reolink -u <username> -p <password> -s sub --ffmpeg-args='-c:v copy -bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k''
  • (Note: Camera/channel arguments are either -s main, or -s sub. The substream is limited to a maximum 15fps so note the tick_rate in the example)

  • Possibly a limitation of my VM I'm testing I cannot get smooth Live view on mobile/web if the main streams bitrate is over
    1024

The Reolink part of this project seems to not work well any more many of us seem to fail to get it to work. I'm not a pro but this is my small contribution, and I'm learning along the way.

@GhostlyCrowd
Copy link
Contributor Author

Ive actually got live view, in mobile app live view and recording all working now. THe issue is i had to edit the code to force in ffmpeg args because of the error above, so i had to make the ffmpeg args

default='-c:v copy -bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k',

@keshavdv can you please advice how to fix the error im getting trying to pass arguments or if there is a way i can include this as a default in my reolink.py

I think alot of people are going to be happy.

-bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1"

Is the new magic for new versions of ffmpeg as -vbsf is depreciated.

@keshavdv
Copy link
Owner

Thanks for figuring out the new bitfilter format! I'd love to figure out exactly which version of ffmpeg supports this style of flags and add some logic to use the correct set of arguments since that will also allow me to fix #55. Do you know when this new format was introduced?

@GhostlyCrowd
Copy link
Contributor Author

GhostlyCrowd commented Dec 27, 2021

Thanks for figuring out the new bitfilter format! I'd love to figure out exactly which version of ffmpeg supports this style of flags and add some logic to use the correct set of arguments since that will also allow me to fix #55. Do you know when this new format was introduced?

somewhere between V3.4.X and 4.1.X I have not looked that deep into it yet. I am kind of learning as i go here.

Do you have any incite as to why i cannot pass --ffmpeg-args as explained above? ive been having to manually edit the default in base.py each time i want to try new arguments with my PR.

Ive also found that fixed_frame_rate_flag=1 is no longer supported in the latest ffmpeg 4.3.3 it throws an error. so there seems to be maybe even more changes we need to investigate to get this project working with the most recent ffmpeg.

@keshavdv
Copy link
Owner

keshavdv commented Dec 27, 2021

This looks like https://stackoverflow.com/questions/16174992/cant-get-argparse-to-read-quoted-string-with-dashes-in-it so I'd suggest trying

--ffmpeg-args='-bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1"'

Can you share ffmpeg output both with and without fixed_frame_rate_flag and corresponding error? The option itself is still supported and isn't deprecated, but the implementation might have changed in recent ffmpeg releases.

@GhostlyCrowd
Copy link
Contributor Author

This looks like https://stackoverflow.com/questions/16174992/cant-get-argparse-to-read-quoted-string-with-dashes-in-it so I'd suggest trying

--ffmpeg-args='-bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1"'

Can you share ffmpeg output both with and without fixed_frame_rate_flag and corresponding error? The option itself is still supported and isn't deprecated, but the implementation might have changed in recent ffmpeg releases.

I thought i replied to this already sorry. Turns out I'm a fool I was missing the = in my ffmpef-args so thats working fine. As for the ffmpeg error im no longer on a version of ffmpeg that is an issue. But its been discussed here #31 (comment) and the error is

[h264_metadata @ 0x558813a280] Failed to write extradata.
Error initializing bitstream filter: h264_metadata```

Fix the reolink recommended ffmpeg args
Fixed W293 blank line contains whitespace
Fixed B950 line too long (91 > 80 characters)
@GhostlyCrowd
Copy link
Contributor Author

@keshavdv Here is the output you wanted

y$ ffmpeg -nostdin -y -stimeout 15000000 -rtsp_transport tcp -i "rtsp://unifiprotect:[email protected]:554//h264Preview_01_main" -c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k -metadata streamname=VIt3mr7LLdjlN8p7 -f flv - | /usr/bin/python3 -m unifi.clock_sync | nc 192.168.7.245 7550
ffmpeg version 4.3.3-0+deb11u1 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10 (Debian 10.2.1-6)
  configuration: --prefix=/usr --extra-version=0+deb11u1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, rtsp, from 'rtsp://unifiprotect:[email protected]:554//h264Preview_01_main':
  Metadata:
    title           : Session streamed by "preview"
    comment         : h264Preview_01_main
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 2560x1920, 12 tbr, 90k tbn, 180k tbc
    Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp
[h264_metadata @ 0x561f0f0db540] low_delay_hrd_flag does not match inferred value: 1, but should be 0.
[h264_metadata @ 0x561f0f0db540] Failed to write unit 0 (type 7).
[h264_metadata @ 0x561f0f0db540] Failed to write extradata.
Error initializing bitstream filter: h264_metadata
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
    Last message repeated 1 times

And with out the ixed_frame_rate_flag

ffmpeg -nostdin -y -stimeout 15000000 -rtsp_transport tcp -i "rtsp://unifiprotect:[email protected]:554//h264Preview_01_main" -c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001" -ar 32000 -ac 2 -codec:a aac -b:a 32k -metadata streamname=VIt3mr7LLdjlN8p7 -f flv - | /usr/bin/python3 -m unifi.clock_sync | nc 192.168.7.245 7550
ffmpeg version 4.3.3-0+deb11u1 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10 (Debian 10.2.1-6)
  configuration: --prefix=/usr --extra-version=0+deb11u1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, rtsp, from 'rtsp://unifiprotect:[email protected]:554//h264Preview_01_main':
  Metadata:
    title           : Session streamed by "preview"
    comment         : h264Preview_01_main
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 2560x1920, 12 tbr, 90k tbn, 180k tbc
    Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Output #0, flv, to 'pipe:':
  Metadata:
    title           : Session streamed by "preview"
    comment         : h264Preview_01_main
    streamname      : VIt3mr7LLdjlN8p7
    encoder         : Lavf58.45.100
    Stream #0:0: Video: h264 (Baseline) ([7][0][0][0] / 0x0007), yuv420p(progressive), 2560x1920, q=2-31, 12 tbr, 1k tbn, 90k tbc
    Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 32000 Hz, stereo, fltp, 32 kb/s
    Metadata:
      encoder         : Lavc58.91.100 aac
[flv @ 0x55aa769eda00] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
[aac @ 0x55aa769ee8c0] Queue input is backward in time
[flv @ 0x55aa769eda00] Non-monotonous DTS in output stream 0:1; previous: 672, current: 589; changing to 672. This may result in incorrect timestamps in the output file.
[flv @ 0x55aa769eda00] Non-monotonous DTS in output stream 0:1; previous: 672, current: 621; changing to 672. This may result in incorrect timestamps in the output file.
[flv @ 0x55aa769eda00] Non-monotonous DTS in output stream 0:1; previous: 672, current: 653; changing to 672. This may result in incorrect timestamps in the output file.

Forgot to update motion det url
Start of work to separate the main and substreams. Channel -c is not valid for stand alone cameras, so its been set to default=0 and required=True has been removed. Leaving it in for the future if its needed. 

Substreams are now picked with ```--substream, -s``` and choices are main or sub. this is a mandatory argument. ```-s main``` will take main high quality stream, ```-s sub``` will choose the cameras substream.  

Example: ```unifi-cam-proxy -H 192.168.7.245 -i 192.168.7.100 --mac 'ec:71:db:dc:47:b1' -c client.pem -t 5pSyFxkJy7UYhFVFqZpglSj4IxV7Bgis reolink -u unifiprotect -p 00423600 -s main --ffmpeg-args='-c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k'```

and

```unifi-cam-proxy -H 192.168.7.245 -i 192.168.7.100 --mac 'ec:71:db:dc:47:b1' -c client.pem -t 5pSyFxkJy7UYhFVFqZpglSj4IxV7Bgis reolink -u unifiprotect -p 00423600 -s sub --ffmpeg-args='-c:v copy -bsf:v "h264_metadata=tick_rate=60000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k'```

Which spawns the respective substream as seen here:

```ffmpeg -nostdin -y -stimeout 15000000 -rtsp_transport tcp -i "rtsp://unifiprotect:[email protected]:554//h264Preview_01_sub" -c:v copy -bsf:v "h264_metadata=tick_rate=30000/1001:fixed_frame_rate_flag=1" -ar 32000 -ac 2 -codec:a aac -b:a 32k -metadata streamname=VIt3mr7LLdjlN8p7 -f flv - | /usr/bin/python3 -m unifi.clock_sync | nc 192.168.7.245 7550
```

For the substream take note of the tick_rate. the substream is maximum 15fps can go no higher in the camera settings.

Future goal is to spawn the video channels properly so the hi quality and low quality substreams feed their respective counterparts in the Unifi protect system. 

Reasoning behind this work is the substream is Absolut flawless for preview playback, no lag at all.  But one would want the main hi resolution stream for recording purposes.
Added examples of using the main or sub camera substreams for reolink stand alone cams with motion detection.
@GhostlyCrowd
Copy link
Contributor Author

@keshavdv I'm working on main and substream separation now. Mainly the substream if i use it the web app and mobile app preview and live views are 100% stable. and in reality We should just want full resolution for recording.

I see 3 video streams are spawned 1-3 what one is for what? is Video1 say for recording or high rez viewing and Video2 is for ow resolution viewing and preview? and video 3 should never spawn with a snapshot url correct?

adjusted example command to push no audio to the proxy
the Reolink web UI just suggests it does not
@GhostlyCrowd GhostlyCrowd reopened this Jan 5, 2022
@GhostlyCrowd
Copy link
Contributor Author

Fat fingers on mobile sorry

@GhostlyCrowd
Copy link
Contributor Author

Finally, after a back and forth between flake8 and isort the actions passes and Lint is happy with everything.

@keshavdv keshavdv merged commit d643963 into keshavdv:main Jan 5, 2022
@Tr4um4z
Copy link

Tr4um4z commented Jan 24, 2022

i have tested over three different OS , ubuntu , photonOS and finally debian , with a reolink rlc520a does not work at resolution 2560x1440 , try different bitrates but nothing .... lowest is 1024

continue respawning ffmpeg.

@Tr4um4z
Copy link

Tr4um4z commented Jan 24, 2022

ok now it's work , the only thing that i have changed is the cam firmware , works with the latest reolink firmware that they have made improvements in the rtsp feed.
it's smooth at 1024 and 2048 , at 4096 start loosing some frames looking at the clock, resolution 2560x1440

@GhostlyCrowd i don't think is a VM cpu problem , i have tested on the old and new server , it's not a cpu limit i think.
probably is the ffmpeg metadata injector ....

any chance to get onvif alerts? i'm really interested in this project , i'm not a programmer but i can port forward my cam and donate $$ :)

@GhostlyCrowd
Copy link
Contributor Author

ok now it's work , the only thing that i have changed is the cam firmware , works with the latest reolink firmware that they have made improvements in the rtsp feed. it's smooth at 1024 and 2048 , at 4096 start loosing some frames looking at the clock, resolution 2560x1440

@GhostlyCrowd i don't think is a VM cpu problem , i have tested on the old and new server , it's not a cpu limit i think. probably is the ffmpeg metadata injector ....

any chance to get onvif alerts? i'm really interested in this project , i'm not a programmer but i can port forward my cam and donate $$ :)

Link to firmware?

Also 1024 and 2048 bit rate is the butter zone for these cameras otherwise with 3 streams open they begin to choke.

@Tr4um4z
Copy link

Tr4um4z commented Jan 24, 2022

https://reolink.com/download-center/?utm_source=Rdt&utm_medium=Post&utm_campaign=Rdt+Sidebar

but my is RLC-520A , the A version have vehicle and person detection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants