bashshellid3lib

Get MP3 ID3 (V2) tags using id3v2


I'm having a little difficulty understanding the format of V2 ID3 tags. I simply want to get the 'Genre' ID3 tag of an MP3 file, but the id3v2 utility returns more information than I require, and I don't understand what this data represents.

For example, I'm using the following bash code:

id3v2 -l "$FILE" | grep -i "content type"

Which returns something on the lines of:

TCON (Content type): Jazz (8)

Really I just want to know if it's safe for me to assume this will always be the format, and thus is it safe for me to do the following?:

id3v2 -l "$FILE" | grep -i "content type" | sed "s/^.*: //" | sed "s/ (.*$//"

(Which would result in "Jazz".)

I'm trying to find the best way to get the ID3 'Genre' tag. Many thanks for any suggestions.


Solution

  • I think it is safe to assume that the line will be prefixed with TCON, so I would rather search for that (this will be i18n safe as well).

    id3v2 -l "$FILE" | sed -n '/^TCON/s/^.*: //p' | sed 's/ (.*//'
    

    (no need to run grep)


    From /usr/share/doc/id3lib-3.8.3/doc/id3v2.3.0.txt

    TCON

    The 'Content type', which previously was stored as a one byte numeric value only, is now a numeric string. You may use one or several of the types as ID3v1.1 did or, since the category list would be impossible to maintain with accurate and up to date categories, define your own.

    References to the ID3v1 genres can be made by, as first byte, enter "(" followed by a number from the genres list (appendix A.) and ended with a ")" character. This is optionally followed by a refinement, e.g. "(21)" or "(4)Eurodisco". Several references can be made in the same frame, e.g. "(51)(39)". If the refinement should begin with a "(" character it should be replaced with "((", e.g. "((I can figure out any genre)" or "(55)((I think...)". The following new content types is defined in ID3v2 and is implemented in the same way as the numerig content types, e.g. "(RX)".

     RX  Remix
     CR  Cover