error function and for loop fix

This commit is contained in:
2020-11-25 15:04:51 -05:00
parent 07596331d2
commit 8a8cf03861

View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# Help function ### Help function ###
Help() Help()
{ {
@@ -14,14 +14,36 @@ Help()
echo "Learn more about FFmpeg's quality settings: https://trac.ffmpeg.org/wiki/Encode/H.264" echo "Learn more about FFmpeg's quality settings: https://trac.ffmpeg.org/wiki/Encode/H.264"
} }
### Error function ###
PROGNAME="$(basename $0)"
error_exit()
{
# ----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# ----------------------------------------------------------------
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
# Example call of the error_exit function. Note the inclusion
# of the LINENO environment variable. It contains the current
# line number.
### Script ### ### Script ###
DIRECTORY=$1 DIRECTORY=$1
QUALITY_HD=22 QUALITY_HD=23
QUALITY_4K=24 QUALITY_4K=22
# Basic error handling # Check if source directory is provided
if [ -z "$1" ]; then if [ -z "$1" ] || [ ! -d "$1" ]; then
printf "ERROR: You must specify a source directory\n\n" 1>&2 printf "ERROR: You must specify a source directory\n\n" 1>&2
Help Help
exit 1 exit 1
@@ -32,17 +54,16 @@ if [ ! -d "$DIRECTORY/output" ]; then
mkdir "$DIRECTORY/output" mkdir "$DIRECTORY/output"
fi fi
# Encode each file in the directory with different CRF setting based on resolution # Encode each file in the directory with different CRF setting based on resolution
for FILE in "$DIRECTORY"/*; do for FILE in "$DIRECTORY"/*.*; do
RES=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$FILE") RES=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$FILE")
FILENAME=$(basename "$FILE") FILENAME=$(basename "$FILE")
if [[ $RES -gt 1920 ]]; then if [[ $RES -gt 1920 ]]; then
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_4K" -c:a copy "$DIRECTORY"/output/"$FILENAME" ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_4K" -c:a copy "$DIRECTORY"/output/"$FILENAME"
elif [[ $RES -le 1920 ]]; then elif [[ $RES -le 1920 ]]; then
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_HD" -c:a copy "$DIRECTORY"/output/"$FILENAME" ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_HD" -c:a copy "$DIRECTORY"/output/"$FILENAME"
else
echo "$FILENAME is not a valid filetype"
fi fi
done done || error_exit "$LINENO: An error has occurred."
exit 0 exit 0