#!/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= roi_file= motion_threshold=1 ffmpeg_args="-nostats -loglevel error" dvr_scan_args="-q" verbose= config_dir=$HOME/.config/video-util config_dir_set= 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() { >&2 echo "error: $@" 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 <<-_EOF usage: $PROGNAME OPTIONS command Options: -i|--input input file/directory -o|--output output file/directory --roi-file path to file with ROI sets -mt motion threshold -v, -vv, vx be verbose --config-dir config directory Commands: fix, mass-fix fix video timestamps mass-fix-mtime fix mtimes of recordings motion detect motion snapshot take video snapshot _EOF exit 1 } check_input_file() { [ -z "$input" ] && die "input file not specified" [ -f "$input" ] || die "input file '$input' doesn't exist" } check_input_dir() { [ -z "$input" ] && die "input directory not specified" [ -d "$input" ] || die "input directory '$input' doesn't exist" } fix_video_timestamps() { local input="$1" local dir=$(dirname "$input") local temp="$dir/.temporary_fixing.mp4" local mtime=$(get_mtime "$input") # debug "fix_video_timestamps: ffmpeg $ffmpeg_args -i \"$input\" -y -c copy \"$temp\"" ffmpeg $ffmpeg_args -i "$input" -y -c copy "$temp"