toMKV.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # Grab some codec info
  27. INFO="`~/bin/video/movInfo.pl "${inFile}" 2>/dev/null`"
  28. VCODECS="`echo "${INFO}" | grep VIDEO_CODEC`"
  29. if [ -z "${VCODECS}" ]; then
  30. if echo "${inFile}" | grep -qi '\.avi$' && [ `wc -c < "${inFile}"` -lt $MIN_AVI_SIZE ]; then
  31. echo "Deleting unlikely AVI: ${inFile}" 1>&2
  32. rm -f "${inFile}"
  33. exit 3
  34. fi
  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. # Exit cleanly
  79. exit 0