Advertisement
Guest User

bpm-tag-patch

a guest
Aug 29th, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.60 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2013 Mark Hills <mark@xwax.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # version 2, as published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License version 2 for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # version 2 along with this program; if not, write to the Free
  16. # Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. #
  19.  
  20. #
  21. # Analyse an audio file and add BPM metadata
  22. #
  23.  
  24. set -e
  25.  
  26. usage()
  27. {
  28.     cat <<END
  29. bpm-tag (C) Copyright 2013 Mark Hills <mark@xwax.org>
  30.  
  31. Usage: bpm-tag [options] <file>
  32. Tag an audio file with tempo (in beats-per-minute, BPM)
  33.  
  34.   -f   Ignore existing BPM value
  35.   -n   Display BPM only, don't tag
  36.   -m   Minimum detected BPM
  37.   -x   Maximum detected BPM
  38.   -h   Display this help message and exit
  39.  
  40. See the bpm-tag(1) man page for more information and examples.
  41. END
  42. }
  43.  
  44. # Parse command line arguments
  45.  
  46. FORCE=false
  47. WRITE=true
  48. ARGS=""
  49.  
  50. while getopts "fhnm:x:" OPT; do
  51.     case "$OPT" in
  52.     f)
  53.         FORCE=true
  54.         ;;
  55.     n)
  56.         WRITE=false
  57.         ;;
  58.     m)
  59.         ARGS="$ARGS -m $OPTARG"
  60.         ;;
  61.     x)
  62.         ARGS="$ARGS -x $OPTARG"
  63.         ;;
  64.     h)
  65.         usage
  66.         exit 0
  67.         ;;
  68.     ?)
  69.         exit 1
  70.     esac
  71. done
  72. shift $((OPTIND - 1))
  73.  
  74. if [ -z "$1" ]; then
  75.     usage >&2
  76.     exit 1
  77. fi
  78.  
  79. set -u
  80.  
  81. FILE="$1"
  82. shift
  83.  
  84. # Don't overwrite an existing BPM tag
  85.  
  86. case "$FILE" in
  87. *.flac)
  88.     BPM=`metaflac --show-tag=BPM "$FILE" | sed -e 's/BPM=//'`
  89.     ;;
  90. *.mp3)
  91.     BPM=`id3v2 -l "$FILE" | sed -n 's/^TBPM.*: \([0-9\.]\+\)/\1/p'`
  92.     ;;
  93. *.ogg)
  94.     BPM=`vorbiscomment "$FILE" | sed -n 's/^BPM=//p'`
  95.     ;;
  96. *.m4a)
  97.     BPM=`mp4info "$FILE" | sed -n 's/^\s*BPM://p'`
  98.     ;;
  99. *)
  100.     echo "$FILE: file extension not known" >&2
  101.     exit 1
  102.     ;;
  103. esac
  104.  
  105. if [ -n "$BPM" ] && ! $FORCE; then
  106.     echo "$FILE: already tagged, $BPM BPM" >&2
  107.     exit 1
  108. fi
  109.  
  110. # Analyse the BPM
  111.  
  112. BPM=`sox -V1 "$FILE" -r 44100 -e float -c 1 -t raw - | bpm $ARGS`
  113. if [ -z "$BPM" ]; then
  114.     exit 1
  115. fi
  116.  
  117. echo "$FILE: $BPM BPM" >&2
  118.  
  119. if ! $WRITE; then
  120.     exit 0
  121. fi
  122.  
  123. # Write a BPM tag
  124.  
  125. case "$FILE" in
  126. *.flac)
  127.     metaflac --remove-tag=BPM --set-tag="BPM=$BPM" "$FILE"
  128.     ;;
  129. *.mp3)
  130.     id3v2 --TBPM "$BPM" "$FILE"
  131.     ;;
  132. *.ogg)
  133.     vorbiscomment -at "BPM=$BPM" "$FILE"
  134.     ;;
  135. *.m4a)
  136.     mp4tags -tempo "$BPM" "$FILE"
  137.     ;;
  138. *)
  139.     echo "$FILE: don't know how to tag this type of file" >&2
  140.     exit 1
  141. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement