Saving h264 using feature-h264-videostorage

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

So a few people have been trying out the feature-h264-videostorage branch and others have asked how to get it working. Please don't try this if this for your first ZoneMinder installation, don't try and upgrade an existing install, as a general rule feature branches are not supported. And sometimes we make breaking changes.
Hopefully others in this thread can help you out if you hit issues.

What is does?
It is two separate features that have been merged into a single branch:
1. h264 Passthrough - Passes FFmpeg cameras h264 packets direct to an mp4 container. Best used in Record mode. By it's nature you must have a h264 capable camera using FFmpeg source type, and if you want to record audio it must be compatible with mp4 container, and if you want that to playback in a browser has to be AAC.
2. h264 Encode - Takes any camera sources jpegs and encodes them to h264. Would not advise this for h264 capable camera as you would be decoding and encoding back to same format. It is good for old mpeg cameras etc. But you take a cpu hit on the h264 encoding.

What it still needs
1. Some sort of buffer for h264 packets, so we can do Modect and other functions that only record video on demand. Currently video starts recording but has to wait for a keyframe before picture is established.
2. Transcode Audio to AAC so we can play back via browsers
3. Enabled streaming live video without decoding to jpegs
4. Stop decoding jpegs all together.
5. Fix Timeline view


Installing
If you have some non h264 capable cameras and want to use the h264 encode function you need to install libx264-dev and libmp4v2-dev or whatever are your distro equivalents. I have added these to the ubuntu1604 debian folder already so if you use ubuntu you will be fine.

To install you need to build a package until I get an auto build making the PPA and RPM's
Deb
Building Package from Source
Only difference is you need to tell the script to use the feature branch, so when install guide talks about master and stable commands use this instead.

Code: Select all

./do_debian_package.sh `lsb_release -a 2>/dev/null | grep Codename | awk '{print $2}'`  `date +%Y%m%d`01 local feature-h264-videostorage
RPM
How to build custom zoneminder package
When you get to the How to modify the source prior to build section
You will need to checkout the branch also, so add that command to end of this section of commands.

Code: Select all

cd
git clone https://github.com/ZoneMinder/ZoneMinder
cd ZoneMinder
git submodule init
git submodule update
git checkout feature-h264-videostorage
Resume Install
Once you have your package head back to standard install guides and substitute the standard install process with your new custom built package.

Using it
The branch adds a new Storage tab to monitor setup
Screen Shot 2016-07-31 at 8.59.05 AM.png
Screen Shot 2016-07-31 at 8.59.05 AM.png (111.94 KiB) Viewed 28084 times
Save Jpegs
Decide whether or not to keep ZoneMinder's standard jpegs. Note: Currently we are still decoding h264 to enable the live streaming function of ZoneMinder.
Video Writer
Choose h264 Passthrough or h264 Encode, refer to What it Does above for which to choose.
Optional Encoder Parameters
For h264 Encode option you can play with the parameters
Whether to store the audio stream when saving an event.
Do that, but if you turn this on and start getting issues probably some problem with your stream, so turn it off again.

I will update this thread as people give feedback etc.
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Re: Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

[Place holder 1]
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Re: Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

[Place holder 2]
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
jzaw
Posts: 2
Joined: Sun Jul 31, 2016 9:36 am

Re: Saving h264 using feature-h264-videostorage

Post by jzaw »

Nice!
as you know I've been using this branch for a while and really appreciate the work being done
If there's anything I can do as a beta tester you know you only need to ask

cheers

J
Dude2016
Posts: 5
Joined: Sun Jul 31, 2016 6:01 pm

Re: Saving h264 using feature-h264-videostorage

Post by Dude2016 »

Minor clarification needed:
When building the Deb package from source, the instructions are for the latest master snapshot or latest stable release. What is the feature-h264-videostorage branch using? Can we separate this branch into a known-good-working (e.g. stable) and a dev version? That way we can use the stable version even if the dev version breaks things.

