Skip to content

Instantly share code, notes, and snippets.

@pawelszydlo
Created July 8, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelszydlo/ee6d317b2d6f37e32a93f97a3d5615d0 to your computer and use it in GitHub Desktop.
Save pawelszydlo/ee6d317b2d6f37e32a93f97a3d5615d0 to your computer and use it in GitHub Desktop.
Script for simultaneous streaming and saving output from Raspberry Pi camera.
#!/bin/bash
# Script for simultaneous streaming and saving output from Raspberry Pi camera.
# newest version at: https://gist.github.com/pawelszydlo/ee6d317b2d6f37e32a93f97a3d5615d0
date=`date +%Y-%m-%d_%H-%M-%S`i
recordings_dir="/home/pi/recordings"
stream_dir="$recordings_dir/$date"
min_disk_space=200
monitor_space(){
while true; do
left=$(df -m . | tail -1 | awk '{print $4}')
echo "Disk space left: $left MB"
if [ "$left" -lt "$min_disk_space" ]; then
# Find and delete the oldest file.
oldest_file=$(find $recordings_dir -type f -printf "%T@ %p\n" | sort -nr | tail -1 | cut -d' ' -f 2)
echo "Deleting oldest file: $oldest_file"
rm "$oldest_file"
# remove empty directories.
find $recordings_dir -type d -empty -delete
fi
sleep 60
done
}
echo "Creating stream dir $stream_dir..."
mkdir -p "$stream_dir"
cd "$stream_dir"
echo "Starting free space monitoring..."
monitor_space &
monitor_pid=$!
echo "Starting streaming..."
raspivid -o - -t 0 -hf -w 1920 -h 1080 -fps 30 | \
tee >(split -d --suffix=3 --additional-suffix='.h264' --bytes=50m -) | \
cvlc stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=0.0.0.0:1234}' :demux=h264
echo "Stopping disk space monitor..."
kill $monitor_pid > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment