Capturing HD Video With The Pi Camera Module

Raspberry Pi Camera ModuleNot only can the Raspberry Pi camera capture photos but it can also capture full HD video at 1920×1080. The official camera module has been optimised to use the full hardware media capabilities of the Pi’s processor which allows it to handle video that a standard 700MHz would struggle to process.

To capture video you can use the “raspivid” utility. This should be available on the Pi assuming you followed my Installing The Raspberry Pi Camera Module guide and upgraded your operating system.

I would recommend you use an 8GB or larger SD card as you will find the space useful once you start capturing video. Use the following command to check how much space you’ve got available :

df -h

This gives you something looking this example from my 8GB SD card :

pi@raspberrypi ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.3G  3.4G  3.6G  49% /
/dev/root       7.3G  3.4G  3.6G  49% /
devtmpfs        180M     0  180M   0% /dev
tmpfs            38M  228K   38M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            75M     0   75M   0% /run/shm
/dev/mmcblk0p1   56M   19M   38M  33% /boot

Here I’ve got over 3GB free which is enough for 30 mins for full HD video. With the default options you will need up to 115MB per minute of HD video.

Using “raspivid” is really easy and the following command will capture 5 seconds of footage :

raspivid -o myvid.h264

You want more than 5 seconds right? So you just add the “t” option and specify a time :

raspivid -o myvid.h264 -t 60000

This will give you 60 seconds (60000 milliseconds) of video. The default resolution is 1920×1080 with a bitrate of 17Mbs giving files of 115MB per minute. To change the video resolution to 1280×720 you can use :

raspivid -o myvid.h264 -w 1280 -h 720

and to set a custom bitrate you can use :

raspivid -o myvid.h264 -w 1280 -h 720 -b 8000000

where “8000000″ is a bitrate of 8000Kbs (kilo bits per second) or 8Mb (8 mega bits per second). The default is usually 17000000. There is no right or wrong answer when it comes to bitrates. You have to experiment and decide what gives you an acceptable result.

As with capturing stills using raspistill there are a number of other advanced options you can use.

-?,   --help        : This help information
-w,   --width       : Set image width <size>. Default 1920
-h,   --height      : Set image height <size>. Default 1080
-b,   --bitrate     : Set bitrate. Use bits per second
                      (e.g. 10MBits/s would be -b 10000000)
-o,   --output      : Output filename <filename>
-v,   --verbose     : Output verbose information during run
-t,   --timeout     : Duration of video (in ms)
-d,   --demo        : Demo mode(cycle through range of camera
                      options, no capture)
-fps, --framerate   : Specify the frames per second to record
-e,   --penc        : Display preview image *after* encoding
                      (shows compression artifacts)
-p, --preview       : Preview window settings <'x,y,w,h'>
-f, --fullscreen    : Fullscreen preview mode
-n, --nopreview     : Do not display a preview window
-sh, --sharpness    : Set image sharpness (-100 to 100)
-co, --contrast     : Set image contrast (-100 to 100)
-br, --brightness   : Set image brightness (0 to 100)
-sa, --saturation   : Set image saturation (-100 to 100)
-ISO, --ISO         : Set capture ISO
-vs, --vstab        : Turn on video stablisation
-ev, --ev           : Set EV compensation
-ex, --exposure     : Set exposure mode (see Notes)
-awb, --awb         : Set AWB mode (see Notes)
-ifx, --imxfx       : Set image effect (see Notes)
-cfx, --colfx       : Set colour effect (U:V)
-mm, --metering     : Set metering mode (see Notes)
-rot, --rotation    : Set image rotation (0, 90, 180, 270)
-hf, --hflip        : Set horizontal flip
-vf, --vflip        : Set vertical flip

Additional values that can be supplied for Exposure, Auto White Balance and Image Effects are shown below :

Exposure  mode options  :   
off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,
verylong,fixedfps,antishake,fireworks   

AWB mode options :   
off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,
flash,horizon   

Image  Effect mode options :   
none,negative,solarise,sketch,denoise,emboss,oilpaint,h atch,
gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,
posterise,colourpoint,colourbalance,cartoon  

Metering Mode options  :   
average,spot,backlit,matrix

