yet another transmission post process script

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
underrepresented
Posts: 15
Joined: 15 Nov 2015, 21:38

yet another transmission post process script

Post by underrepresented »

I'm sure I could have added all of filebot parameters to this file. just haven't sussed it out yet. same could be said for handbrake and or ffmpeg parameters. having an external file for parameters is always good once you test out what you want the script to do. its easier to edit your parameters file than it is re-editing your shell script once it works the way you want it to. unfortunately I haven't quite figured out; yet, how to format what I want to go into my handbrake or ffmpeg parameters file. as you see in the script I have several types of options commented out.

for now... this is it. I still manually move the completed recoded/renamed files to their final resting place on my NAS.

_filebot_format.txt located in $HOME/.filebot as is amc.log and error.log

Code: Select all

movieFormat={n} ({y}){" CD$pi"}
seriesFormat={n.replaceTrailingBrackets()} {s00e00} - {t}
animeFormat=Anime/{n}/{fn}
minFileSize=0
my commenting may be excessive but I'm getting old and don't remember as well as I used to. be carefull copying this code as I'm editing it I see long lines word wrapped in the code box. all programs... handbrake/ffmpeg/filebot all have the option to create log files and have parameter files in their config dir under $HOME/.program name for extended troublehooting. be sure to read the ffmpeg doc for loglevel and report and where the log is placed/named.

_transmission_recode_and_rename.sh located in the same dir as your transmission d/l dir

Code: Select all

#!/usr/bin/env bash

# development i.e. debug output
# if [ -z "$1" ]; then
# 	TR_DOWNLOADS="$TR_TORRENT_DIR/$TR_TORRENT_NAME"
# else
# 	TR_DOWNLOADS="$1"
# 	TR_TORRENT_NAME="Temp_Torrent_Name"
# fi

# delete filebot media ($TR_TORRENT_NAME.log) logs older than 5 days... 
find $TR_TORRENT_DIR/_transition/Logs -mtime +5 -type f -delete
# delete ffmpeg log files if logging for ffmpeg is flagged to log
find $TR_TORRENT_DIR/_transition/_recode -mtime +2 -type f -delete

# production
# supplied by Transmission's env
TR_DOWNLOADS="$TR_TORRENT_DIR/$TR_TORRENT_NAME"

LOG_DIR="$TR_TORRENT_DIR/_transition/Logs"
# my required directories needed in Torrent download directory
# this will make the directories if they don't already exist
# mkdir -p "$LOG_DIR"
# development i.e. debug output
# mkdir -p "$TR_TORRENT_DIR/_transition"
# mkdir -p "$TR_TORRENT_DIR/_transition/_finished"
# mkdir -p "$TR_TORRENT_DIR/_transition/_recode"
# mkdir -p "$TR_TORRENT_DIR/_transition/_rename"

LOGFILE=$LOG_DIR/$TR_TORRENT_NAME.log
if [ ! -e "$LOGFILE" ] ; then
    touch "$LOGFILE"
fi

# env -vvvvv >> "$LOGFILE"	# development i.e. debug output
function edate {
	echo "`date '+%Y-%m-%d %H:%M:%S'`    $1" >> "$LOGFILE"
#			echo "$1"	# development i.e. debug output
}

edate ""	# pretty up the logfile
edate "		Working copy within Transmission."

	# Not really used in my version of this script as all programs have the ability
	# to move or copy their files on successful completion. used for func "process_file()".
	# plus I recode to x264 SD and all audio to aac.so everything goes thru func "recode_and_copy_file()".
function copy_file() {
	edate "Copying $TR_TORRENT_NAME to _rename folder"	# development i.e. debug output
	cp -v "$1" "$TR_TORRENT_DIR/_transition/_rename/" >> "$LOGFILE" 
}

