ffmpeg-encode-jellyfin/ffmpeg.sh

113 lines
2.8 KiB
Bash
Raw Permalink Normal View History

2024-01-09 18:44:28 +00:00
#!/bin/bash
UPDATED="2024-01-09"
header () {
clear
echo "FFMpeg Encoder (HEVC/AAC) [$UPDATED]"
echo "--------------------------------------------------"
}
header
# Grab input parameters
FLDR=$1 # working folder
SRC=$2 # input file
DST=$3 # output file
LOGFILE=encode.log # log file
# Check if specified folder is present
if [ ! -d "$FLDR" ]; then
echo "Path [$FLDR] don't exist"
exit 1
fi
cd $FLDR/
# Check if source file is present
if [ ! -f "$SRC" ]; then
echo "File [$SRC] don't exist."
exit 1
fi
# Define audio/video codecs
CODEC_AUDIO="libfdk_aac"
CODEC_VIDEO="hevc_nvenc"
# Define audio/video bitrates
BITRATE_AUDIO="448k"
BITRATE_VIDEO="2600k"
# Define input/output files
INPUT="-i ${SRC}"
OUTPUT="${DST}.mp4"
# Define ffmpeg encoding options
DEFAULTS="-y -hide_banner -loglevel error -stats"
HWACCELL="-hwaccel_device 0 -hwaccel cuda -hwaccel_output_format cuda"
SUBTITLES="-c:s copy"
AUDIO="-c:a ${CODEC_AUDIO} -b:a ${BITRATE_AUDIO}"
VIDEO="-c:v ${CODEC_VIDEO} -b:v ${BITRATE_VIDEO}"
RENDER_AUDIO="-tune hq -vbr 5"
RENDER_VIDEO="-preset p6"
# Print pass informations
pass() {
PASS=$1
header
echo ">>> Pass $PASS..."
echo "--------------------------------------------------"
}
# Abort with message on failed ffmpeg encoding
isFailed() {
PASS=$1
ret_code=$?
if [ $ret_code != 0 ]; then
if [ -z $2 ]; then
echo "[ Pass $PASS: Failed! ]"
else
echo "[ Something was wrong! ]"
fi
exit 1
else
echo "[ Done! ]"
fi
}
# Do encode processing (2 passes)
encode() {
pass 1
#ffmpeg -y \
# -hide_banner \
# -loglevel error -stats \
# -hwaccel_device 0 -hwaccel cuda -hwaccel_output_format cuda \
# -i ${INPUT} \
# -c:v hevc_nvenc -preset medium -b:v 2600k \
# -c:a libfdk_aac -b:a 448k \
# -pass 1 \
# -an -f null /dev/null
ffmpeg ${DEFAULTS} ${HWACCELL} ${INPUT} ${SUBTITLES} ${AUDIO} ${VIDEO} -pass 1 -an -f null /dev/null
isFailed 1
pass 2
#ffmpeg -y \
# -hide_banner \
# -loglevel error -stats \
# -hwaccel_device 0 -hwaccel cuda -hwaccel_output_format cuda \
# -i ${INPUT} \
# -c:s copy \
# -c:v hevc_nvenc -b:v 2600k \
# -preset p6 \
# -c:a libfdk_aac -b:a 448k \
# -tune hq -vbr 5 \
# -movflags +faststart -profile:v main10 \
# -pass 2 \
# ${OUTPUT}.mp4
ffmpeg ${DEFAULTS} ${HWACCELL} ${INPUT} ${SUBTITLES} ${AUDIO} ${RENDER_AUDIO} ${VIDEO} ${RENDER_VIDEO} -pass 2 ${OUTPUT}
isFailed 2
}
# Process encoding and check result code level
encode > >(tee $LOGFILE) 2>&1
isFailed 3
exit 0