Skip to content

Instantly share code, notes, and snippets.

@radermacher
Forked from stenuto/timelapse.sh
Last active November 3, 2024 19:27
Show Gist options
  • Save radermacher/c62c88d872a02302b1ed52a657ac30b8 to your computer and use it in GitHub Desktop.
Save radermacher/c62c88d872a02302b1ed52a657ac30b8 to your computer and use it in GitHub Desktop.
Timelapse script; Usage: `timelapse.sh -d 1 -i 3` to record display 1 (main display) with an interval of 3 seconds OR use `timelapse.sh` and manually select display. An interval of 3 is default.
#!/bin/bash
# Check if ffmpeg is installed
if ! command -v ffmpeg &> /dev/null; then
echo "Error: ffmpeg is not installed or not in the PATH." >&2
echo "Please install ffmpeg to use this script." >&2
exit 1
fi
interval=3
display=""
counter=1
datetimestamp=$(date +"%Y%m%d-%Hh%Mm%Ss")
folder=~/Movies/Timelapse/${datetimestamp}
timelapseFilePath="${folder}/timelapse-${datetimestamp}.mp4"
mkdir -p ${folder}
# Function to list displays
list_displays() {
system_profiler -json SPDisplaysDataType |
sed -n '/spdisplays_ndrvs/,/]/p' |
grep -o '"_name" : "[^"]*"' |
sed 's/"_name" : "//;s/"$//' |
awk '{print NR ") " $0}' > /dev/tty
}
# Function to select a display
select_display() {
echo "Available displays for recording:" >&2
list_displays >&2
echo "" >&2
read -p "Select a display (enter number): " choice >&2
echo "$choice"
}
# Parse command-line options
while getopts ":d:i:" opt; do
case $opt in
d) display="$OPTARG" ;;
i) interval="$OPTARG" ;;
\?) echo "Invalid option -$OPTARG" >&2; exit 1 ;;
:) echo "Option -$OPTARG requires an argument" >&2; exit 1 ;;
esac
done
# If no display is set, use interactive selection
if [ -z "$display" ]; then
display=$(select_display)
fi
generate_video() {
echo ""
echo "Generating timelapse video..."
ffmpeg -loglevel error -framerate 30 -pattern_type glob -i "${folder}/*.png" -vf scale=1920:1080 -c:v libx264 -pix_fmt yuv420p ${timelapseFilePath}
open ${timelapseFilePath}
}
# Set up the trap to run the generate_video script on exit
trap 'echo ""; generate_video; echo ""; echo "Done."' EXIT
echo ""
echo "Recording goes to ${folder}"
echo "Using display: ${display}"
echo ""
echo "To end recording press ctrl+c"
while true; do
# Take a fullscreen screenshot and save it to $folder with a sequential name
screencapture -D${display} -x ${folder}/${counter}.png
# Increment the counter
((counter++))
# Wait for the specified interval
sleep "$interval"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment