commit 194cc02948e69206ddab6bd4a3727ac953ce0301
parent ca0c22d9acb41ac1a88b544fc19b355516b6b23c
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Sat, 28 Dec 2019 11:38:32 +0100
Add scripts for capturing images from video device and creating html gallery
Diffstat:
2 files changed, 197 insertions(+), 0 deletions(-)
diff --git a/.local/bin/capture_image.sh b/.local/bin/capture_image.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+set -e
+
+version=0.1.0
+
+device=/dev/video0
+
+show_help() {
+ echo "usage: ${0##*/} [OPTIONS] [FILE]"
+ echo "captures an image from a video device and timestamps it."
+ echo "The output is saved to FILE."
+ echo "If FILE is not specified, the image is saved as:"
+ echo " DEVICE_YYYY-MM-DD_HH:MM:SS.jpg"
+ echo
+ echo "FILE is written as stdout."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show verbose information"
+ echo " -d, --device <device> specify video device"
+ echo " -- do not consider any following args as options"
+}
+
+show_version() {
+ echo "${0##*/} version $version"
+ echo "Licensed under the ISC License"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://src.adamsgaard.dk/dotfiles"
+}
+
+die() {
+ printf '%s\n' "$1" >&2
+ exit 1
+}
+
+capture() {
+ loglevel="level+error"
+ if [ "$verbose" = 1 ]; then
+ loglevel="level+info"
+ echo "$1"
+ fi
+ NO_COLOR=1 ffmpeg -f video4linux2 -i "$device" -vframes 1 \
+ -loglevel "$loglevel" -y "$1"
+}
+
+post_process() {
+ convert "$1" label:"$(date +'%Y-%m-%d %H:%M:%S %Z')" \
+ -gravity east -append "$1"
+}
+
+verbose=0
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -d|--device)
+ device="$2"
+ shift
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'Error: Unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+output="${1:-${device##*/}_$(date +'%Y-%m-%d_%H:%M:%S').jpg}"
+if [ $# -gt 1 ]; then
+ die 'error: invalid arguments'
+elif [ $# -gt 0 ]; then
+ output="$1"
+fi
+
+capture "$output"
+post_process "$output"
+echo $output
diff --git a/.local/bin/html_gallery.sh b/.local/bin/html_gallery.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+set -e
+
+version=0.1.0
+
+output="${1:-${device##*/}.html}"
+
+show_help() {
+ echo "usage: ${0##*/} [OPTIONS] [FILE ...]"
+ echo "outputs a html page with all FILE[S] as images."
+ echo "If no FILE[S] are provided, ${0##*/} expects file paths as stdin."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show verbose information"
+ echo " -t, --title <value> set page title"
+ echo " -s, --stylesheet <path> embed custom css stylesheet"
+ echo " -- do not consider any following args as options"
+}
+
+show_version() {
+ echo "${0##*/} version $version"
+ echo "Licensed under the ISC License"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://src.adamsgaard.dk/dotfiles"
+}
+
+die() {
+ printf '%s\n' "$1" >&2
+ exit 1
+}
+
+generate_html_header() {
+ printf "<!DOCTYPE html>\n"
+ printf "<html lang=\"en\">\n"
+ printf "<meta charset=\"utf-8\">\n"
+ printf "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
+ if [ -n "$title" ]; then
+ printf "<title>%s</title>\n" "$title"
+ fi
+ if [ -n "$stylesheet" ]; then
+ printf "<style>\n%s</style>\n" "$(cat "$stylesheet")"
+ fi
+ printf "<article>\n"
+ if [ -n "$title" ]; then
+ printf " <header>\n"
+ printf " <h1>%s</h1>\n" "$title"
+ printf " </header>\n\n"
+ fi
+}
+
+generate_html_footer() {
+ printf "</article>\n"
+ printf "</html>\n"
+}
+
+generate_html() {
+ generate_html_header
+ for img in "$@"; do
+ printf " <img src=\"%s\" alt=\"%s\"/><br>\n" "$img" "$img"
+ done
+ generate_html_footer
+}
+
+verbose=0
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -t|--title)
+ title="$2"
+ shift
+ ;;
+ -s|--stylesheet)
+ stylesheet="$2"
+ shift
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'Error: Unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ $# -lt 1 ]; then
+ generate_html "$(cat)"
+else
+ generate_html "$@"
+fi