Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

My dvd ripping solution, 2022 edition

Last year, I wrote about my dvd-ripping solution. Of course, over time my process has improved and evolved.

Ripping the DVDs

I now use makemkv again, because Handbrake itself suggests that other tools are better for ripipng from removable media. Handbrake is for transcoding.

When I stick discs in my (3) drives, I then run a command inside GNU screen.

time APPLY=1 INPUT=/dev/sr0:/dev/sr1:/dev/sr2 JOBNAME=s3d6 sh -x makemkv.sh

The jobname is arbitrary and helps keep job outputs distinct when I let them pile up. Here is the script makemkv.sh.

#!/bin/sh
# Startdate: 2021-10-02 19:21
# Purpose: rip disc automatically
# History:
#    This is the second version of makemkv.sh (first one was renamed to makemkv-2019.sh)
#    2022-08-03 13:11 add unshare to keep makemkv from accessing the network
# Reference:
#    /mnt/public/Support/Programs/DVDs/handbrake-internal.sh
# Flow: use makemkv to make the original rip, and then handbrake to convert to the preferred size/format.
test -z "${INPUT}" && INPUT="/dev/sr0" # colon-delimited
test -z "${OUTPUTDIR}" && OUTPUTDIR=/mnt/public/Video/temp/makemkv
test -z "${JOBNAME}" && JOBNAME="$( < /dev/urandom tr -dc 'A-Z0-9' | head -c6 )"
test -z "${step1file}" && step1file="${OUTPUTDIR}/1-${JOBNAME}-run-makemkv.sh"
test -z "${step2file}" && step2file="${OUTPUTDIR}/2-${JOBNAME}-run-handbrake.sh"
printf '' > "${step1file}"
printf '' > "${step2file}"
for line in $( echo ":${INPUT}:" | tr ':' '\n' | grep -viE '^\s*$' ) ;
do
   bline="$( basename "${line}" )"
   echo "process ${line}"
   mminfo="$( unshare -r -n makemkvcon info dev:"${line}" )"
   #echo "${mminfo}" >> "${TMPFILE}"
   count="$( echo "${mminfo}" | sed -r -e '1,/Total/d' | grep -c Title )"
   raw="$( HandBrakeCLI --markers --format mkv --min-duration 125 --scan --input "${line}" --output "${OUTPUT}"  --encoder x264 --rate 30 --native-language eng --title 0 2>&1 )"
   disctitle="$( echo "${raw}" | sed -n -r -e '/DVD Title:/{s/.*DVD Title: //;p}' )"
   x=0
   while test $x -lt $count ;
   do
      OUTDIR="${OUTPUTDIR}/${JOBNAME:+${JOBNAME}_}${bline}/${disctitle}_${x}" ; OUTDIR="$( echo "${OUTDIR}" | tr ' ' '_' )"
      #mkdir -p "${OUTDIR}"
      #echo makemkvcon mkv "dev:${line}" "${x}" "${OUTDIR}"
      echo "mkdir -p \"${OUTDIR}\" ; unshare -r -n makemkvcon mkv \"dev:${line}\" \"${x}\" \"${OUTDIR}\"" >> "${step1file}"
      newfile="$( find "${OUTDIR}" -mindepth 1 -maxdepth 1 ! -type d -print -quit 2>/dev/null )"
      test -z "${newfile}" && newfile="${OUTDIR}/*.mkv"
      outfilename="$( echo "${OUTPUTDIR}/${JOBNAME:+${JOBNAME}_}${bline}_${disctitle}v${x}.mkv" | tr ' ' '_' )"
      echo "HandBrakeCLI --markers --format mkv --input "${OUTDIR}"/*.mkv --output \"${outfilename}\" --encoder x264 --rate 30 --native-language eng --mixdown 6ch --aencoder ffaac --audio 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, --subtitle 0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,scan" >> "${step2file}"
      x=$((x+1))
   done
done
# So after all the prep work is done, we need to run step 1 and then step 2
echo "BEGIN STEP 1"
if test -n "${APPLY}" ;
then
   sh -x "${step1file}"
else
   cat "${step1file}"
fi
echo "####################################################################"
echo "####################################################################"
echo "You may now eject the disc(s)."
echo "####################################################################"
echo "####################################################################"
echo "BEGIN STEP 2"
if test -n "${APPLY}" ;
then
   sh -x "${step2file}"
else
   cat "${step2file}"
fi
echo "DONE"
date

Once that is done, the raw ripped files are stored at glob /mnt/public/Video/temp/makemkv/JOBNAME_sr*/*/*.mkv, and the handbrake-transcoded files are at glob /mnt/public/Video/temp/makemkv/JOBNAME_sr*_namefromdisc.mkv.

Now I manually inspect each video file and rename them to the correct episode name, or extra file name (which probably won't be updated in the next step).

For show episodes, I run a script which I described in that previous post. I have an input csv which I hand-curate from the Wikipedia pages usually, that includes columns s, sep, ep, airdate, filename.

have,s,ep,sep,title,airdate,filename
1,1,1,01-e02,Encounter at Farpoint,1987-09-28,s01e01-e02 - Encounter at Farpoint
1,1,3,3,The Naked Now,1987-10-05,s01e03 - The Naked Now
1,1,4,4,Code of Honor,1987-10-12,s01e04 - Code of Honor
1,1,5,5,The Last Outpost,1987-10-19,s01e05 - The Last Outpost
1,1,6,6,Where No One Has Gone Before,1987-10-26,s01e06 - Where No One Has Gone Before

I describe my csv generation process a little more in that previous post.

Then I run the next script:

time tv-mkv-helper.py --inputcsv "/mnt/public/Video/TV/Star Trek The Next Generation (1987)/STTNG.csv" -d /mnt/public/Video/temp/

This script is still exactly the same as in the previous post. I like the metadata of the original airdate on the episode, as well as of course video title. And I have never found a use for the "next file" tags that mkvtoolnix can show you, but I populate them because I can. Maybe it'll be useful someday.

And then I manually move the files to their intended destinations.

Comments