Register
It is currently Thu Oct 23, 2014 3:58 pm

id3tag script request


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Aug 11, 2009 3:32 pm   

Joined: Tue Aug 11, 2009 2:41 pm
Posts: 1
I'm pretty new to bash scripting so some of the stuff is simple, most aren't.

What I'm looking for is a script that will read all my mp3 tags within a directory using id3info and rewrite them with a predefined format using id3tag.

What I do is move a group of mp3s to a directory named couch'compilation or such, then I have to manually change the id3 tags on each one to include the artist in the title, rename the album to the directory name and change the artist to me and strip the current track number.

What I've got so far pretty much doesn't do anything.
Code:
#!/bin/bash -
#
# convert title to artist - title
#

function year {
   id3info "$i" | grep TYER | cut -d":" -f2 | sed 's§ §§'
   }
function genre {
   id3info "$i" | grep TCON | cut -d":" -f2 | sed 's§ §§'
   }
function title {
   id3info "$i" | grep TIT2 | cut -d":" -f2 | sed 's§ §§'
   }
function artist {
   id3info "$i" | grep TPE1 | cut -d":" -f2 | sed 's§ §§'
   }
function album {
   id3info "$i" | grep TALB | cut -d":" -f2 | sed 's§ §§'
   }
function track {
   id3info "$i" | grep TRCK | cut -d":" -f2 | sed 's§ §§'
   }
function bit {
   id3info "$i" | grep Bitrate | cut -d":" -f2 | sed 's§ §§'
   }
function freq {
   id3info "$i" | grep Frequency | cut -d":" -f2 | sed 's§ §§'
   }

for i in *.mp3
do
nice -n 19 id3tag -2 --song='$artist" - "$title' "$i"
done



and the output from id3tag and id3info

Code:
couch[test]$ id3info *.mp3

*** Tag information for 01 The Look of Love.mp3
=== TYER (Year): 0
=== TCON (Content type): New Wave
=== TIT2 (Title/songname/content description): The Look of Love
=== TPE1 (Lead performer(s)/Soloist(s)): ABC
=== TALB (Album/Movie/Show title): The Ultimate hits of the 80's
=== TRCK (Track number/Position in set): 1/109
*** mp3 info
MPEG1/layer III
Bitrate: 160KBps
Frequency: 44KHz

couch[test]$ id3tag --help
id3tag
Usage: id3tag [OPTIONS]... [FILES]...
   -h         --help            Print help and exit
   -V         --version         Print version and exit
   -1         --v1tag           Render only the id3v1 tag (default=off)
   -2         --v2tag           Render only the id3v2 tag (default=off)
   -aSTRING   --artist=STRING   Set the artist information
   -ASTRING   --album=STRING    Set the album title information
   -sSTRING   --song=STRING     Set the title information
   -cSTRING   --comment=STRING  Set the comment information
   -CSTRING   --desc=STRING     Set the comment description
   -ySTRING   --year=STRING     Set the year
   -tSTRING   --track=STRING    Set the track number
   -TSTRING   --total=STRING    Set the total number of tracks
   -gSHORT    --genre=SHORT     Set the genre
   -w         --warning         Turn on warnings (for debugging) (default=off)
   -n         --notice          Turn on notices (for debugging) (default=off)


I really don't know what I've done wrong.
Any help would be very appreciated


Top
 Profile  
 PostPosted: Mon Oct 05, 2009 6:39 pm   

Joined: Mon Oct 05, 2009 6:31 pm
Posts: 6
here's a script I wrote awhile ago that accomplishes what you would want, though it uses different applications

Code:
#!/bin/bash

# SCRIPT NAME: id3name
# FUNCTION: renames MPEG1 Layer III files based on ID3 tag info if present
# DEVELOPED BY - GD Lewis (rapskat@gmail.com)
# CREATION DATE - Tue Aug  10 21:43:15 EDT 2004
# REVISION NUMBER - 0.1
# REVISION DATE -
# DEVELOPMENT ENVIRONMENT - Linux hotbox 2.4.22-10mdksmp #1 SMP i686 GNU bash, version 2.05b
# COMMENTS -
# NOTE - This script was developed on a Linux system and only tested on other Linux systems.
# In theory, it should be able to run on any system that can run bash shell scripts, but
# I don't know for sure.  If anyone tries it let me know how it turns out.
#
#                                          REVISION HISTORY
#
# 0.1 - Renames MP3 files based on ID3 tag info
#          Used mp3info to read tags.
#
#  Specify the full path for sort directories here or leave
#  blank to default to the same path as the source files...
OUTDIR=

#  Setting this option to 1 will attempt to sort the resulting mp3 files by the ID3
#  tag data within subdirectories created using this information if it is present.
#  Sort sub-directories are created in the order of genre/artist/album
#  All spaces, caps and punctuation are removed from directory names
#  DO NOT CHANGE THIS IF YOU WANT THIS SCRIPT TO ACTUALLY DO ANYTHING!
SORTSRT=1

# ======================= FUNCTION SECTION =============================
printHelp() {
     echo "    SCRIPT NAME: id3tag"
     echo "    FUNCTION: Sets ID3 tag info based on MP3 filenames"
     echo "    EXPECTED USAGE: id3tag /path/to/mp3 files"
     echo "    RETURNS: none"
     echo "    DEPENDANCIES: tagmp3, mp3info, file, gawk, sed, tr, bc, cut, dirname, basename, bash"
     echo "    INPUT FILES: as specified"
     echo "    OUTPUT FILES: as specified"
     echo " "
     echo "    OPTIONS: "
     echo -e "    -h | --help\t\t Print this help and exit."
     echo ""
     echo "    EXAMPLES:"
     echo "    `basename "$0"` ~/media/foo.mp3"
     echo "    This would rename foo.mp3 "
     echo ""
     echo "    `basename "$0"` ~/albums/*.mp3"
     echo "    This would rename all mp3 files in the albums directory"
     echo ""
}


