Raspberry PI 4 timelapse, video recording and video streaming.

To download as a .pdf please click https://drive.google.com/file/d/149SQgipGs-uaK1hS0kpJMGdBLuh2YMn7/view?usp=sharing

A friend wanted to experiment with capturing still images and video using her Raspberry PI4. I knew this would be complicated so I documented the whole process. Here are 3 ways to grab images or video using a PI 4, but I also tested most of this with a Raspberry PI 3B+ too.


Timelapse recording with webcam



Taken from https://www.raspberrypi.org/documentation/usage/webcams/

First let’s do an update, then we can install fswebcam

sudo apt update sudo apt install fswebcam

next add your user to the video group to get the right permissions (replace pi if you use a different username)

sudo usermod -a -G video pi
sudo mkdir webcam
chmod +rwx webcam
cd webcam

test the program using

fswebcam image.jpeg

you should see something like 
--- Opening /dev/video0... Trying source module v4l2... /dev/video0 opened. No input was specified, using the first. Adjusting resolution from 384x288 to 352x288. --- Capturing frame... Captured frame in 0.00 seconds. --- Processing captured image... Writing JPEG image to 'testimage.jpg'.

Open a file explorer and double click the image, you should see an image like below

great! Now this is at the base resolution of 320x240 pixels, but we can go much higher depending on your webcam

try adding the -r parameter to set the resolution -

fswebcam -r 1280x720 image2.jpg

most webcams can handle it

if you have a hd1080 cam try

fswebcam -r 1920x1080 imagehd.jpg

if you would like to remove the banner change it to

fswebcam -r 1920x1080 –no-banner imagehd2.jpg

Setting up regular frame capture If you are experienced with cron, then you can use that. For beginners this is a little easier -

create a configuration file using

sudo nano ~/.fswebcam.conf

copy and paste the following data into it -

device /dev/video0 
input 0 
loop 15 
skip 20 
background 
resolution 1920x1080 
set brightness=60% 
set contrast=15% 
top-banner title "I am watching you" 
timestamp "%Y-%m-%d_%H-%M-%S (%Z)" 
jpeg 95 
save /home/pi/webcam/"%Y-%m-%d_%H-%M-%S".jpg 
palette MJPEG

to run fswebcam using this script type

sudo fswebcam -c ~/.fswebcam.conf

you may need to adjust the resolution, brightness and contrast figures to suit your webcam

the device will select the first video capture device, if you have more than one then you may need to change it to /dev/video1 or /dev/video2 etc. If not stated it will default to video0 anyway.

The loop parameter specifies how often in seconds that the frame will be captured, 15 seconds here.

The skip value sets how many frames should be ignored before a frame is captured as older webcams may need a few frames to settle their picture.

Background allows fswebcam to run in the background as a daemon job, easier to configure than cron.

To stop the capture you will need to type

sudo pkill fswebcam

you should do this rather than run fswebcam again as it will re-read the conf file.

The resolution should be set you your webcam resolution, this is normally higher for still frames than for video. See https://www.digitalcitizen.life/what-screen-resolution-or-aspect-ratio-what-do-720p-1080i-1080p-mean for more info.

The top banner and title allow you to label each image, maybe ‘Garage’, ‘Garden’ or whatever.

The timestamp will set a filename in the format ‘2020-04-23_11-08-45.jpg’, the reason I set the year, month, day is so that the files will be ordered correctly in your file explorer, that is by year, month, day, hours, minutes, seconds. You can format it differently by experimenting. %Y is four digit year %y is two digit year, %m is month, %d is date. Then %H is hour in 24 hr format, %M is minutes (not month) and %S is seconds. You can also add %Z for GMT/BST.

Of course you can set up different conf files and run them like this

sudo fswebcam -c ~/.fswebcam.conf
sudo fswebcam -c ~/.fswebcam2.conf




Make a timelapse video




If you wish to convert your collection of timelapse images into a video stream use the ffmpeg command like so -


sudo ffmpeg -framerate 1-pattern_type glob -i '*.jpg' 'video.avi'


you may need to tweak the parameters to suit your needs.



Now for recording video


If you have ffmeg codecs installed you can simply use them by typing

sudo ffmpeg -f video4linux2 -r 25 -s 1280x720 -i /dev/video0 /home/pi/webcam/test.avi

You can change it to .mpg too and set the frame rate higher than -r 25 and resolution more than -s 1280x720 if your webcab can deliver, try the settings.

If you do not have them installed simply type

sudo apt install ffmpeg



Install OBS to stream to Youtube,Twitch, Smashcast, Facebook live, Twitter, Periscope etc.


So I discovered that it is possible to install OBS the streaming and recording software (the same one this video is recorded on in Linux, it is cross-platform, but no Rasberry Pi version exists) But it has to be compiled from ‘C’. Now normally this could be daunting, even for me, but the link below contains commands to set up the environment and a script that automates the whole compilation process.

Thanks to https://obsproject.com/forum/threads/obs-raspberry-pi-build-instructions.115739/

First if you are using a Raspberry PI with an SD card it would be a good idea to set up a swapfile. This is optional if you are using an SSD as they do not benefit from swapspace.

sudo dd if=/dev/zero of=/var/swap2 bs=1024 count=1000000 sudo chmod 600 /var/swap2sudo mkswap /var/swap2
sudo swapon /var/swap2
sudo swapon

then make a scripts folder and create an installation shell script

cd ~
mkdir scripts 
cd scripts 



sudo nano installOBS.sh

Copy and paste this code in (some of the commands are multi-line)

you can copy the script accurately from https://pastebin.com/ULfzc51q

#!/bin/bash
#File: installobs.sh (sudo chmod +x installobs.sh)
#!!! Please run from /home/pi/Downloads (cd /home/pi/Downloads)
sudo apt-get --allow-releaseinfo-change update
sudo DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
sudo apt-get -y install build-essential checkinstall cmake git libmbedtls-dev libasound2-dev libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libcurl4-openssl-dev libfontconfig1-dev libfreetype6-dev libgl1-mesa-dev libjack-jackd2-dev libjansson-dev libluajit-5.1-dev libpulse-dev libqt5x11extras5-dev libspeexdsp-dev libswresample-dev libswscale-dev libudev-dev libv4l-dev libvlc-dev libx11-dev libx11-xcb1 libx11-xcb-dev libxcb-xinput0 libxcb-xinput-dev libxcb-randr0 libxcb-randr0-dev libxcb-xfixes0 libxcb-xfixes0-dev libx264-dev libxcb-shm0-dev libxcb-xinerama0-dev libxcomposite-dev libxinerama-dev pkg-config python3-dev qtbase5-dev libqt5svg5-dev swig
sudo wget http://ftp.uk.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac1_0.1.4-2+b1_armhf.deb
sudo wget http://ftp.uk.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac-dev_0.1.4-2+b1_armhf.deb
sudo dpkg -i libfdk-aac1_0.1.4-2+b1_armhf.deb
sudo dpkg -i libfdk-aac-dev_0.1.4-2+b1_armhf.deb
sudo git clone --recursive https://github.com/obsproject/obs-studio.git
cd obs-studio
sudo mkdir build && cd build
sudo cmake -DUNIX_STRUCTURE=1 -DCMAKE_INSTALL_PREFIX=/usr ..
sudo make -j4
sudo make install
obs #only need to run this command from now on
exit 0

*** If you have a PI3B or 3B+ then you just need to type obs or run it from the start menu in the Sound and Video section.  If you have a PI 4 B+ then you must run if from the following command *** 

MESA_GL_VERSION_OVERRIDE=3.3 obs

This seems to work fine and you can incorporate it into a shell script


cd webcam

sudo nano obsStart.sh

MESA_GL_VERSION_OVERRIDE=3.3 obs

ctrl + x, y , enter to save

then

sudo chmod +x obsStart.sh 

to make it executable

and run it with

./obsStart.sh

and that is it!


I hope that you enjoy this article, it took a full day to write and test, then I had to do it all again for my partner. I will welcome your comments and will assist if I can.

Comments

My photo
Ralph Beardmore
Derbyshire, United Kingdom
I am full of life and rarely still. I have forsaken TV for YouTube and other channels. I love creating and empowering others to do the same. You CAN do it!