aboutsummaryrefslogtreecommitdiff
path: root/bin/envtag
blob: 1e230e91a32f945170c18048b28c2760751ecb34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh

usage() {
    cat >&2 <<EOF
usage: ${0##*/} [-0] files...
EOF
    exit 69
}

filter() {
    sed -E \
        -e '/^stream_[0-9]+_tags/ s/^stream_[0-9]+_tags_//' \
        -e '/^format_tags_/ s/^format_tags_//   #format tags = ID3, stream_tags = else' \
        -e 's/^([^=]+)=/\L\1=\$\(printf -- /    #lowercase variable names, escape for printf' \
        -e 's/$/\)/                             #complete for printf' \
        -e '/^musicbrainz_([^=]+)/ {            #homogenize musicbrainz variables across ID3/Ogg
                s/^musicbrainz_(album|artist|release)_/musicbrainz_\1/
                s/^musicbrainz_([a-z]+)_(country|id)/musicbrainz_\1\2/
            }' \
        | if [ "${list}" -eq 1 ]; then sed 's/=.*//'; else cat; fi \
        | if [ "${null}" -eq 1 ]; then xe -j0 -N0 printf '%s\0'; else cat; fi
}

list=0
null=0
while getopts :0l arg >/dev/null 2>&1; do
    case "${arg}" in
        0) null=1 ;;
        l) list=1 ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND - 1))

for f; do
    ffprobe \
        -loglevel -32 \
        -of flat=sep_char=_:hierarchical=0 \
        -select_streams a \
        -show_entries stream_tags:format_tags \
        -i "${f}" \
        | filter
done
            # -e 's/^([^=]+)=(.*)/&; printf "%s: %s\n" "\1" "$\1"/' \
            # -e 's/\\n/\n/g                  #lastly we unescape the newlines and nothing else' \