discToMkv.sh 3.2 KB

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