toMKV.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # Defaults
  3. if [ -z "${MIN_AVI_SIZE}" ]; then
  4. MIN_AVI_SIZE=$(( 350 * 1024 * 1024 ))
  5. fi
  6. function tryRecode() {
  7. file="${1}"
  8. # Try to recode with Handbrake/ffmpeg
  9. if ~/bin/video/canRecode.sh; then
  10. echo "`basename "${0}"`: Attempting recode instead..." 1>&2
  11. ~/bin/video/recode "${file}"
  12. exit "${?}"
  13. else
  14. echo "`basename "${0}"`: Unable to recode." 1>&2
  15. exit 1
  16. fi
  17. }
  18. # Command line
  19. inFile="${1}"
  20. outFile="${2}"
  21. if [ -z "${inFile}" ] || [ ! -e "${inFile}" ]; then
  22. echo "Usage: `basename "${0}"` input_file [output_file]" 1>&2
  23. exit 1
  24. fi
  25. # AVIs are always unlikely
  26. if echo "${inFile}" | grep -qi '\.avi$' && [ `wc -c < "${inFile}"` -lt $MIN_AVI_SIZE ]; then
  27. echo "Deleting unlikely AVI: ${inFile}" 1>&2
  28. rm -f "${inFile}"
  29. exit 3
  30. fi
  31. # Grab some codec info
  32. INFO="`~/bin/video/movInfo.pl "${inFile}" 2>/dev/null`"
  33. VCODECS="`echo "${INFO}" | grep VIDEO_CODEC`"
  34. if [ -z "${VCODECS}" ]; then
  35. echo "`basename "${0}"`: Could not determine video codec: ${inFile}" 1>&2
  36. # Try to recode -- this does not return
  37. tryRecode "${inFile}"
  38. fi
  39. # Exclude files with MPEG-1/2 streams
  40. if echo "${VCODECS}" | grep -q ffmpeg[12]; then
  41. echo "Usage: `basename "${0}"` input_file [output_file]" 1>&2
  42. exit 1
  43. fi
  44. # Construct the output file name
  45. if [ -z "${outFile}" ]; then
  46. outFile="`basename "${inFile}"`"
  47. outFile="`echo "${outFile}" | sed 's%\.[A-Za-z0-9]*$%%'`"
  48. outFile="`dirname "${inFile}"`/${outFile}"
  49. fi
  50. # Merge external subtitles, if they exist
  51. srtFile="`echo "${inFile}" | sed 's%\.[^\.]*$%%'`.srt"
  52. if [ ! -e "${srtFile}" ]; then
  53. srtFile="`echo "${inFile}" | sed 's%\.[^\.]*$%%'`.ssa"
  54. fi
  55. if [ ! -e "${srtFile}" ]; then
  56. srtFile=""
  57. fi
  58. # Convert to MKV
  59. # Hide some common AVI conversion warnings
  60. tmpFile="`mktemp -t toMKV`"
  61. outFile="${outFile}.mkv"
  62. if [ -n "${srtFile}" ]; then
  63. OUT="`mkvmerge --quiet --default-language eng -o "${tmpFile}" "${inFile}" "${srtFile}" 2>&1`"
  64. else
  65. OUT="`mkvmerge --quiet --default-language eng -o "${tmpFile}" "${inFile}" 2>&1`"
  66. fi
  67. echo "${OUT}" | grep -v "The AVC video track is missing the 'CTTS' atom for frame timecode offsets."
  68. # Check for errors
  69. if [ ! -e "${tmpFile}" ] || [ `stat -f '%z' "${tmpFile}"` -lt 1000 ]; then
  70. echo "`basename "${0}"`: Error creating output file for input: ${inFile}" 1>&2
  71. # Try to recode -- this does not return
  72. tryRecode "${inFile}"
  73. fi
  74. # Move into place, dropping the original
  75. tmpOut="`mktemp "${outFile}.XXXXXXXX"`"
  76. cp -X "${tmpFile}" "${tmpOut}" && rm "${inFile}" && mv "${tmpOut}" "${outFile}"
  77. rm -f "${tmpFile}" "${srtFile}"
  78. # Re-label undefined audio tracks in files with no English tracks
  79. ~/bin/video/undToEng.pl "${outFile}"
  80. # Exit cleanly
  81. exit 0