Also, here is the command to create a stream tee so that you can view the video simultaneously while recording to disk. I used avconv because ffmpeg was branched in ubuntu 14.04 to avconv but it "might" work directly with ffmpeg on 16.04 but I haven't tested it.

Code: Select all

avconv -i rtsp://192.168.1.XXX:8554/ -acodec copy -vcodec copy -map 0 -f segment -segment_time 3600 -segment_format mp4 -crf 22.0  "/events/video/CameraXXX/`date +%Y-%m-%d-%H-%M-%S`-capture-%03d.mp4" -vcodec copy -an -f flv -metadata streamName=CameraXXX tcp://0.0.0.0:6666 
And then you need a suitable viewer to view the stream. I don't know what zoneminder uses so I created my own using apache and jwplayer. In /var/www/html you can add a file with the following contents:

Code: Select all

<html>
  <head>
    <title>Camera RTMP stream</title>
  </head>
  <body>
    <div id="video-jwplayer_wrapper" style="position: relative; display: block; width: 960px; height: 540px;">
      <object type="application/x-shockwave-flash" data="/jwplayer/jwplayer.flash.swf" width="100%" height="100%" bgcolor="#000000" id="video-jwplayer" name="video-jwplayer" tabindex="0">
        <param name="allowfullscreen" value="true">
        <param name="allowscriptaccess" value="always">
        <param name="seamlesstabbing" value="true">
        <param name="wmode" value="opaque">
      </object>
      <div id="video-jwplayer_aspect" style="display: none;"></div>
      <div id="video-jwplayer_jwpsrv" style="position: absolute; top: 0px; z-index: 10;"></div>
    </div>
    <script src="/jwplayer/jwplayer.js"></script>
    <script type="text/javascript">
     <script type="text/javascript">
    jwplayer('video-jwplayer').setup({
      flashplayer:"/jwplayer/jwplayer.flash.swf"
      , file:"rtmp://" + window.location.hostname + "/flvplayback/flv:CameraXXX.flv"
      , autoStart: true
      , rtmp:{
        bufferlength:0.1
      }
      , deliveryType: "streaming"
      , width: 960
      , height: 540
      , player: {
        modes: {
          linear: {
            controls:{
              stream:{
                manage:false
                , enabled: false
              }
            }
          }
         }
      }
      , shows: {
        streamTimer: {
          enabled: true
          , tickRate: 100
        }
      }
    });
    </script>
  </body>
</html> 
          
This has been up and running for over a year so it's rock solid. I just go to the web page and it loads the camera stream. It needs improvement to work as a montage because these streams are full resolution 1080p@30fps which adds up and overloads the network connection of the server. I have 14 cameras running 24x7 on this setup and it hardly uses any server resources at all. :D
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Re: Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

Dude not keen to maintain two branches, since we can always use git to use roll back to a known working version, jzaw was doing feature-h264-videostorage~9 recently until I fixed a bug for him. We merge master in on an ad hoc basis to keep the gap minimal, so to answer the question we track master. But as of this week master and stable are close to the same due to release of 1.30.0.

Thanks for the example, I have seen a few ways to run a cli to stream a video, ffserver is another way. Have your tried anything other than flash for the live stream? Just need to turn one into a lib based version that we control and not need to make cli calls. Have the ability to stream lower res to solve the montage issues, a way to avoid multiple connection issues without opening new ports (web sockets?) and ideally have backward compatibility to convert previously recorded jpegs into a compatible stream.
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
Dude2016
Posts: 5
Joined: Sun Jul 31, 2016 6:01 pm

Re: Saving h264 using feature-h264-videostorage

Post by Dude2016 »

Here's some python code for detecting motion in h264 vector data:
http://bits.citrusbyte.com/motion-detec ... pberry-pi/
Dude2016
Posts: 5
Joined: Sun Jul 31, 2016 6:01 pm

