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  :   

AWB mode options :   

Image  Effect mode options :   
none,negative,solarise,sketch,denoise,emboss,oilpaint,h atch,

Metering Mode options  :   

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.

21 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

  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 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?

    • Matt says:

      If you use the “PiCamera” Python module you can tell the camera to start and stop recording. This module is installed by default on the official Raspbian image now.

  7. Alan says:

    I get approximately 1 second of video then my Pi reboots. Any suggestions?


  8. Dingus says:

    Lots of great information here.
    I haven’t yet made the purchase but depending on my research I might use this item.
    I work in a field that requires safety, photo and video for archive and estimations. the company I work for wont buy the units that are made for this use so I figure I will build one for myself.
    It has to record when told to, stop when told to. It has to be as small as possible and light as possible. It will all be fitted into a type of self made case and mounted on a construction hard hat.
    I have all of that figured out and mostly made but I am not sure how I should proceed with this project as far as the electronics.
    If it works then I will make more for the other gents that I work with so they can keep their hands on the supports instead of fiddling with a camera.

  9. ma says:

    Hi, Matt,

    Do you think it’s possible to save the video directly to a external hard drive via USB port? I need more than 120Gb memory…

    • Matt says:

      I have saved video directly to a USB stick so this should be possible. Just get it mounted and you should be able to save to the appropriate mount point.

  10. Trung says:

    How do I run the pi camera live on the screen with customize resolution ? I’m using Python language. I tried with the preview command but it give me the full screen image. Thanks

  11. Steve says:

    Novice so please forgive my ignorance…
    This project looks cool but I have a few questions before I embark.

    1.Can I use two Pi Camera’s and record both feeds concurrently?

    2.Could I use analogue ntsc cameras instead? For instance these as they ‘hide’ better

    3.Is it possible to ‘loop’? For instance have 60 minutes video looping? Perhaps I would need two files and flip between them replacing the old?

    Thanks in advance.

    PS: This would be for us in my car to continually record/loop to SD card. In event of accident footage could be used.
    I have another idea to use this at home with a motion sensor to switch on/off, but that’s secondary.

    • Matt says:

      1) The Pi only has 1 camera interface so you can’t use 2 camera modules. You could use 2 webcams on the USB ports but the mechanism for recording would be different and I’ve never tried that. The webcam quality would be nowhere near the camera module.

      2. The Pi has no analogue inputs so you would need some other hardware to digitise it. There may be a USB device to do this? The great thing with the camera module is that it can use hardware video encoding to get full HD. Any other camera is going to load the CPU and will not perform as well.

      3. It is possible to continuously record. Take a look at my Video Capture Unit. That can be used as a basic dash-cam. You can set the number of files to use and how big each one is. It then records in a loop.

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=""> <s> <strike> <strong>