Newer
Older
rconvert / rconvert
GallaFrancesco on 8 Apr 4 KB install + dir creation
#!/bin/bash

# mountpoint
TARGET=~/mnt


# base directory for rsync
BASEDIR=$TARGET/MVScript

export IN=flac
export OUT=mp3
export FFMPEG_FLAGS="-vn -ar 44100 -ac 2 -ab 192k" #mp3
#export FFMPEG_FLAGS="-codec:a libvorbis -qscale:a 7" #ogg

# number of parallel conversions
NPROC=12

if [[ -e ".mdirs" ]]; then
	rm ".mdirs"
fi

touch ".mdirs"

if [ -p /dev/stdin ]; then
	while read line; do
		echo $line >> ".mdirs"
	done
elif [[ $# -lt 1 || $1 == "--help" || $1 == "-h" ]]; then
	echo "==============================================="
	printf "[rconvert]\n"
	echo "==============================================="
	printf "Use ffmpeg to convert a set of media files\nfrom one format (IN) to another (OUT),\nthen send them to a mountpoint via rsync.\n"
	echo "==============================================="
	printf "Usage: rconvert [dir1] [dir2] ... [dirN]\n"
	echo
	printf "Each directory must contain some 'files.IN' to convert to 'files.OUT'\n"
	echo   "Each file in [dir...] matching the .IN extension will be converted inside [dir...]"
	echo   "The conversion process uses GNU parallel with NPROC tasks"
	echo   "[dir...] will be replicated to the destination target with files matching the .OUT extension"
	echo "==============================================="
	printf "Configuration: edit \$IN, \$OUT, \$TARGET, \$BASEDIR, \$NPROC in 'rconvert'\n"
	exit 0
else
	for DIR in "$@"; do
		echo $DIR >> ".mdirs"
	done
fi

function convert {
	OUTFILE="${1%.*}.$OUT"
	INFILE="${1%.*}.$IN"

	[[ -e "$OUTFILE" ]] && printf "[FOUND] %s\n File already exists. Skipping.\n" "$OUTFILE" && exit

	ffmpeg -loglevel error -i "${INFILE}" $FFMPEG_FLAGS -f ${OUT} "$OUTFILE"
	printf "[FFMPEG-SOURCE] %s\n[FFMPEG-DST] %s\n" "${INFILE}" "$OUTFILE"

	if [[ $? -ne 0 ]]; then
		echo
		echo "-----------------[ERROR]-----------------------"
		echo "While encoding: $OUTFILE"
	else
		echo
		echo "----------------[SUCCESS]----------------------"
	fi
}
export -f convert

if [ ! -d "$TARGET" ]; then
	echo "==============================================="
	echo "Creating directory $TARGET"
	mkdir -p $TARGET
	echo "==============================================="
fi

# for each cli argument
while read DIR
do
	echo "==============================================="
	echo "$DIR"
	echo "==============================================="
	if [[ ! -e "$DIR" ]]; then
		echo "Couldn't find "$DIR" in $(pwd)"
		exit 1
	fi

	DEST="$BASEDIR/$DIR"

	find "$DIR" -iname "*.$IN" | parallel -j $NPROC convert
	cnt=$(find "$DIR" -iname "*.$OUT" | wc -l)
	echo "==============================================="
	if [[ $cnt -eq 0 ]]; then
		echo "No files converted, aborting transfer."
		continue
	fi
	echo "Converted $cnt $OUT files:"
	echo "==============================================="
	find "$DIR" -iname "*.$OUT" > ".tmp.files"
	cat ".tmp.files"
	echo "==============================================="


	# check if mountpoint is correctly mounted
	if [[ $(findmnt -m $TARGET) ]]; then
		echo "Transfer files to \"$DEST\"?"
		read -p "'y' to copy, 'move' to move. [y/move/N]" c < /dev/tty
		echo "==============================================="

		if [[ $c == "y" || $c == "Y" || $(echo $c | tr '[:upper:]' '[:lower:]') == "yes" ]]; then
			REMOVE=""
		elif [[ $(echo $c | tr '[:upper:]' '[:lower:]') == "move" ]]; then
			REMOVE="--remove-source-files"
			echo "[WARNING] Converted files will be moved. Ctrl-C to abort"
			time=5
			while [ $time -gt 0 ]; do
			   printf " $time"
			   sleep 1
			   : $((time--))
			done
			echo
		else
			echo "Aborting transfer."
			continue
		fi

		if [[ -w $TARGET ]]; then
			rsync -zrv $REMOVE --progress --include "*/" --include "*.$OUT" --exclude "*" "$DIR" "$DEST"
		else
			echo "You don't have write permissions on $TARGET"
			echo "Enter passwd to transfer a superuser, Ctrl-C to abort."
			sudo rsync -zrv $REMOVE --progress --include "*/" --include "*.$OUT" --exclude "*" "$DIR" "$DEST"
		fi
	else
		echo "Aborting transfer since $TARGET is not mounted."
	fi
done<".mdirs"

if [[ -e ".tmp.files" ]]; then
	rm ".tmp.files"
fi

echo "==============================================="
echo "Done!"