Debian Codegraph @

bpm-tag

raw
1 #!/bin/sh
2 #
3 # Copyright (C) 2013 Mark Hills <[email protected]>
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 <[email protected]>
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 -R "$FILE" | sed -n 's/^TBPM.*: \([0-9\.]\+\)/\1/p'`
92 ;;
93 *.ogg)
94 BPM=`vorbiscomment "$FILE" | sed -n 's/^BPM=//p'`
95 ;;
96 *)
97 echo "$FILE: file extension not known" >&2
98 exit 1
99 ;;
100 esac
101
102 if [ -n "$BPM" ] && ! $FORCE; then
103 echo "$FILE: already tagged, $BPM BPM" >&2
104 exit 1
105 fi
106
107 # Analyse the BPM
108
109 BPM=`sox -V1 "$FILE" -r 44100 -e float -c 1 -t raw - | bpm $ARGS`
110 if [ -z "$BPM" ]; then
111 exit 1
112 fi
113
114 echo "$FILE: $BPM BPM" >&2
115
116 if ! $WRITE; then
117 exit 0
118 fi
119
120 # Write a BPM tag
121
122 case "$FILE" in
123 *.flac)
124 metaflac --remove-tag=BPM --set-tag="BPM=$BPM" "$FILE"
125 ;;
126 *.mp3)
127 id3v2 --TBPM "$BPM" "$FILE"
128 ;;
129 *.ogg)
130 vorbiscomment -at "BPM=$BPM" "$FILE"
131 ;;
132 *)
133 echo "$FILE: don't know how to tag this type of file" >&2
134 exit 1
135 esac