function recode_and_copy_file() {
	filename=$(basename "$1")
	extension="${filename##*.}"
	filename="${filename%.*}"

	cd "$TR_TORRENT_DIR/_transition/_recode" > /dev/null 2>&1
	current_time=$(date "+%Y.%m.%d-%H.%M.%S")

	mkdir "$current_time"

edate ""	# pretty up the logfile

# development i.e. debug output
	edate "Made directory for recode i.e. $current_time/"	# development i.e. debug output

edate ""	# pretty up the logfile

	edate "ffmpeg input $1"	# development i.e. debug output
	edate "recoding $TR_TORRENT_NAME"
	edate "ffmpeg output to _recode dir $current_time/$filename.mp4"	# development i.e. debug output

	# simple Recode from .ext to .mp4
	# /opt/local/bin/ffmpeg -i "$1" -c:v copy -c:a copy "$current_time/$filename.mp4" >> "$LOGFILE"

	# simple Recode to video x264 & force aac stereo audio only
	#/opt/local/bin/ffmpeg -i "$1" -c:v libx264 copy -c:a aac_at -ac 2 -strict 2 -level 40 -map_metadata -1 -metadata creation_time=now -y "$current_time/$filename.mp4" # # >> "$LOGFILE"

	# same as above but wipes any metadata --c:v libx264 -crf 20 -r 24000/1001 -s 720x480

	# roku parameters from iffmpeg
	# /opt/local/bin/ffmpeg  -i "$1" -map 0:0 -map 0:1 -c:a aac -ab 128k -ac 2 -strict -2 -async 1 -c:v libx264 -crf 20 -r 24000/1001 -s 720x480 -aspect 16:9 -pix_fmt yuv420p -level 40 -weightp 1 -partitions partb8x8+partp4x4+partp8x8+parti8x8 -b-pyramid 2 -weightb 1 -8x8dct 0 -fast-pskip 1 -direct-pred 1 -rc-lookahead 10 -coder ac -trellis 0 -me_method hex -subq 2 -me_range 16 -bf 3 -b_strategy 1 -refs 1 -flags +loop -sws_flags fast_bilinear -sc_threshold 40 -keyint_min 24 -g 240 -qmin 3 -qmax 51 -qdiff 4 -sn -map_metadata -1 -metadata creation_time=now -y "$current_time/$filename.mp4" >> "$LOGFILE"
	# /opt/local/bin/ffmpeg  -i "$1" -map 0:0 -map 0:1 -c:a aac_at -ab 128k -ar 48000 -ac 2 -strict -2 -async 1 -c:v libx264 -crf 20 -r 24000/1001 -s 720x480 -aspect 39:22 -pix_fmt yuv420p -level 40 -weightp 1 -b-pyramid 1 -weightb 1 -8x8dct 1 -fast-pskip 1 -direct-pred 1 -x264-params deadzone-inter=0:deadzone-intra=0 -coder ac -trellis 1 -me_method hex -subq 6 -me_range 16 -bf 3 -b_strategy 1 -refs 3 -flags +loop -sws_flags fast_bilinear -sc_threshold 40 -qmin 3 -qmax 51 -qdiff 4 -sn -map_metadata -1 creation_time=now -sn -y "$current_time/$filename.mp4" >> "$LOGFILE"

	# low voice loud sound/music effects w/dolby surround - i.e. normalize voice/effects
	# /opt/local/bin/ffmpeg -i "$1" -c:v copy -c:a libfaac -bsf:a aac_adtstoasc -af aresample=matrix_encoding=dplii -ac 2 -ar 48000 -ab 256k "$current_time/$filename.mp4" >> "$LOGFILE"

	# Or if your file needs encoding, you can use HandBrake for that.
	# /opt/local/bin/HandBrakeCLI -i "$1" -o "$current_time/$filename.mp4" --preset="Normal" >> "$LOGFILE"

	# possible roku settings
	# handbrakecli -i "$1" -o "$current_time/$filename.mp4" --preset-import-file "$TR_TORRENT_DIR/_transition/roku_stick.json" >> "$LOGFILE"

	# to remove meta after using HB. HB doesn't have the ability to do that
	# /opt/local/bin/ffmpeg -i "$1" -c:v copy -c:a copy -map_metadata -1 -metadata "$current_time/$filename.mp4"  >> "$LOGFILE"


	/opt/local/bin/ffmpeg -i "$1" -c:v libx264 -crf 20 -r 24000/1001 -s 720x480 -c:a aac_at -ac 2 -strict 2 -level 40 -map_metadata -1 -metadata creation_time=now -y "$current_time/$filename.mp4" >> "$LOGFILE"
RCFF="$?"
	if [ $RCFF -eq "0" ] 
	then
		/opt/local/bin/terminal-notifier -sender com.osxbytes.iffmpeg6  -title "Recode Complete" -subtitle "$filename.$extension" -message "$current_time     DONE: error code $RCFF" -sound "ping"
	edate "Recode complete $filename.mp4"	     # development i.e. debug output
	else  
	if [ $RCFF -gt "0" ] 
	then
		/opt/local/bin/terminal-notifier -sender com.osxbytes.iffmpeg6  -title "Recode FAILED" -subtitle "$filename.$extension" -message "$current_time     DONE: error code $RCFF" -sound "basso"
	edate "Recode FAILED"	     # development i.e. debug output
	fi	
	fi

edate ""	# pretty up the logfile
edate ""	# pretty up the logfile

	mv -vf "$current_time/$filename.mp4" "$TR_TORRENT_DIR/_transition/_rename/" >> "$LOGFILE" 	
 	edate "Moving _recode/$current_time/$filename.mp4 to _rename folder"	# development i.e. debug output
	rm -frv "$current_time"
	edate "deleting _recode/$current_time/ directory"	# development i.e. debug output

edate ""	# pretty up the logfile

	edate "renaming _rename/$filename.mp4"	# development i.e. debug output

FB_PATH="$TR_TORRENT_DIR/_transition/_rename/"
FB_NAME="$filename.mp4"
FB_LABEL="N/A"		
FB_OUTPUT="$TR_TORRENT_DIR/_transition/_finished"

	edate "debug the variables: FB_output = $FB_OUTPUT  , ut_dir = $FB_PATH , ut_name = $FB_NAME"	# development i.e. debug output
	# I use --action move instead of "duplicate" or "copy" but either of these may be used for testing purposes.
	/usr/local/bin/filebot -script fn:amc --output "$FB_OUTPUT" --action move --conflict skip -non-strict --log ALL --log-file amc.log --def @"$HOME"/.filebot/_filebot_format.txt ut_dir="$FB_PATH" ut_kind="multi" ut_title="$FB_NAME" ut_label="$FB_LABEL" >> "$LOGFILE" 2>&1
RCFB="$?"
fbname=$( ls -t $FB_OUTPUT | head -n1 )
fbname=$( ls -t $FB_OUTPUT/$fbname | head -n1)

	if [ $RCFB -eq "0" ]
	then
		/opt/local/bin/terminal-notifier -sound "ping" -sender net.filebot.Filebot -title "Rename Complete" -subtitle "$fbname" -message "$current_time     DONE: error code $RCFB"
	edate "Rename Complete"		# development i.e. debug output
	else
	if [ $RCFB -gt "0" ]
	then
		/opt/local/bin/terminal-notifier -sound "basso" -sender net.filebot.Filebot -title "Rename FAILED" -subtitle "$filename.$extension" -message "$current_time     DONE: error code $RCFB"
	edate "Rename FAILED"		# development i.e. debug output
	fi
unset fbname
	fi
# env >> "$LOGFILE"	# development i.e. debug output
edate ""	# pretty up the logfile
}  # end of function recode_and_copy_file()

function process_file() {
	filename=$(basename "$1")
	extension="${filename##*.}"
	filename="${filename%.*}"
# env -vvvvv >> "$LOGFILE"	# development i.e. debug output
	
#	edate "Processing file: $filename.$extension"	# development i.e. debug output
	case "$extension" in 
	# orignally copy_file() function
		"mp4") recode_and_copy_file "$1"
			;;
	# orignally copy_file() function
		"m4v") recode_and_copy_file "$1"
			;;

		"mkv") recode_and_copy_file "$1"
			;;

		"avi") recode_and_copy_file "$1"
			;;

		"mov") recode_and_copy_file "$1"
			;;

	esac
}

edate "Torrent Directory is $TR_TORRENT_DIR"
edate "Torrent ID is $TR_TORRENT_ID"
edate "Torrent Hash is $TR_TORRENT_HASH"
edate "Working on the new download $TR_DOWNLOADS"

if [ -d "$TR_DOWNLOADS" ]; then
	SAVEIFS=$IFS
	IFS=$(echo -en "\n\b")
	for f in $(find "$TR_DOWNLOADS"); do
		if ! [ -d "$f" ]; then
			process_file "$f"
#			edate "processing ... $f "
		fi
	done;
	IFS=$SAVEIFS
else
	process_file "$TR_DOWNLOADS"
