Be a video star

Now that we know our camera module is working, we can try and capture some video. To do this, we'll use the raspivid utility. The following command will take 5 seconds of high-definition video and save the file to your Raspberry Pi:

$ raspivid –o test.h264 –t 5000

You'll notice that file is called test.h264—this is because the video is captured as a raw H.264 video stream. Unfortunately, not many media players will handle these files (although VLC player will—it rocks and handles practically anything you throw at it—get it on your PC at www.videolan.org).

If you want to play the file on smartphones and conventional media players, then we will need to wrap it in a container format, such as MPEG-4, and give the file a .mp4 extension.

To do this, we'll use the GPAC package, which is an open source multimedia framework. It comes with a utility called MP4Box, which is a tool we'll use to create an MP4 container for our video file:

  1. First, install the GPAC package:
    $ sudo apt-get install gpac
    
  2. Once it's installed, run the command to convert the test video we created:
    $ MP4Box -fps 30 -add test.h264 test.mp4
    

You should now have the file, test.mp4, which you can download and play on your PC or smartphone.

Tip

Another popular conversion tool is ffmpeg, which I use a lot on Windows to convert video files; however, it can be quite complex and although there is a package for the Raspberry Pi, I actually couldn't get it to convert properly on the Pi. MP4Box is much more straightforward and fitting for our needs.

Caught on camera

So, we now have a method of capturing still images and video, which we can put to use in our security system. If we want to have this running constantly, we could write a script to take video constantly, but this would soon fill up our memory card and wouldn't be particularly efficient. So, we'll combine our camera system with the motion detectors we connected earlier.

In the last chapter, we created an alarm zone which had a couple of sensors and a motion detector connected to our system on the input GPA0. So, let's write a script that will take a video clip whenever the motion detector is triggered:

#!/bin/bash


#set up port expander 
sudo i2cset –y 1 0x20 0x00 0xFF

# loop forever
while true
do
  # read the GPA inputs
  GPA=$(sudo i2cget –y 1 0x20 0x12)

  # detect the zone on input 0
  if [ $GPA == "0x01" ]
  then
    #circuit normally closed so zone is OK
    #short delay
sleep 0.5

  else
    #zone is activated so take a 20 sec video clip
    
    #filename will be based on current timestamp
    sDate='date +%d%m%y'
    sTime='date +%T'
    echo "Zone 1 Activate at $sDate $sTime"

#take video clip
raspivid –o $sDate$sTime.h264 –t 20000
    
#convert to MP4
MP4Box -fps 30 -add $sDate$sTime.h264 $sDate$sTime.mp4
  fi
done
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.28.237