#!/bin/bash set -e DIR="$( cd "$( dirname "$(realpath "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )" PROGNAME="$0" BOLD=$(tput bold) RST=$(tput sgr0) RED=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) CYAN=$(tput setaf 6) input= output= command= motion_threshold=1 ffmpeg_args="-nostats -loglevel error" dvr_scan_args="-q" verbose= config_dir=$HOME/.config/video-util config_dir_set= write_data_prefix= write_data_time= _time_started= time_start() { _time_started=$(date +%s) } time_elapsed() { local _time_finished=$(date +%s) echo $(( _time_finished - _time_started )) } debug() { if [ -n "$verbose" ]; then >&2 echo "$@" fi } echoinfo() { >&2 echo "${CYAN}$@${RST}" } echoerr() { >&2 echo "${RED}${BOLD}error:${RST}${RED} $@${RST}" } echowarn() { >&2 echo "${YELLOW}${BOLD}warning:${RST}${YELLOW} $@${RST}" } die() { echoerr "$@" exit 1 } file_in_use() { [ -n "$(lsof "$1")" ] } get_mtime() { stat -c %Y "$1" } # converts date in format yyyy-mm-dd-hh.ii.ss to unixtime date_to_unixtime() { local date="$1" date=${date//./-} date=${date//-/ } local nums=($date) local y=${nums[0]} local m=${nums[1]} local d=${nums[2]} local h=${nums[3]} local i=${nums[4]} local s=${nums[5]} date --date="$y-$m-$d $h:$i:$s" +"%s" } filename_as_unixtime() { local name="$1" name="$(basename "$name")" name="${name/record_/}" name="${name/.mp4/}" date_to_unixtime "$name" } config_get_prev_mtime() { local prefix="$1" [ -z "$prefix" ] && die "config_get_prev_mtime: no prefix" local file="$config_dir/${prefix}_mtime" if [ -f "$file" ]; then debug "config_get_prev_mtime: $(cat "$file")" cat "$file" else debug "config_get_prev_mtime: 0" echo "0" fi } config_set_prev_mtime() { local prefix="$1" [ -z "$prefix" ] && die "config_set_prev_mtime: no prefix" local mtime="$2" [ -z "$mtime" ] && die "config_set_prev_mtime: no mtime" local file="$config_dir/${prefix}_mtime" debug "config_set_prev_mtime: writing '$mtime' to '$file'" echo "$mtime" > "$file" } usage() { cat <&2 echo "using ${BOLD}$config_dir${RST} as config directory" fi [ -z "$command" ] && die "command not specified" case "$command" in fix) check_input_file fix_video_timestamps "$input" echo "done" ;; mass-fix) check_input_dir do_mass_fix ;; mass-fix-mtime) check_input_dir do_mass_fix_mtime ;; motion) check_input_file do_motion "$input" ;; mass-motion) check_input_dir do_mass_motion "$input" ;; snapshot) check_input_file [ -z "$output" ] && { echowarn "--output not specified, using snapshot.jpg as default" output="snapshot.jpg" } ffmpeg $ffmpeg_args -i "$input" -frames:v 1 -q:v 2 "$output"