[fix] add bitrate and switch to concat

This commit is contained in:
alban 2019-10-08 17:26:07 +02:00
parent 813be866ea
commit ce402fa9b2
1 changed files with 35 additions and 18 deletions

View File

@ -2,15 +2,16 @@
usage(){
cat << EOF
$(basename $0) -d <directory> -s <rate> -p <prefix> -v <video_dir> -s <size>
$(basename $0) -d <directory> -s <accelerate> -p <prefix> -v <video_dir> -s <size>
——————————————————————————————————————————————————————————————————————————————————
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
-a accelerate int Convert only 1/N images 1
-p prefix str Images filename prefix. ''
-v video_dir path Directory path to store video file. ./videos
-b bitrate str Video bitrate 1500k
-s size str Images geometry. '1024x768'
——————————————————————————————————————————————————————————————————————————————————
@ -21,26 +22,33 @@ panic(){ echo $@; exit 2;}
directory="./pics"
prefix=""
rate="1"
accelerate="1"
size='1024x768'
video_dir="./videos"
bitrate="1500k"
while getopts "hd:s:p:v:" o; do
while getopts "a:b:d:hs:p:r:v:" o; do
case "${o}" in
h)
usage
;;
r)
rate=${OPTARG}
a)
accelerate=${OPTARG}
;;
d)
directory=${OPTARG}
;;
h)
usage
;;
p)
prefix=${OPTARG}
;;
b)
bitrate=${OPTARG}
;;
s)
size=${OPTARG}
;;
v)
video_dir=$( realpath ${OPTARG} 2>/dev/null )
video_dir=${OPTARG}
;;
\?)
panic "Unknown option at position $OPTIND. Exiting."
@ -55,12 +63,21 @@ which ffmpeg &>/dev/null || panic "No ffmpeg found. Exiting."
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 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 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 )/"
@ -71,11 +88,11 @@ video_dir="$( realpath ${video_dir} 2>/dev/null )/"
[ "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"
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}"
ffmpeg -hide_banner -loglevel quiet -stats -f concat -safe 0 -i "${tmpfile}" -pix_fmt yuv420p -s "${size}" -vcodec libx264 -b ${bitrate} "${video_file}"
rm -f $tmpfile