Re: Saving h264 using feature-h264-videostorage

Post by Dude2016 »

I got the following info from this forum thread https://www.raspberrypi.org/forums/view ... 5&p=513958
Here's the code to make ffmpeg expose the motion vectors from h264:

Code: Select all

gcc -O3 -std=c99  -o motion_watch motion_watch.c  -I/home/pi/ffmpeg/ffmpeg-1.2.x/target/include/ /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavfilter/libavfilter.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libswscale/libswscale.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavdevice/libavdevice.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavformat/libavformat.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavcodec/libavcodec.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavutil/libavutil.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/zlib-1.2.8/libz.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libtheora-1.1.1/lib/.libs/libtheoraenc.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libtheora-1.1.1/lib/.libs/libtheora.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libtheora-1.1.1/lib/.libs/libtheoradec.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/yasm-1.2.0/libyasm.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/x264-snapshot-20140109-2245/libx264.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libvpx-v1.1.0/libvpx_g.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libvpx-v1.1.0/libvpx.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/faac-1.28/libfaac/.libs/libfaac.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/faac-1.28/common/mp4v2/libmp4v2.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libpostproc/libpostproc.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libavutil/libavutil.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/ffmpeg-1.2.1/libswresample/libswresample.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/bzip2-1.0.6/libbz2.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/lame-3.99.5/mpglib/.libs/libmpgdecoder.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/lame-3.99.5/libmp3lame/.libs/libmp3lame.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/xvidcore/build/generic/=build/libxvidcore.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libpng-1.6.2/.libs/libpng16.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libogg-1.3.1/src/.libs/libogg.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libvorbis-1.3.3/lib/.libs/libvorbisenc.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libvorbis-1.3.3/lib/.libs/libvorbis.a /home/pi/ffmpeg/ffmpeg-1.2.x/build/libvorbis-1.3.3/lib/.libs/libvorbisfile.a -lpthread -lbz2 -lm -lz
Here's the C code to detect motion in h264 video files:
https://gist.github.com/anonymous/9698508
Here's C++ algorithms to do real-time motion detection on h264
https://sourceforge.net/projects/livevideo/
Here's a detailed tutorial and documentation of the above
https://26e4ce32-a-62cb3a1a-s-sites.goo ... edirects=0
Dude2016
Posts: 5
Joined: Sun Jul 31, 2016 6:01 pm

Re: Saving h264 using feature-h264-videostorage

Post by Dude2016 »

SteveGilvarry wrote:Dude not keen to maintain two branches, since we can always use git to use roll back to a known working version, jzaw was doing feature-h264-videostorage~9 recently until I fixed a bug for him. We merge master in on an ad hoc basis to keep the gap minimal, so to answer the question we track master. But as of this week master and stable are close to the same due to release of 1.30.0.

Thanks for the example, I have seen a few ways to run a cli to stream a video, ffserver is another way. Have your tried anything other than flash for the live stream? Just need to turn one into a lib based version that we control and not need to make cli calls. Have the ability to stream lower res to solve the montage issues, a way to avoid multiple connection issues without opening new ports (web sockets?) and ideally have backward compatibility to convert previously recorded jpegs into a compatible stream.
OK, can we assume feature-h264-videostorage~9 is a safe place to get started on this project? Can we add this to top of the thread that this branch is a working branch and the feature-h264-videostorage is unstable? It takes a large amount of setting up to get a working zoneminder with all these different cameras so it would greatly help all of us if we can eliminate whether it's the camera or the branch that is the problem.

I tried several other stream formats but went with flv because of jwplayer. I'm not familiar with any other players but it would be really nice to find a 100% FOSS player that doesn't require any additional setup. I just read that firefox on Linux now plays h264 directly via HTML5. Here's the code:

Code: Select all

