Monday, 19 September 2011

handy script to add mp3 title/info to mp3 files. add_mp3info.sh


I am listening to Irish CD that Kate brought home and fixing up some .mp3 file info. Using this script I made earlier (25/12/2009).

#!/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





A couple of other handy snippets:

# fill in fixed  mp3 info into file, increment track number as you go.
track=1
function mrfle1 {
title="$TTITLE"
f=$t
mp3info -t "$title" -l "$album" -a "$artist" -n "$track" -c "is maith linn broc" "$f"
track=$((track+1))
}

# rename track files according to title (replacing non-alpha-numerics for thefile name)
function mrfle {
f=$(echo "$TTITLE"|tr "[:punct:][:space:]" "__").mp3
mv $t $f
}


No comments: