toMKV.sh 2.5 KB

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