capture_image.sh (1882B)
1 #!/bin/sh 2 set -e 3 4 version=0.1.0 5 6 device=/dev/video0 7 8 show_help() { 9 echo "usage: ${0##*/} [OPTIONS] [FILE]" 10 echo "captures an image from a video device and timestamps it." 11 echo "The output is saved to FILE." 12 echo "If FILE is not specified, the image is saved as:" 13 echo " DEVICE_YYYY-MM-DD_HH:MM:SS.jpg" 14 echo 15 echo "The FILE name is written as stdout." 16 echo 17 echo "OPTIONS are one or more of the following:" 18 echo " -h, --help show this message" 19 echo " -v, --version show version and license information" 20 echo " -V, --verbose show verbose information" 21 echo " -d, --device <device> specify video device (default $device)" 22 echo " -- do not consider any following args as options" 23 } 24 25 show_version() { 26 echo "${0##*/} version $version" 27 echo "Licensed under the ISC License" 28 echo "written by Anders Damsgaard, anders@adamsgaard.dk" 29 echo "https://src.adamsgaard.dk/dotfiles" 30 } 31 32 die() { 33 printf '%s\n' "$1" >&2 34 exit 1 35 } 36 37 capture() { 38 loglevel="level+error" 39 if [ "$verbose" = 1 ]; then 40 loglevel="level+info" 41 echo "$1" 42 fi 43 NO_COLOR=1 ffmpeg -f video4linux2 -i "$device" -vframes 1 \ 44 -loglevel "$loglevel" -y "$1" </dev/null 45 } 46 47 post_process() { 48 convert "$1" label:"$(date +'%Y-%m-%d %H:%M:%S %Z')" \ 49 -gravity east -append "$1" 50 } 51 52 verbose=0 53 while :; do 54 case "$1" in 55 -h|-\?|--help) 56 show_help 57 exit 0 58 ;; 59 -v|--version) 60 show_version 61 exit 0 62 ;; 63 -V|--verbose) 64 verbose=1 65 ;; 66 -d|--device) 67 device="$2" 68 shift 69 ;; 70 --) # end all options 71 shift 72 break 73 ;; 74 -?*) 75 die 'Error: Unknown option specified' 76 ;; 77 *) # No more options 78 break 79 esac 80 shift 81 done 82 83 output="${1:-${device##*/}_$(date +'%Y-%m-%d_%H:%M:%S').jpg}" 84 if [ $# -gt 1 ]; then 85 die 'error: invalid arguments' 86 elif [ $# -gt 0 ]; then 87 output="$1" 88 fi 89 90 capture "$output" 91 post_process "$output" 92 echo $output