toMov.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # Command line
  3. inFile="${1}"
  4. outFile="${2}"
  5. movLength="${3}"
  6. if [ -z "${inFile}" ] || [ ! -e "${inFile}" ]; then
  7. echo "Usage: `basename "${0}"` input_file [output_file] [mov_length]" 1>&2
  8. exit 1
  9. fi
  10. # Grab some codec info
  11. INFO="`~/bin/video/movInfo.pl "${inFile}"`"
  12. ACODECS="`echo "${INFO}" | grep AUDIO_CODEC`"
  13. if [ -z "${ACODECS}" ]; then
  14. echo "`basename "${0}"`: Could not determine audio codec" 1>&2
  15. exit 2
  16. fi
  17. VCODECS="`echo "${INFO}" | grep VIDEO_CODEC`"
  18. if [ -z "${VCODECS}" ]; then
  19. echo "`basename "${0}"`: Could not determine video codec" 1>&2
  20. exit 2
  21. fi
  22. # Exclude files with DTS soundtracks or MPEG-1/2 streams
  23. if echo "${ACODECS}" | grep -q ffdca; then
  24. exit 1
  25. elif echo "${VCODECS}" | grep -q ffmpeg[12]; then
  26. exit 1
  27. fi
  28. # Construct the output file name
  29. if [ -z "${outFile}" ]; then
  30. # Use the input file name with a .mov extension
  31. outFile="`basename "${inFile}"`"
  32. outFile="`echo "${outFile}" | sed 's%\.[A-Za-z0-9]*$%%'`"
  33. outFile="`dirname "${inFile}"`/${outFile}"
  34. fi
  35. # Convert to MOV or MKV
  36. tmpFile="`mktemp -t toMov`"
  37. if which catmovie > /dev/null 2>&1; then
  38. outFile="${outFile}.mov"
  39. catmovie -q -self-contained -o "${tmpFile}" "${inFile}" 2>/dev/null
  40. elif which mkvmerge > /dev/null 2>&1; then
  41. outFile="${outFile}.mkv"
  42. mkvmerge -o "${tmpFile}" "${inFile}"
  43. fi
  44. # Check for errors
  45. if [ ! -e "${tmpFile}" ] || [ `stat -f '%z' "${tmpFile}"` -lt 1000 ]; then
  46. echo "`basename "${0}"`: Error creating output file for input: ${inFile}" 1>&2
  47. # Try to recode (with Handbrake/ffmpeg) if catmovie/mkvmerge fails
  48. echo "`basename "${0}"`: Attempting recode instead..." 1>&2
  49. ~/bin/video/recode "${inFile}"
  50. exit "${?}"
  51. fi
  52. # Move into place, dropping the original
  53. tmpOut="`mktemp "${outFile}.XXXXXXXX"`"
  54. cp -X "${tmpFile}" "${tmpOut}" && rm "${inFile}" && mv "${tmpOut}" "${outFile}"
  55. rm -f "${tmpFile}"
  56. # Exit cleanly
  57. exit 0