toMov.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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]*$%%'`"
  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. outfile="${outfile}.mov"
  31. elif which mkvmerge > /dev/null 2>&1; then
  32. mkvmerge -o "${tmpFile}" "${inFile}"
  33. outfile="${outfile}.mkv"
  34. fi
  35. # Check for errors
  36. if [ ! -e "${tmpFile}" ] || [ `stat -f '%z' "${tmpFile}"` -lt 1000 ]; then
  37. echo "`basename "${0}"`: Error creating output file for input: ${inFile}" 1>&2
  38. # Try to recode (with Handbrake/ffmpeg) if catmovie/mkvmerge fails
  39. echo "`basename "${0}"`: Attempting recode instead..." 1>&2
  40. ~/bin/video/recode "${inFile}"
  41. exit "${?}"
  42. fi
  43. # Move into place, dropping the original
  44. tmpOut="`mktemp "${outFile}.XXXXXXXX"`"
  45. cp -X "${tmpFile}" "${tmpOut}" && rm "${inFile}" && mv "${tmpOut}" "${outFile}"
  46. rm -f "${tmpFile}"
  47. # Exit cleanly
  48. exit 0