LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with an id3tag script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-an-id3tag-script-747312/)

couchquail 08-13-2009 11:58 AM

Help with an id3tag script
 
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(a 100 or so) 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

David the H. 08-13-2009 01:01 PM

Do you really want to change the song title to include both the artist and title information? Why?

Well, for one thing this will not work:
Code:

id3tag -2 --song='$artist" - "$title' "$i"
Single quotes make everything inside them literal. So you're setting the title of the song to the literal string |$artist" - "$title|. You need to use double quotes only around variables, which still allow the values to be substituted.

(Unlike single quotes, double quotes allow $, ` and \ to be interpreted, which allows for variable and command substitutions and escape sequences.).

Actually, looking again, you're not even using variables, but functions. Functions aren't called by "$name", just "name" alone. So what you really need to be using is $(command) substitution:
Code:

id3tag -2 --song="$(artist) - $(title)" "$i"
Next, you're doing a lot of unecessary work to process the fields in your functions. Sed can do it all by itself. Try this:

Code:

id3info "$i"|sed -rn '\|TALB| s|.*:[ ](.*)$|\1|p'
You can learn more about sed here.


Finally, I've been working on something similar (but more complex) myself for the last week, and I've found that working with music metadata is a real pain, particularly when trying to work with multiple formats. I don't see id3info/id3tag available on Debian, but for mp3 files I've had better success with mid3v2, the python-based tag reader. It has a much easier input and output format for using in shell scripts. You might give it a try.


Edit: One more thing I just noticed. You're simultaneously reading information from and writing to the same file. I'm not sure that's safe. It would be better for you to read the tag information into variables first and then write out the new values only after you have everything extracted.

couchquail 08-13-2009 03:19 PM

Thanks alot
Your
Code:

id3tag -2 --song="$(artist) - $(title)" "$i"
worked like a charm.

Why do I do this?
My mp3 player(Philips GoGear 4gb) doesn't do playlists very well. When I do it this way it reads the title of the mp3 which now includes the artist. I can also change 'playlists' by selecting a new album.


All times are GMT -5. The time now is 09:44 AM.