Skip to content

Instantly share code, notes, and snippets.

@aphex3k
Last active October 6, 2021 17:00
Show Gist options
  • Save aphex3k/502adb248dc388302f1df20746aae114 to your computer and use it in GitHub Desktop.
Save aphex3k/502adb248dc388302f1df20746aae114 to your computer and use it in GitHub Desktop.
Plex Server postprocessing script to convert MPEG2-TS to 10-bit HEVC mp4
#!/bin/bash
#
# Plex DVR Postprocessing
renice -n 19 -p $$
ionice -n 3 -p $$
# https://forums.plex.tv/t/plexpostproc-script-error-1-failed-to-convert-using-ffmepg/316669/7
LD_LIBRARY_PATH=/home/micha/ffmpeg_build/lib:/home/micha/ffmpeg_build/lib:/usr/lib/x86_64-linux-gnu
export LD_LIBRARY_PATH
inFile=${1}
outFile=$(dirname "${inFile}")/$(basename "${inFile}" .ts).mp4
ffmpeg="/usr/local/bin/ffmpeg -hide_banner -y -analyzeduration 64M -probesize 64M -fflags +genpts "
echo "process id: ${id}" >> /var/log/plex_pp.log
echo "start date: $(date)" >> /var/log/plex_pp.log
echo "file: $@" >> /var/log/plex_pp.log
if [ ! -f "${inFile}" ]
then
echo No input file specified | tee -a /var/log/plex_pp.log
exit 0
fi
if [ ! -s "${inFile}" ]
then
echo input file had 0 size | tee -a /var/log/plex_pp.log
rm -f "${inFile}"
exit 0
fi
# Audio Options
ca="libfdk_aac"
# -max_muxing_queue_size 400 https://trac.ffmpeg.org/ticket/6375
af="-max_muxing_queue_size 1024 -ac:a 2 -ar 48000 -profile:a aac_he_v2 -vbr:a 3 -vbr:a:0 5 -profile:a:0 aac_low" # <- AAC Stereo for main audio channel 0
#af="-max_muxing_queue_size 1024 -ac:a 2 -ar 48000 -profile:a aac_he_v2 -vbr:a 3 -c:a:0 copy " # <- AC3 5.1 Audio for main audio channel 0
# Mapping
# See: https://trac.ffmpeg.org/wiki/Map
map="-map 0 -ignore_unknown"
height=$(/usr/local/bin/ffprobe -analyzeduration 64M -probesize 64M -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "${inFile}" | tr -d "\n")
#height=$((${sheight}))
cq="29"
if [ $height -eq 720 ] || [ $height -eq 720720 ]
then
cq=$(($cq + 0))
npp="w=1280:h=720:"
else
echo "height ${height} is not equal to 720" | tee -a /var/log/plex_pp.log
fi
if [ $height -eq 1080 ] || [ $height -eq 10801080 ]
then
cq=$(($cq + 3))
npp="w=1920:h=1080:"
else
echo "height ${height} is not equal to 1080" | tee -a /var/log/plex_pp.log
fi
frate=$(/usr/local/bin/ffprobe -analyzeduration 64M -probesize 64M -v 0 -of csv=p=0 -select_streams 0 -show_entries stream=r_frame_rate "${inFile}" | head -n 1)
fps=$((${frate}))
if [ $fps -gt 48 ] && [ $fps -lt 121 ]
then
rate="-r "$(awk "BEGIN {print (${fps})/2}")
echo adjusting output rate from $fps to ${rate} fps | tee -a /var/log/plex_pp.log
fi
echo "outpt file: ${outFile}" | tee -a /var/log/plex_pp.log
moov="-movflags +faststart"
frags="-frag_duration 3000000 -min_frag_duration 1000000"
#See: https://devblogs.nvidia.com/nvidia-ffmpeg-transcoding-guide/
${ffmpeg} -nostdin -vsync 0 -hwaccel cuvid -hwaccel_output_format cuda -c:v mpeg2_cuvid -i "${inFile}" ${map} -c:v hevc_nvenc -vf yadif_cuda=0:-1:1,scale_npp=${npp}format=yuv420p,hwdownload,format=yuv420p -pix_fmt p010le -profile:v main10 -tag:v hvc1 -bsf:v hevc_mp4toannexb -c:a ${ca} ${af} -preset:v p7 -2pass 1 -tune hq -spatial_aq 1 -rc-lookahead 24 -rc vbr -b:v 0 -maxrate 2800K -bufsize 1M -cq ${cq} -qmin ${cq} -qmax $((${cq} + 1)) ${moov} ${frags} "${outFile}" </dev/null 2> /dev/null
echo "cleanup..."
if [ $? -eq 0 -a -s "${outFile}" ]
then
echo Removing input file | tee -a /var/log/plex_pp.log
rm -f "${inFile}" 2>> /var/log/plex_pp.log
rm -f "${ffreportfile}" # removing the ffmpeg report file
chmod 664 "${outFile}"
chgrp _nzb "${outFile}" || echo "failed chgrp"
else
rm -f "${outFile}" 2>> /var/log/plex_pp.log || echo "..."
tail /var/log/plex_pp.log
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment