Skip to content

Instantly share code, notes, and snippets.

@huguesb
Created September 14, 2018 23:16
Show Gist options
  • Save huguesb/5cdad457bf882db8c5cf925463d2c2dd to your computer and use it in GitHub Desktop.
Save huguesb/5cdad457bf882db8c5cf925463d2c2dd to your computer and use it in GitHub Desktop.
Semi-automated DVD ripping w/ HandBrakeCLI
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"
SELF=$SCRIPTPATH/$(basename $0)
LOG=/var/log/ripit.log
function usage() {
cat <<EOF
ripit, semi-automated dvd ripping
usage: $0 master </path/to/storage>
$0 event <device> <path/to/storage>
$0 slave <device> <path/to/storage>
EOF
}
if [[ $# < 2 ]] ; then
usage
exit 1
fi
if [[ $1 == "master" ]] ; then
# mode: master
STORAGE=$2
RULES=/etc/udev/rules.d/99-ripit.rules
# add udev rule which calls slave on disk insertion
cat > $RULES <<EOF
ACTION=="change", KERNEL=="sr[0-9]*", ENV{ID_CDROM_DVD}=="1", ENV{ID_CDROM_MEDIA_STATE}=="complete", RUN+="$SELF event \$kernel '$STORAGE'"
EOF
udevadm control --reload-rules
echo "udev rule installed, waiting for disk insertion"
# tail slave log
touch $LOG
tail -n 0 -f $LOG
echo "removing udev rule"
rm -rf $RULES
udevadm control --reload-rules
elif [[ $1 == "event" ]] ; then
# mode: event
exec >> $LOG
exec 2>&1
echo "$@"
# re-invoke self detached to avoid blocking udevd
echo "$SELF" slave "${@:2}" | batch
elif [[ $1 == "slave" ]] ; then
# mode: slave
exec >> $LOG
exec 2>&1
dev=/dev/$2
echo "insertion event: $dev"
# grab volume label
TITLE=$(blkid -o value -s LABEL $dev)
# TODO convert "UPPER_SNAKE_CASE" to "Title Space Case"
# TODO grab title from DVD content rather than volume label?
dst=$3/$TITLE.mp4
if [ -f $dst ] ; then
echo "$dst already exists"
else
echo "ripping: $dst"
# TODO: avoid calling handbrake as root?
# TODO: refine transcoding options
HandBrakeCLI -i $dev \
--main-feature \
--audio-lang-list eng,fra \
--subtitle-lang-list eng,fra \
-f av_mp4 \
-o $dst
# TODO: remove output on failure
# TODO: chown/chmod output
fi
eject $dev
else
usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment