Skip to content

Folder Processing

Michael Higgins edited this page Jan 30, 2021 · 1 revision

This bash script was contributed by eikowagenknecht

Features:

  • Optionally choose root directory to process and save multiple autoProcess.ini files, one for each directory.
  • Choose which files to process based on RegEx patterns. You insert two patterns here: One for files you want processed normally and one for files you want to process with the "fc" option (="Force conversion regardless of streams being in appropriate format").
  • Show which files will be processed.
  • Fix the permissions after running (needed when run from docker on Synology).
  • Optionally run in background mode (exiting the SSH connection won't stop the processing then).
#!/bin/bash

FOLDER='/tv'
CONFIG_FILE='autoProcess.ini'

# Comment this in if you want to be able to choose from multiple directories and use different autoProcess.ini settings for them.
##### Select folder
#echo "Which folder should be processed?"
#select FOLDER in "/movies" "/tv"
#do
#	case "$FOLDER" in
#		/movies) CONFIG_FILE='autoProcessMovies.ini'; break ;;
#		/tv) CONFIG_FILE='autoProcessShows.ini'; break ;;
#	esac
#done
#echo "You selected $FOLDER."

# Feel free to adjust the defaults for often needed values
DEFAULT_MUXED='.*\.mkv'
DEFAULT_FORCED='.*\(264\|265\|HEVC\|hevc\).*10.*\.\(mkv\|mp4\)' 
if [ -z "$1" ]; then
	# No user input given, handle it here.
	read -e -r -p "Process files (prefer muxing): " -i $DEFAULT_MUXED REGEX_MUXED
	read -e -r -p "Process files (force encoding): " -i $DEFAULT_FORCED REGEX_FORCED

	COMMAND_FORCED=(find $FOLDER -regex $REGEX_FORCED)
	COMMAND_MUXED=(find $FOLDER -regex $REGEX_MUXED ! -regex $REGEX_FORCED)
	FILES_FORCED=$(${COMMAND_FORCED[@]})
	FILES_MUXED=$(${COMMAND_MUXED[@]})

	echo ""
	echo "Processed files (prefer muxing):"
	echo "$FILES_MUXED"
	echo ""
	echo "Processed files (force encoding):"
	echo "$FILES_FORCED"
	echo ""
	echo "Should these files be processed?"
	select yn in "Yes" "No"
	do
		case $yn in
			Yes) break;;
			No) exit;;
		esac
	done
	echo ""
	echo "Run in background mode?"
	select yn in "Yes" "No"
	do
		case $yn in
			Yes) BACKGROUND=true; break ;;
			No) BACKGROUND=false; break ;;
		esac
	done
	
	if [ $BACKGROUND = true ] ; then
		# Run script in background by calling itself with arguments
		echo "Running in background: $0 $REGEX_MUXED $REGEX_FORCED"
		$0 $REGEX_MUXED $REGEX_FORCED &
		exit 0
	fi
	# No else here because when it's not run in background just continue as if the parameters were set.
else
	# If parameters are set, use them
	echo ""
	echo "Script called with arguments $1 and $2."
	REGEX_MUXED=$1
	REGEX_FORCED=$2
fi

COMMAND_FORCED=(find $FOLDER -regex $REGEX_FORCED)
COMMAND_MUXED=(find $FOLDER -regex $REGEX_MUXED ! -regex $REGEX_FORCED)
FILES_FORCED=$(${COMMAND_FORCED[@]})
FILES_MUXED=$(${COMMAND_MUXED[@]})

IFS=$'\n'; set -f
for f in $FILES_MUXED
do
	echo "--------------------"
	echo "Processing $f..."
	$SMA_PATH/venv/bin/python3 $SMA_PATH/manual.py -a -c ./$CONFIG_FILE -i "$f"
done
unset IFS; set +f

IFS=$'\n'; set -f
for f in $FILES_FORCED
do
	echo "--------------------"
	echo "Processing $f..."
	$SMA_PATH/venv/bin/python3 $SMA_PATH/manual.py -fc -a -c ./$CONFIG_FILE -i "$f"
done
unset IFS; set +f

# Fix permissions (adjust if needed, these are my values for synology docker)
chown -R abc:users $FOLDER
chmod -R a=,u+rwX,g+rwX $FOLDER
Clone this wiki locally