discToMkv.sh 3.3 KB

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