chk4progs() {
     CHKCNT=0
     NOTFNDCNT=0
     echo -e "Checking system for necessary programs\c"
     for prog in file mp3info gawk tr sed bc cut dirname basename ; do
          if [ -z "`which $prog 2> /dev/null`" ]; then
               NOTFNDCNT=$(($NOTFNDCNT+1))
               NOTFND[$NOTFNDCNT]="$prog"
          fi
         CHKCNT=$(($CHKCNT+1))
         if [ $CHKCNT = 3 ] ; then
               echo -e ".\c"
               CHKCNT=0
         fi
     done
     if [ $NOTFNDCNT -gt 0 ] ; then
          echo "uh oh, you've got issues!"
          echo " "
          echo "    $NOTFNDCNT program(s) that are needed by this script were not found:"
          echo " "
          echo "              ${NOTFND[@]}"
          echo " "
          echo "    Install these program(s), and then try running this script again."
          echo "    If you think the program(s) are installed, then check your "\$PATH" variable"
          echo "    to make sure it lists the location that the binaries are in"
          echo "    and also that you have proper permissions to execute the program(s)."
          echo " "
          echo "Execution cannot continue, sorry."
          exit 1
     else
          echo "it's all good!"
     fi
}


ismp3() {
   MP3OK=0
   if file -bn "$1" | grep "MPEG ADTS, layer III" ; then
      MP3OK=1
   elif file -bn "$1" | grep "MP3" ; then
      MP3OK=1
   fi
}

get_trackinfo() {
     #TRACKINFO=`mp3info -p "%a@%c@%g@%l@%n@%t@%y@%r@%Q@%o" $1`
     TARTIST=`mp3info -p "%a" $1`
     TGENRE=`mp3info -p "%g" $1`
     TTITLE=`mp3info -p "%t" $1`
}

get_fileinfo() {
   FARTIST=`echo "$1" | cut -f1 -d"-" | tr  "_" ' '`
   FTRACK=`echo "$1" | cut -f2 -d"-" | tr  "_" ' '`
}

stripspc_caps() {
     OLDIFS="IFS"
     IFS=:
     MOD_NAME=`echo "$1" | gawk 'BEGIN { FS="@"} { print tolower($1) };' | tr '[:blank:]' '_'`
     IFS="$OLDIFS"
     return
}

strippunc() {
     MOD_NAME=`echo "$1" | gawk '{ if ( $1 ~ /^[[:punct:]]/) {print substr($1, 2) ;} else { print $1 ;}}' | tr '!?":;\[\]\\\/{}(),' '.' | sed -e s/\&/and/g | sed -e s/\'//g | sed -e s/_w_/_and_/g | tr -s '[:punct:]'`
     return
}

# ======================== END FUNCTION SECTION ========================

# ==================== VARIABLE DECLARATION SECTION ====================

TRIED=0
SUCCESS=0
FAILED=0
SKIPPED=0
TRACKINFO=
TARTIST=
TCOMMENT=
TGENRE=
TALBUM=
TRACKNUM=
TTITLE=
TDATE=
TBITRATE=
TQUAL=
MOD_NAME=
TRACK=
SRCTRACK=
SRTTRACK=

# ================ END VARIABLE DECLARATION SECTION ==============

# +--------------------------------------------------------------------------------------------------+
# +                                            BEGIN SCRIPT                                                      +
# +--------------------------------------------------------------------------------------------------+
chk4progs
echo ""
echo "                                 -=-=MP3 RENAMING SCRIPT=-=-"


if [ $# -lt 1 ]; then
     echo "    EXPECTED USAGE: id3name /path/to/[mp3 file(s)]"
     echo "    Use -h or --help to print help."
     echo "No files specified...exiting."
     exit 1
fi

echo "--> $# filename(s) given..."

for TRACK in "$@"
do
    ismp3 "$TRACK"
    if [ $MP3OK -eq 1 ] ; then
          SRCDIR=`dirname "$TRACK"`
          SRCTRACK=`basename "$TRACK"`
          get_trackinfo "$TRACK"
          if [ -n "$TTITLE" ] && [ -n "$TARTIST" ] ; then
      ID3NAME="$TARTIST"'_-_'"$TTITLE"".mp3"
# echo "$ID3NAME"
          else
               echo "--> $SRCTRACK has insufficient tag info...skipping"
               SKIPPED=$((SKIPPED += 1))
               continue
          fi
          SORTTRACK="$SRCDIR/$ID3NAME"
          if [ -f "$SORTTRACK" ] ; then
               echo "--> $SRCTRACK appears to have already been renamed...skipping"
               SKIPPED=$((SKIPPED += 1))
               continue
          else
               echo -e "--> Renaming $SRCTRACK to $ID3NAME...\c"
               TRIED=$((TRIED += 1))
               if mv "$TRACK" "$SORTTRACK" 2> /dev/null ; then
                    SUCCESS=$((SUCCESS += 1))
                    echo "ok."
               else
                    FAILED=$((FAILED += 1))
                    echo "failed!"
               fi
          fi
     else
          echo "--> $TRACK does not appear to be an mp3 file...skipping"
          SKIPPED=$((SKIPPED += 1))
          continue
     fi
done

echo "Operation completed..."
echo "Final Stats -- Skipped: $SKIPPED  Attempted: $TRIED  Renamed: $SUCCESS  Failed: $FAILED"
exit

# +------------------------------------------------- END SCRIPT ----------------------------------------+


I know, it's kind of verbose, but it works well for me anyway. Let me know.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP