discToMkv.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # Config
  3. DRIVE_NAME="iHBS112"
  4. OUT_DIR="${HOME}/Desktop/Docs/Downloads"
  5. APP_PATH="/Applications/Zach/Media/MakeMKV.app"
  6. # Globals
  7. BIN_PATH="${APP_PATH}/Contents/MacOS/makemkvcon"
  8. TMP="`mktemp -t discToMkv`"
  9. # Sanity check
  10. if [ ! -d "${OUT_DIR}" ]; then
  11. echo "Invalid output directory: ${OUT_DIR}"
  12. exit 1
  13. fi
  14. # Find the DR drive number based on the device name
  15. i=1
  16. declare -i DRIVE_NUM_DR
  17. while [ $i -lt 100 ]; do
  18. STATUS="`drutil -drive $i status 2>&1`"
  19. if echo "${STATUS}" | grep -q "${DRIVE_NAME}"; then
  20. DRIVE_NUM_DR=$i
  21. break
  22. elif echo "${STATUS}" | grep -q 'Could not find a valid device.'; then
  23. break
  24. fi
  25. i=$(( $i + 1 ))
  26. done
  27. # Ensure a disk is inserted
  28. if drutil -drive "${DRIVE_NUM_DR}" status | grep -q 'Type: No Media Inserted'; then
  29. echo "No disk available" 1>&2
  30. drutil -drive "${DRIVE_NUM_DR}" tray open
  31. exit 1
  32. fi
  33. # Find the MKV drive number and OS device path based on the device name
  34. declare -i DRIVE_NUM_MKV
  35. DEV_PATH=""
  36. DRIVE="`"${BIN_PATH}" --robot info dev | grep '^DRV:' | cut -d ':' -f 2- | grep "${DRIVE_NAME}"`"
  37. DRIVE_NUM_MKV="`echo "${DRIVE}" | awk -F ',' '{print $1}'`"
  38. DEV_PATH="`echo "${DRIVE}" | awk -F ',' '{print $7}' | cut -d '"' -f 2`"
  39. # Sanity check
  40. if [ $DRIVE_NUM_MKV -lt 1 ] || [ $DRIVE_NUM_DR -lt 1 ] || [ -z "${DEV_PATH}" ] || [ ! -c "${DEV_PATH}" ]; then
  41. echo "Unable to find drive: ${DRIVE_NAME}" 1>&2
  42. exit 1
  43. fi
  44. # Parse the disc
  45. "${BIN_PATH}" --noscan --robot "--messages=${TMP}" info "disc:${DRIVE_NUM_MKV}"
  46. # Find the disk title -- long if available, short if not
  47. CINFO="`grep '^CINFO\:' "${TMP}"`"
  48. LONG="`echo "${CINFO}" | awk -F ',' '$1 == "CINFO:2" && $2 == "0" {print $3}' | sed 's%^"\(.*\)"$%\1%'`"
  49. SHORT="`echo "${CINFO}" | awk -F ',' '$1 == "CINFO:32" && $2 == "0" {print $3}' | sed 's%^"\(.*\)"$%\1%'`"
  50. NAME="${LONG}"
  51. if [ -z "${NAME}" ]; then
  52. NAME="${SHORT}"
  53. fi
  54. # Cleanup the parse data
  55. rm -f "${TMP}"
  56. # Sanity check
  57. if [ -z "${NAME}" ]; then
  58. echo "Unable to determine disc name" 1>&2
  59. exit 1
  60. fi
  61. # Ensure the name is filesystem-safe
  62. NAME="`echo "${NAME}" | sed 's%[^a-zA-Z0-9_\-\ ,;\.!@#$\%\^&\*()=\+\?]%_%'`"
  63. # Create the output directory
  64. if [ ! -d "${OUT_DIR}/${NAME}" ]; then
  65. mkdir "${OUT_DIR}/${NAME}"
  66. fi
  67. # Extract all tracks to MKVs
  68. # Tracks are selected with the default profile (as set in the GUI)
  69. # Recommended selection string:
  70. # -sel:all,+sel:(favlang|nolang),-sel:(core),+sel:special,-sel:mvcvideo,=100:all,-10:favlang
  71. # This preserves all audio and subtitles in your prefered language, all audio and subtitles with no language, and all special tracks, but excludes the core audio from DTS-HD tracks
  72. # Or if you capture films with no soundtrack in your favorite language:
  73. # -sel:all,+sel:(audio),-sel:(havemulti),+sel:(favlang|nolang),-sel:(core),+sel:special,-sel:mvcvideo,=100:all,-10:favlang
  74. "${BIN_PATH}" --noscan --robot mkv "disc:${DRIVE_NUM_MKV}" all "${OUT_DIR}/${NAME}"
  75. # Bail on error
  76. if [ $? -ne 0 ]; then
  77. exit $?
  78. fi
  79. # Eject when complete
  80. diskutil eject "${DEV_PATH}"