Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created June 24, 2017 18:37
Show Gist options
  • Save hlorand/0b4bf5fb12fb437b7e6d1cd47bcd07b0 to your computer and use it in GitHub Desktop.
Save hlorand/0b4bf5fb12fb437b7e6d1cd47bcd07b0 to your computer and use it in GitHub Desktop.
Video recording and streaming script for Raspberry Pi + Pi Camera
#!/bin/bash
#
# Video recording script for Raspberry Pi. Set bitrate and recording time below
#
# To autostart this script after login, edit /etc/xdg/lxsession/LXDE-pi/autostart and add the following line BEFORE "@xsreensaver":
#
# sh ./RaspberryPiVideoRecord.sh
#
# To convert the output .h264 file to mp4:
#
# sudo apt-get install gpac
# MP4Box -add video.h264 video.mp4
clear
echo "Video recording starts in 10 sec."
echo "Press Ctrl+C to stop."
sleep 10
cd /home/pi/
mkdir $(date +%Y-%m-%d-%H-%M)
cd $(date +%Y-%m-%d-%H-%M)
#####################################
#1 Mbps = 125 KB/s - 7.5 MB/m - 450 MB/h
#2 Mbps = 250 KB/s - 15 MB/m - 900 MB/h
#4 Mbps = 500 KB/s - 30 MB/m - 1.8 GB/h
#8 Mbps = 1 MB/s - 60 MB/m - 3.6 GB/h
#Youtube 1920x1080 = 8 Mbps
#Youtube 1280x720 = 5 Mbps
#Youtube 854x480 = 2.5 Mbps
#Youtube 640x360 = 1 Mbps
#Specify bitrate in bps, so you must multiply above bitrate with 1'000'000
#####################################
BITRATE=4000000
#####################################
#Frames per sec
#####################################
FPS=25
#####################################
#Recording time in ms (1sec=1000ms)
#####################################
RECTIME=7200000 # 2 hours
####################################
#Kill recording after this timeout (recommended: RECTIME+10sec)
####################################
TIMEOUT=7210000
####################################
#RECORD to "video.h264" file
####################################
timeout $TIMEOUT raspivid -b $BITRATE -o video.h264 -t $RECTIME -fps $FPS --nopreview --hflip --vflip --sharpness 0 --contrast 0 --brightness 50 --saturation 15 --ISO 400 --ev 2 --awb sun --exposure sports --metering matrix --drc med
####################################
#RECORD to file and CONVERT to mp4
#(uncomment to use)
####################################
#raspivid -b 4000000 -o video.h264 -t 10000 -fps 30 -f --hflip --vflip --sharpness 0 --contrast 0 --brightness 50 --saturation 10 --ISO 400 --ev 2 --awb auto --exposure auto --metering matrix --drc low; MP4Box -add video.h264 video.mp4
####################################
#STREAM to VLC
#(uncomment to use)
#Open this in VLC to watch:
# rtsp://raspberrypi.local:8554/
####################################
# sudo raspivid -b 4000000 -o - -t 0 -fps 30 -f --hflip --vflip --sharpness 0 --contrast 0 --brightness 50 --saturation 15 --ISO 400 --ev 2 --awb sun --exposure sports --metering matrix --drc med | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264
sudo shutdown -h +1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment