added file input option, broke into functions

This commit is contained in:
2020-11-25 16:31:14 -05:00
parent 8a8cf03861
commit 2217c1d773

View File

@@ -1,10 +1,20 @@
#!/bin/bash #!/bin/bash
### Help function ### ### VARIABLES ###
PROGNAME="$(basename "$0")"
INPUT_SOURCE=$1
QUALITY_HD=23
QUALITY_4K=22
FILEDIR=$(dirname "$INPUT_SOURCE")
### FUNCTIONS###
### Function: Help
Help() Help()
{ {
# Display Help
echo "This script uses sensible ffmpeg options to batch encode MKV files in a directory to compressed H264 MKVs." echo "This script uses sensible ffmpeg options to batch encode MKV files in a directory to compressed H264 MKVs."
echo "You can change the CRF parameters in the script, defaults are 24 for HD and 22 for 4K." echo "You can change the CRF parameters in the script, defaults are 24 for HD and 22 for 4K."
echo echo
@@ -14,56 +24,69 @@ 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 ### ### Funtion: Error
PROGNAME="$(basename $0)"
error_exit() 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 echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1 exit 1
} }
# Example call of the error_exit function. Note the inclusion
# of the LINENO environment variable. It contains the current
# line number.
### Script ### # Function: encode each file in the directory with different CRF setting based on resolution
DIRECTORY=$1 folder_encode () {
QUALITY_HD=23 if [ ! -d "$INPUT_SOURCE/output" ]; then
QUALITY_4K=22 mkdir "$INPUT_SOURCE/output"
fi
# Check if source directory is provided for FILE in "$INPUT_SOURCE"/*.*; do
if [ -z "$1" ] || [ ! -d "$1" ]; then RES=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$FILE")
FILENAME=$(basename "$FILE")
if [[ $RES -gt 1920 ]]; then
echo "File is 4K or higher, encoding using CRF $QUALITY_4K"
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_4K" -c:a copy "$INPUT_SOURCE"/output/"$FILENAME"
elif [[ $RES -le 1920 ]]; then
echo "File is HD or lower, encoding using CRF $QUALITY_HD"
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_HD" -c:a copy "$INPUT_SOURCE"/output/"$FILENAME"
fi
done
}
# Function: encode single file with different CRF setting based on resolution
file_encode () {
if [ ! -d "$FILEDIR/output" ]; then
mkdir "$FILEDIR/output"
fi
FILENAME=$(basename "$INPUT_SOURCE")
RES=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$INPUT_SOURCE")
if [[ $RES -gt 1920 ]]; then
ffmpeg -i "$INPUT_SOURCE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_4K" -c:a copy "$FILEDIR"/output/"$FILENAME"
elif [[ $RES -le 1920 ]]; then
ffmpeg -i "$INPUT_SOURCE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_HD" -c:a copy "$FILEDIR"/output/"$FILENAME"
fi
}
### SCRIPT ###
# Check if source input is provided
if [ -z "$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
fi fi
# Create output folder within source directory # Run function based on file or folder input
if [ ! -d "$DIRECTORY/output" ]; then if [ -f "$1" ]; then
mkdir "$DIRECTORY/output" file_encode || error_exit "$LINENO: An error has occurred." 1>&2
elif [ -d "$1" ]; then
folder_encode || error_exit "$LINENO: An error has occurred." 1>&2
else
error_exit "$LINENO: Not a valid source" 1>&2
fi fi
exit 0
# Encode each file in the directory with different CRF setting based on resolution
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")
FILENAME=$(basename "$FILE")
if [[ $RES -gt 1920 ]]; then
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_4K" -c:a copy "$DIRECTORY"/output/"$FILENAME"
elif [[ $RES -le 1920 ]]; then
ffmpeg -i "$FILE" -c:v libx264 -preset slow -tune film -crf "$QUALITY_HD" -c:a copy "$DIRECTORY"/output/"$FILENAME"
fi
done || error_exit "$LINENO: An error has occurred."
exit 0