toMov.sh 1.5 KB

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