Thursday, 24 December 2009

add_mp3info.sh - Retrospectively add mp3 info into mp3 files.

cat /home/jamesc/bin/add_mp3info.sh
#!/bin/bash

# Retrospectively add mp3 info into mp3 files.
# Based on directory name and file name.


# usage:
#~/mp3$ add_mp3info.sh */*/*.mp3
#~/mp3$ add_mp3info.sh -f therapy/*/*.mp3
# WARNING: doesn't work for other directory levels
# TODO: oops, doesn't work if leading ./ or anything leading.
# BUT: is safe enough, it won't overwrite existing mp3info (unless -f FORCE flag is passed)

# e.g.
# artist_name/album_name/01-track_title.mp3 does not have an ID3 1.x tag.
#find . -name *.mp3 -exec mp3info {} \; |less


FORCE=$1
if [[ "$FORCE" == "-f" ]] ; then echo foo; shift; fi
#for f in tom_waits/*/*; do
for f in $*; do
 info=$(mp3info "$f")
 if [[ "$FORCE" == "-f" || "$info" == "" ]] ; then
  echo "Adding info to file:$f"
  artist=;album=;title=;track=;
  artist=${f%%\/*.mp3}
  rest=${f#*\/}
  album=${rest%%\/*.mp3}
  rest=${rest#*\/}
  title=${rest%%.mp3}
  title=${title##*-}
  track=${rest%%-*}
  title=$(echo $title|sed "s/_\([a-z]\)/ \U\1/g" |sed "s/^\([a-z]\)/\U\1/")
  artist=$(echo $artist|sed "s/_\([a-z]\)/ \U\1/g" |sed "s/^\([a-z]\)/\U\1/")
  album=$(echo $album|sed "s/_\([a-z]\)/ \U\1/g" |sed "s/^\([a-z]\)/\U\1/")
  params=""
  #[[ "$title" != "" ]] && params="$params -t \"\$title\""
  mp3info -t "$title" -l "$album" -a "$artist" -n "$track" -c "badgers are lovely" "$f"
 fi
done


# TODO: oops, doesn't work if leading ./ or anything leading.
#/home/jamesc/bin/add_mp3info.sh: 24: [[: not found
# so this won't work:  find . -name "*.mp3" -exec add_mp3info.sh {} \;

# to get meta info from music/video files generally (if it is there): exiftool

No comments: