Tutorial 13: Shoot Video with Pi Camera

Although not used in the Cookie Thief Project, I thought it would be neat to show you how to take a video recording using the Pi Camera. Normally the output file format from video capture is a raw .h264 format. I also show you how to convert this file format into something a little easier to view: a MP4.

This tutorial is a little older (published in 2015!). The main principles are still relevant, so I haven't needed to update this lesson.


DIFFICULTY
EASY
LINUX UNDERSTANDING
LITTLE
COMPUTER FAMILIARITY
LITTLE
PYTHON PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to use Python 3 to capture a video with the Pi Camera
  • How to convert raw .h264 video files to .mp4 automatically
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!


import picamera
from time import sleep
from subprocess import call

# Setup the camera
with picamera.PiCamera() as camera:
    # Start recording
    camera.start_recording("pythonVideo.h264")
    sleep(5)
    # Stop recording
    camera.stop_recording()

# The camera is now closed.

print("We are going to convert the video.")
# Define the command we want to execute.
command = "MP4Box -add pythonVideo.h264 convertedVideo.mp4"
# Execute our command
call([command], shell=True)
# Video converted.
print("Video converted.")
If you’re interested in a little more on this topic, check out: