#!/bin/bash usage(){ cat << EOF $(basename $0) -d -s -p -v -s —————————————————————————————————————————————————————————————————————————————————— Opt | Variable | Type | Definition | Default value —————————————————————————————————————————————————————————————————————————————————— -d directory path Directory path of images to convert ./pics -r rate int Convert only 1/N images 1 -p prefix str Images filename prefix. '' -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="" rate="1" size='1024x768' video_dir="./videos" while getopts "hd:s:p:v:" o; do case "${o}" in h) usage ;; r) rate=${OPTARG} ;; d) directory=${OPTARG} ;; p) prefix=${OPTARG} ;; v) video_dir=$( realpath ${OPTARG} 2>/dev/null ) ;; \?) 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 prefix [ -n "$( ls ${directory}/${prefix}* 2>/dev/null)" ] || panic "${directory}/${prefix}\* doesn't match any file. Exiting." # Safe test rate [ ${rate} -gt 0 ] || panic "Invalid rate '${rate}'" pts=$( echo 1/$rate | bc -l ) # 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" } [ -n "$( ls ${directory}/${prefix}* 2>/dev/null)" ] || panic "${directory}/${prefix}\* doesn't match any file. Exiting." # Set video file name date=$(date +%s) video_file="${video_dir}$date.mp4" echo -e "\nSaving to video file $video_file.\n" ffmpeg -hide_banner -loglevel quiet -stats -pattern_type glob -i "pics/*.jpg" -filter:v "setpts=(${pts})*PTS" -pix_fmt yuv420p -s "${size}" -vcodec libx264 "${video_file}"