timelapse_ipwebcam/makevideo.sh

105 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
usage(){
cat << EOF
$(basename $0) -a <accelerate> -b bitrate -d <directory> -p <prefix> -q -v <video_dir> -s <size>
——————————————————————————————————————————————————————————————————————————————————
Opt | Variable | Type | Definition | Default value
——————————————————————————————————————————————————————————————————————————————————
-a accelerate int Convert only 1/N images 1
-b bitrate str Video bitrate 1500k
-d directory path Directory path of images to convert ./pics
-p prefix str Images filename prefix. ''
-q quiet int Minimal output 0
-v video_dir path Directory path to store video file. ./videos
-s size str Images geometry. '1024x768'
——————————————————————————————————————————————————————————————————————————————————
EOF
exit
}
panic(){ echo $@; exit 2;}
directory="./pics"
prefix=""
accelerate="1"
size='1024x768'
video_dir="./videos"
bitrate="1500k"
quiet=0
while getopts "a:b:d:hs:p:qr:v:" o; do
case "${o}" in
a)
accelerate=${OPTARG}
;;
b)
bitrate=${OPTARG}
;;
d)
directory=${OPTARG}
;;
h)
usage
;;
p)
prefix=${OPTARG}
;;
q)
quiet=1
;;
s)
size=${OPTARG}
;;
v)
video_dir=${OPTARG}
;;
\?)
panic "Unknown option at position $OPTIND. Exiting."
;;
esac
done
# Safe test ffmpeg
which ffmpeg &>/dev/null || panic "No ffmpeg found. Exiting."
# Safe test directory
directory="$( realpath ${directory} 2>/dev/null )/"
[ -n "${directory}" ] && [ -d "${directory}" ] || panic "'${directory}' is not a valid directory. Exiting."
# Safe test accelerate
[ ${accelerate} -gt 0 ] || panic "Invalid rate '${rate}'"
# Safe test prefix
let c=0
tmpfile=$( mktemp )
for file in ${directory}${prefix}* ; do
let $(( c++ ))
[[ $(( $c % ${accelerate} )) -eq 0 ]] || [[ $rate -eq 1 ]] && echo "file '$file'" >> $tmpfile
done
[ 0 -eq $c ] && panic "No file found"
# Safe test video_dir
video_dir="$( realpath ${video_dir} 2>/dev/null )/"
[ -n "${video_dir}" ] || panic "The video_dir option cannot be empty. Exiting."
[ -d "${video_dir}" ] || {
read -p "'${video_dir}' does not exist, do you want to create it? [Y/n] : "
REPLY=${REPLY:-Y}
[ "N" == ${REPLY^^} ] && panic "The video directory must exist. Exiting."
mkdir -p ${video_dir} 2>/dev/null || panic "Failed to create '${video_dir}'. Exiting"
}
# Set video file name
date=$(date +%s)
video_file="${video_dir}$date.mp4"
echo -e "\nSaving to video file $video_file\n"
[ 0 -eq $quiet ] || quiet_options=" -hide_banner -loglevel quiet -stats "
ffmpeg $quiet_options -f concat -safe 0 -i "${tmpfile}" -pix_fmt yuv420p -s "${size}" -vcodec libx264 -b ${bitrate} "${video_file}"
rm -f $tmpfile