<video controls="controls" width="500">
    <!-- if Firefox -->
    <source src="video.ogg" type="video/ogg" />
    <!-- if Safari/Chrome-->
    <source src="video.mp4" type="video/mp4" />
</video>
I'll test it out in the next couple of days to see if it works with the streams I have. You can just tell avconv whatever format you want and it should work. Here's the MP4 version

Code: Select all

avconv -i rtsp://192.168.1.XXX:8554/ -acodec copy -vcodec copy -map 0 -f segment -segment_time 3600 -segment_format mp4 -crf 22.0  "/events/video/CameraXXX/`date +%Y-%m-%d-%H-%M-%S`-capture-%03d.mp4" -f rtp -vcodec copy rtp://localhost/CameraXXX.mp4 
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Re: Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

Use current branch,
zaw was doing feature-h264-videostorage~9 recently until I fixed a bug for him.
As a general rule use current branch as that helps us find bugs and fix them, only if having a new problem after an update should you rollback. You can roll back or forward without changing the database, unless the change is DB related, which don't happen that often and I will put up top.
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
jzaw
Posts: 2
Joined: Sun Jul 31, 2016 9:36 am

Re: Saving h264 using feature-h264-videostorage

Post by jzaw »

Dude2016 wrote: OK, can we assume feature-h264-videostorage~9 is a safe place to get started on this project?
Greets!

I can confirm that feature-h264-videostorage current does work fine, with the caveats of the jpg viewing of monitors etc that's been mentioned at the head of this thread

J
User avatar
iconnor
Posts: 2880
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Saving h264 using feature-h264-videostorage

Post by iconnor »

Let me jump in here and say that anyone on a ubuntu build can use my storageareas ppa.

add-apt-repository ppa:iconnor/zoneminder-storageareas

Let me say though, that all of this code is bleeding edge. Don't ask us for a stable version, because none of it is stable.
That being said, I am using my storageareas version in production and it mostly works. It also has all kinds of cool fixes and features that no other branch has.
Dude2016
Posts: 5
Joined: Sun Jul 31, 2016 6:01 pm

Re: Saving h264 using feature-h264-videostorage

Post by Dude2016 »

iconnor wrote:Let me jump in here and say that anyone on a ubuntu build can use my storageareas ppa.

add-apt-repository ppa:iconnor/zoneminder-storageareas

Let me say though, that all of this code is bleeding edge. Don't ask us for a stable version, because none of it is stable.
That being said, I am using my storageareas version in production and it mostly works. It also has all kinds of cool fixes and features that no other branch has.
Can you elaborate on the fixes and features? Is it a nightly build taken from the current branch or...? Which versions of Ubuntu have deb packages?
lordluis2000
Posts: 1
Joined: Tue Sep 06, 2016 6:11 pm

Re: Saving h264 using feature-h264-videostorage

Post by lordluis2000 »

Hi I recently installed this package in order to test recording cameras over h.264 instead of JPEG. The zoneminder was working good. But after I did install the feature can not see video from cameras I got this error on console windows

getStreamCmdResponse stream error: socket_bind( /var/run/zm/zms-222266w.sock ) failed: Address already in uselogger.js:85:5

getStreamCmdResponse stream error: socket_sendto( /var/run/zm/zms-222266s.sock ) failed: No such file or directorylogger.js:85:5

Detected streamWatch of type: stream stopped, restarting

I am over Ubutu

No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 18 Sarah
Release: 18
Codename: sarah
and zoneminder 1.30.0
any help?
SteveGilvarry
Posts: 494
Joined: Sun Jun 29, 2014 1:12 pm
Location: Melbourne, AU

Re: Saving h264 using feature-h264-videostorage

Post by SteveGilvarry »

Nothing really touched in live streaming so I can only assume a standard streaming issue like cgi-bin setup, apache conf or mods.
Production Zoneminder 1.37.x (Living dangerously)
Random Selection of Cameras (Dahua and Hikvision)
Post Reply