Not 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 :
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 :
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!