fi



example log file output

Code: Select all

2017-12-11 00:12:28    Directory is /Volumes/Stuff/!Movies
2017-12-11 00:12:28    Torrent ID is 4
2017-12-11 00:12:28    Torrent Hash is 3f774c87c0163646ecd63b4381b73c740313ec77
2017-12-11 00:12:28    Working on the new download /Volumes/Stuff/!Movies/Family.Guy.S16E09.WEB.x264-TBS[rarbg]
2017-12-11 00:12:28    Processing file: family.guy.s16e09.web.x264-tbs.mkv
2017-12-11 00:12:28    Making directory for recoding i.e. 2017.12.11-00.12.28/
2017-12-11 00:12:28    HB or ffmpeg input /Volumes/Stuff/!Movies/Family.Guy.S16E09.WEB.x264-TBS[rarbg]/family.guy.s16e09.web.x264-tbs.mkv
2017-12-11 00:12:28    HB or ffmpeg output from _recode dir 2017.12.11-00.12.28/family.guy.s16e09.web.x264-tbs.mp4
2017-12-11 00:12:28    Converting /Volumes/Stuff/!Movies/Family.Guy.S16E09.WEB.x264-TBS[rarbg]/family.guy.s16e09.web.x264-tbs.mkv
2017-12-11 00:16:43    HB or ffmpeg exit code 0
2017-12-11 00:16:43    Conversion complete /Volumes/Stuff/!Movies/Family.Guy.S16E09.WEB.x264-TBS[rarbg]/family.guy.s16e09.web.x264-tbs.mkv
2017-12-11 00:16:43    Moving 2017.12.11-00.12.28/family.guy.s16e09.web.x264-tbs.mp4 to _rename folder
2017.12.11-00.12.28/family.guy.s16e09.web.x264-tbs.mp4 -> /Volumes/Stuff/!Movies/_transition/_rename/family.guy.s16e09.web.x264-tbs.mp4
2017-12-11 00:16:43    FB_output = /Volumes/Stuff/!Movies/_transition/_finished  , ut_dir = /Volumes/Stuff/!Movies/_transition/_rename/ , ut_name = family.guy.s16e09.web.x264-tbs.mp4
2017-12-11 00:17:00    development i.e. debug output for possible if/then/else routine
2017-12-11 00:17:00    FB exit code 0
2017-12-11 00:17:00    Rename Complete
2017-12-11 00:17:00    Recode error 0 or Rename error 0 failed
2017-12-11 00:17:00    Processing file: family.guy.s16e09.web.x264-tbs.nfo
2017-12-11 00:17:00    Processing file: RARBG.txt
edit: further refined the script for error trapping an system notifications.
Post Reply