The Raspberry Pi Camera Module: Part 5 - Slow Motion Video

The Raspberry Pi Camera Module Part 5 Slow Motion Video

Shooting slow motion video with the Raspberry Pi Camera Module is dead easy

Last year I wrote a short series of Raspberry Pi camera module tutorials, sharing what I’d learnt as I got to grips with the little camera board.

Between then and now, I was offered an opportunity to write a camera module article for the 31st issue of the MagPi Magazine where I shared a step-by-step guide to shooting and processing 90fps slow motion video with your Raspberry Pi.

As I like to store all my info and knowledge on this blog, I thought I’d write something similar here which will link nicely with my previous four camera module articles.

So, let’s go all movie-style ‘over the top explosion jumping through the air’ and get in to some Raspberry Pi slow motion!

What you’ll need

In order to be the next Speilberg with your £25 computer, you’ll just need the standard Raspberry Pi camera module. You’ll also need a Raspberry Pi with Raspbian set up as well, of course.

Raspberry Pi and camera module

A Raspberry Pi and a camera module is all you need

Optionally you could grab one of my handy ProtoCam boards to make your camera project really clean and tidy, with prototyping options as well!

ProtoCam boards

My ProtoCam boards, both funded via Kickstarter

Connect and set up your camera module

I covered how to set up your camera module in the first two camera module blog posts I wrote. If you haven’t got yours attached and set up yet, take a look at Part 1 and Part 2 now.

Install a video converter

The Pi records video into a raw h264 file which won’t work on most of the devices you’re likely to play them on. However, we can get the Pi to convert them to a playable format (MP4) straight after we’ve recorded them, in the script I’ve shared later on in this post.

To do this we install a package called gpac. In terminal, run the following commands one after the other:

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

gpac will convert your raw video file into a playable format, and installing it is easy.

Testing the camera module

Now that gpac is installed, let’s make sure everything’s working as it should by testing the camera with a couple of terminal commands.

With a screen connected, open a terminal window and type the following command:

raspistill -o test.jpg

The camera’s image should appear on screen for a short time and the image saved to your home folder. If it doesn’t work, check you typed the command correctly or turn off your Pi and re-connect the camera belt before trying again.

Create a Python script

We’ll be using Python to create our slow motion video script.

Open your favourite text editor (I use Notepad++) and copy the code below being careful not to misspell anything along the way. Alternatively, grab a copy of the code from my PasteBin here.

This code takes a 30 second video at 90fps, then processes the raw file for you. Cool huh?

import os
import time

print 'Starting programme'
time.sleep(2)

##### Record the slow motion video #####
# '-w' sets the width  # '-h' sets the height
# '-fps' sets the frames per second (90 maximum - for slow motion)
# 't' sets the time in milliseconds (30000 = 30 seconds)
# '-o' sets the output filename

print 'Recording started (30 seconds)'
os.system("raspivid -w 640 -h 480 -fps 90 -t 30000 -o slowmotion.h264")

print 'Recording complete. Please wait...'
time.sleep(2)

##### Convert the raw recorded video file to playable mp4 #####
# '-add' is the name of the raw video we want to convert
# The second file name is the output mp4 file (followed by '.mp4')

print 'Converting video. Please wait...'
os.system("MP4Box -add slowmotion.h264 slowmotion.mp4") 

print 'Video conversion complete'
time.sleep(2)

print 'Closing programme'
time.sleep(2)

You don’t need to copy the comments if you don’t want to (lines starting with #) – Python just ignores them anyway.

Save your file as slowmotion.py in your home directory.

Run it!

To run the script simply open a terminal window, type ‘cd’ and press ‘return’ to ensure you’re in the home directory, then use the following command:

sudo python slowmotion.py

After pressing enter, you will see the status of the script printed in your terminal window as it carries out its commands, and the camera module’s red LED will light up while it’s recording.

gpac processing

gpac doing its thing

The script will end when the video has been converted. You can watch the video on your Pi straight away by using omxplayer which is included in Raspbian. Simply type:

omxplayer slowmotion.mp4

You could also copy your video onto any other device like your tablet, smartphone or PC.

Summary

So that’s it – simple huh?

This is a nice easy project with minimal parts and cost, that gives hours if not days of fun. I’m planning on taking this to my team’s ice hockey practice one weekend, to try and capture some slow motion slap shots and ice sprays. I just need to work out a way of making my Pi more mobile!

Continue reading here: Making A Home-Made Raspberry Pi Robot

Was this article helpful?

0 0