You may need to flip and rotate your video depending on how you’ve mounted the camera module.

To list all available options you can type :

raspivid | less

Convert Raw H264 Video Data To MP4

The Pi captures video as a raw H264 video stream. This is great but many media players will refuse to play it unless it is “wrapped” in a suitable container format. Luckily it is easy to wrap the data and produce a standard MP4 video file which should play in most media players. To do this wrapping we will use MP4Box which you can install using :

sudo apt-get update
sudo apt-get install -y gpac

Once installed you can then use the following command to wrap your H264 video data in an MP4 container file. This will allow most media players to play the video.

MP4Box -fps 30 -add myvid.h264 myvid.mp4

This will give you a nice video at 30 frames per second that should play in most modern media players.

Creating an MP4 File Using AVCONV

It should also be possible to do this wrapping with AVCONV (an alternative to ffmpeg) but I didn’t have much luck with this method. It creates MP4 files but these weren’t recognised in most of my mediaplayers and when they were I had issues skipping through them. Here are the details for reference :

Install using :

sudo apt-get install -y libav-tools

Use this command to wrap data into MP4 container :

avconv -r 30 -i myvid.h264 -vcodec copy myvid.mp4

“-r 30″ sets the frame rate which should be the same as the rate you used to capture the data. “-i myvid.h264″ defines the input file. “-vcodec copy” makes it clear we do not want to re-encode or convert the data. “myvid.mp4″ is the new output file.

Playing Video Files With OMXPlayer

In order to play your newly created MP4 files you can use OMXPlayer. This is installed by default on the latest Raspbian distribution but you can install manually using :

sudo apt-get -y install omxplayer

To play a video file you can use :

omxplayer myvid.mp4

To send audio via HDMI you can use :

omxplayer -p -o hdmi myvid.mp4

… but at this stage your video is unlikely to have any audio!

This entry was posted in Camera Module, Raspbian and tagged , , , , , , . Bookmark the permalink.


11 Responses to Capturing HD Video With The Pi Camera Module

  1. Seaton says:

    Thanks, but you’ve put:

    where “8000″ is a bitrate of 8000Kbs (kilo bits per second) or 8Mb (8 mega bits per second)

    The help shows it is bits per second. So 8Mbs should be “-b 8000000″

  2. Steve Cox says:

    Excellent Post, very helpful. I have the camera outputting to sd card and over the Ethernet using netcat. I would like to make the camera output over the USB cable to the PC either to view on the screen or store to hard drive. How would I do that?

    Thanks in advance
    Steve

  3. Oliver Aalami says:

    I am looking to capture live video from an external source (HDMI in or BNC in), compress it to H.264, scale it to 1080p, then wirelessly transmit that video to an iPad.

    Do you know who could help figure out how to build this??

    I know the wireless access point modules exist. Don’t know about the video scaling part. Thanks!

    • Matt says:

      The official camera can do HD encoding because it can use the power of the GPU. Encoding external video would have to make do with the CPU and Pi is unlikely to cope.

  4. salaboy says:

    Hi matt, great articles.
    I have a more technical question, do you know what’s the camera device ID? or where I can find it? It seems that the operating system doesn’t know anything about the camera, and all the magic is happening inside the raspivid and raspistill commands. Can you give me some insight about how to get hold of the camera device number or id without using those commands?

    • Matt says:

      Not sure to be honest. I don’t think the camera acts a normal device as it interfaces directly with the Pi’s GPU. Might be worth asking on the RaspberryPi.org forums as at least one of the camera developers posts there.

      • AndrewS says:

        I think you mean “as it doesn’t interface directly with the ARM processor” – the camera connects to the GPU, which does all the camera setup, encoding etc. and then the already-compressed video is finally handed over to the ARM side where it simply gets written to SD card.

  5. Simon says:

    You say a default bit rate of 17Mbs giving files of 115MB for a minute. This doesn’t seem to add up, or am I confusing bits and bites?

    • Matt says:

      17Mbits per second is 17/8 MBytes per second = 2.1MBytes per second = 128MBytes per minute. Given the video codec will do some compression etc then 115MB is close enough.

  6. Giles says:

    Is is possible to create a system that starts recording when you tell it to start and continues until you tell it to stop, like a normal video camera?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>