In Part 1 we hooked up the Camera Module to our Raspberry Pi. That was the easy bit. In part 2 we will look at what you need to do to get your camera doing the thing it was born to do – take pictures and videos.
For this part we’ll look at how to get started with taking images and videos, and then move on to more advanced stuff in part 3, such as slow motion and time-lapse videos.
Ready? Let’s go…
Before we begin, we need to do a couple of things to make sure your Pi is up to date and ready to shoot.
It’s worth doing this regularly anyway, but it’s important to do this now just in case. Enter the following commands in Terminal to update your Pi:
sudo apt-get update
sudo apt-get upgrade
Enable the Camera
Your Pi might not be ready to use the camera model – it’s easily done though, simply go into the configuration screen and enable it there:
Select option 5 to enable the camera module:
There are a number of different types of shot you can take with the camera module, with a lot of variables you can play with as well (I will cover this seperately). For the purpose of this post I’ll keep it to just a few to get you started.
Standard Still Image
In the Terminal, enter the following command for a standard ‘off the shelf’ camera shot:
raspistill -o imagename.jpg
- ‘raspistill‘ – is the camera command
- ‘-o‘ – means to “output” – it tells the Pi that this is to be saved to disk rather than just a preview (thanks to Simone Lippolis for confirming what this was)
- ‘imagename.jpg‘ – is the name of the image file, followed by the image file format
This waits 5 seconds and then takes a shot (you can change this if you want). Here’s one I took looking down out of my apartment window:
Standard Video Recording
Again in the Terminal, enter this command to take a 20 second video:
raspivid -o video.h264 -t 20000
- ‘raspivid‘ – is the video record command
- ‘-o‘ – as before, means “output” – this tells the Pi that this is to be saved to disk rather than just a preview
- ‘video.h264‘ – is the name of the video followed by the video encoding format
- ‘-t 20000‘ – is how long the video should record in milliseconds (20000 = 20 seconds)
Now this will record to a raw .h264 file, which you then need to convert/encode to an mp4 file otherwise you won’t be able to play it. For this we need to install MP4Box which will do the conversion for us:
sudo apt-get install -y gpac
Once you’ve installed that, here’s the code to convert your raw h264 stream to mp4:
MP4Box -fps 30 -add video.h264 video.mp4
- ‘MP4Box‘ – is the command to use MP4Box
- ‘-fps 30‘ – is the frames per seconds for the output video (change this for different speed videos such as slow motion)
- ‘-add video.h264‘ – tells the Pi which raw video to convert
- ‘video.mp4‘ – this is the output video file name in mp4 codec