Skip to content

Instantly share code, notes, and snippets.

@ChatchaiJ
Last active July 3, 2022 18:37
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 ChatchaiJ/174b654511a79d61e02aac57a6ff94b7 to your computer and use it in GitHub Desktop.
Save ChatchaiJ/174b654511a79d61e02aac57a6ff94b7 to your computer and use it in GitHub Desktop.
Record video from webcam using ffmpeg
#!/bin/sh
LEN=10 # video length in seconds
SLEEP=3 # sleep time in seconds
SAVEDIR="/tmp/camera"
CAMERA=$1
# How to use thi script
[ -z "$CAMERA" ] && echo "Usage: $0 /dev/videoX ## where X is [0..n]" && exit
# Check device name
CHK=$(echo $CAMERA | grep '^/dev/video')
[ -z "$CHK" ] && \
echo "Hm? camera device should be in /dev/videoX, are you sure that '$CAMERA' is correct?" && \
exit
# Get only the number part of device
CNO=$(echo $CAMERA | sed -e 's|/dev/video||')
[ ! -d "${SAVEDIR}" ] && mkdir -p ${SAVEDIR}
[ ! -d "${SAVEDIR}" ] && echo "Can't create '${SAVEDIR}' directory" && exit
while true; do
find ${SAVEDIR} -type f -name \*.mp4 -mtime +3 -exec rm -vf {} \;
D=$(date +%Y%m%d%H%M%S)
FILE="C${CNO}${D}.mp4"
printf "Recording ${SAVEDIR}/${FILE} ... "
ffmpeg -i ${CAMERA} -v 0 -t ${LEN} ${SAVEDIR}/${FILE}
printf "Done!\n"
printf "Sleep for $SLEEP secs\r"
sleep $SLEEP
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment