Advertisement
Guest User

mp3reclaimer by Ri

a guest
Jun 27th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # mp3reclaimer
  4. # retrieves mp3 files stored on ipods
  5. # and renames them based on their id3 tags
  6.  
  7. # created 2015-06-26 Ri
  8. # modified 2015-06-26 Ri
  9.  
  10. # set variables
  11. myname=`basename $0`
  12. ipodVolumePath=
  13. ipodMusicPath="iPod_Control/Music"
  14. outputDir=`pwd`
  15.  
  16. # declare functions
  17. error() # error handler
  18. {
  19.     echo "$myname: $1" 1>&2
  20. }
  21.  
  22. check_path() # checks if supplied argument is a valid path
  23. {
  24.     if [ ! -d "$1" ] && [ ! -f "$1" ]; then
  25.         error "invalid path $1"
  26.         exit 1
  27.     fi
  28. }
  29.  
  30. get_id3_tag() # gets id3 tags on supplied file
  31. {
  32.     id3v2 --list "$1" |
  33.     grep -e "^$2" | awk 'BEGIN { FS=": " } ; { print $2 }'
  34. }
  35.  
  36. # parse options and arguments
  37. while [ $# -gt 0 ]; do
  38.     case $1 in
  39.         -o|--outputdir)
  40.             outputDir="$2"
  41.             shift 2
  42.             ;;
  43.         -*|--*)
  44.             error "invalid option $1"
  45.             exit 1
  46.             ;;
  47.         *)
  48.             ipodVolumePath="$1"
  49.             shift
  50.             ;;
  51.  
  52.     esac
  53. done
  54.  
  55. # perform sanity check
  56. check_path "$ipodVolumePath"
  57. check_path "$ipodVolumePath/$ipodMusicPath"
  58. check_path "$outputDir"
  59.  
  60. # execute
  61. for f in "$ipodVolumePath/$ipodMusicPath"/**/*.mp3; do
  62.    
  63.     artist=`get_id3_tag "$f" "TP1"`
  64.     if [ "X$artist" = "X" ]; then
  65.         artist="Unknown Artist"
  66.     fi
  67.  
  68.     artist=`echo "$artist" | LANG=C sed 's/:/;/g'`
  69.     artist=`echo "$artist" | LANG=C sed 's/\//:/g'`
  70.  
  71.     title=`get_id3_tag "$f" "TT2"`
  72.     if [ "X$title" = "X" ]; then
  73.         title="Unknown Title"
  74.     fi
  75.    
  76.     title=`echo "$title" | LANG=C sed 's/:/;/g'`
  77.     title=`echo "$title" | LANG=C sed 's/\//:/g'`
  78.  
  79.     newfilename="$artist - $title"
  80.     cp -v "$f" "$outputDir/$newfilename.mp3"
  81.    
  82. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement