dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

audiotag (4333B)


      1 #!/bin/sh
      2 # A wrapper for audio taggers for different formats
      3 printfq() { printf "'%s'\n" "$(printf '%s' "$1" | sed -e "s/'/'\\''/g")"; }
      4 die() { printf '%s\n' "$1" >&2 && exit 1; }
      5 
      6 FILES=""
      7 
      8 while [ $(($#)) -ne 0 ]; do
      9   case "$1" in
     10     -b|--band)
     11       band="$2"
     12       shift 2
     13       ;;
     14     -t|--title)
     15       title="$2"
     16       shift 2
     17       ;;
     18     -l|--album)
     19       album="$2"
     20       shift 2
     21       ;;
     22     -r|--artist)
     23       artist="$2"
     24       shift 2
     25       ;;
     26     -n|--number)
     27       number="$2"
     28       shift 2
     29       ;;
     30     -i|--image)
     31       image="$2"
     32       shift 2
     33       ;;
     34     -x|--extract)
     35       spec="$2"
     36       shift 2
     37       ;;
     38     -c|--clear)
     39       clear=1
     40       shift
     41       ;;
     42     -h|--help)
     43       printf 'Supported usage\n'
     44       printf '%s -b albumartist -t title -l album -r artist -n tracknumber -i IMG_PATH:DESCRIPTION \n' "$0"
     45       printf '%s [--extract|-x] "filename_spec" FILE\n' "$0"
     46       printf '%s [--clear|-c] FILE\n' "$0"
     47       exit 0
     48       ;;
     49     --) # end arg parsing
     50       shift
     51       break
     52       ;;
     53     -*) # unsupported flags
     54       echo "Unsupported flag $1" >&2
     55       exit 1
     56       ;;
     57     *)
     58       if [ -z "$FILES" ]; then
     59         FILES="$1"
     60       else
     61         FILES="$FILES$(printf '\n%s' "$1")"
     62       fi
     63       shift
     64       ;;
     65   esac
     66 done
     67 
     68 [ -z "$FILES" ] && die 'Need a file to operate on.'
     69 
     70 printf '%s\n' "$FILES" | while IFS='' read -r file; do
     71   [ -f "$file" ] || die "File $file not readable."
     72 
     73   # Clear params, will rebuild them per command
     74   set --
     75 
     76   case "$file" in
     77     *.mp3)
     78       if [ -n "$clear" ]; then
     79         command -v eyeD3 >/dev/null 2>&1 || die 'eyeD3 must be installed.'
     80         eyeD3 --remove-all "$file"
     81       elif [ -n "$spec" ]; then
     82         command -v taffy >/dev/null 2>&1 || die 'taffy must be installed.'
     83         taffy --extract "$spec" "$file"
     84       else
     85         command -v eyeD3 >/dev/null 2>&1 || die 'eyeD3 must be installed.'
     86         set -- --to-v2.4
     87         [ -n "$band" ] && set -- "$@" -b "$band"
     88         [ -n "$title" ] && set -- "$@" -t "$title"
     89         [ -n "$album" ] && set -- "$@" -A "$album"
     90         [ -n "$artist" ] && set -- "$@" -a "$artist"
     91         [ -n "$number" ] && set -- "$@" -n "$number"
     92 
     93         if [ -n "$image" ]; then
     94           img_path="${image%%:*}"
     95           img_desc="${image##*:}"
     96           set -- "$@" --add-image "${img_path}:FRONT_COVER:${img_desc}"
     97         fi
     98         eyeD3 "$@" "$file"
     99       fi
    100       ;;
    101     *.opus)
    102       command -v ffmpeg >/dev/null 2>&1 || die 'ffmpeg must be installed.'
    103       if [ -n "$clear" ]; then
    104         tempdir="$(mktemp -d)"
    105         ffmpeg -i "$file" -map_metadata -1 -codec copy "$tempdir/output.opus" \
    106           && mv "$tempdir/output.opus" "$file" \
    107           && rmdir "$tempdir"
    108       elif [ -n "$spec" ]; then
    109         die 'Extracting from opus filename not yet supported.'
    110       else
    111         # See https://exiftool.org/TagNames/Vorbis.html#Comments
    112         set -- -i "$file" -codec copy
    113         [ -n "$band" ] && set -- "$@" -metadata ALBUMARTIST="$band"
    114         [ -n "$title" ] && set -- "$@" -metadata TITLE="$title"
    115         [ -n "$album" ] && set -- "$@" -metadata ALBUM="$album"
    116         [ -n "$artist" ] && set -- "$@" -metadata ARTIST="$artist"
    117         [ -n "$number" ] && set -- "$@" -metadata TRACKNUMBER="$number"
    118 
    119         tempdir="$(mktemp -d)"
    120         ffmpeg "$@" "$tempdir/output.opus" \
    121           && mv "$tempdir/output.opus" "$file" \
    122           && rmdir "$tempdir"
    123       fi
    124       ;;
    125     *.flac)
    126       command -v metaflac >/dev/null 2>&1 || die 'metaflac must be installed.'
    127       if [ -n "$clear" ]; then
    128         metaflac --remove-all-tags "$file"
    129       elif [ -n "$spec" ]; then
    130         die 'Extracting from flac filename not yet supported.'
    131       else
    132         [ -n "$band" ] && set -- "$@" --set-tag=ALBUMARTIST="$band"
    133         [ -n "$title" ] && set -- "$@" --set-tag=TITLE="$title"
    134         [ -n "$album" ] && set -- "$@" --set-tag=ALBUM="$album"
    135         [ -n "$artist" ] && set -- "$@" --set-tag=ARTIST="$artist"
    136         [ -n "$number" ] && set -- "$@" --set-tag=TRACKNUMBER="$number"
    137 
    138         if [ -n "$image" ]; then
    139           img_path="${image%%:*}"
    140           img_desc="${image##*:}"
    141           set -- "$@" --import-picture-from="3||${img_desc}||${img_path}"
    142         fi
    143 
    144         metaflac "$@" "$file"
    145       fi
    146       ;;
    147     *)
    148       die 'File format not (yet) supported.';;
    149   esac
    150 
    151 done