Evyn has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to change the ALBUM and GENRE tag values in mp3 files. My code changes ALBUM but does not change GENRE. Could you perhaps assist me in getting it to change GENRE too?
use strict; use warnings; use MP3::Info; use Time::Piece; my $date = localtime->strftime('%Y/%m/%d'); my $file = 'file.mp3'; my $tag->{ALBUM} = $date; # Change album to today;s date set_mp3tag($file, $tag); $tag->{GENRE} = "Audiobook"; # Change genre to Audiobook set_mp3tag($file, $tag); my $info = get_mp3info($file); printf "$file length is %d:%d\n", $info->{MM}, $info->{SS};
When I look at the mp3 file in mp3tag ( http://www.mp3tag.de/ ), I can change the GENRE without a problem. Mp3tag tells me the tags are: IDv2.3 (ID3v1 ID3v2.3). When I run the code below to see if I can see what tags actually appear in the mp3, I cannot find the actual genre tag value that appears in mp3tag. That is, if I change GENRE to Audiobook in mp3tag, the word Audiobook does not appear in the output of this code below.
use MP3::Tag; use warnings; use strict; use Data::Dumper; open FILE, ">mp3.txt" or die $!; my $file = 'file.mp3'; my $mp3 = MP3::Tag->new($file); $mp3->get_tags(); print FILE Dumper($mp3);
Thanks for your time.

Replies are listed 'Best First'.
Re: use MP3::Info to change .mp3 genre
by RichardK (Parson) on Jan 31, 2015 at 14:11 UTC

    You can't set GENRE to an arbitrary string, if you look at the docs MP3::Info for set_mp3tag it says :-

    Fields are TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE. All fields have a 30-byte limit, except for YEAR, which has a four-byte limit, and GENRE, which is one byte in the file. The GENRE passed in the function is a case-insensitive text string representing a genre found in @mp3_genres.

    Have a look at the source, or dump @mp3_genres, to see the allowable values.

    Sadly the genre tag for MP3s is really poor, they just didn't design their data model properly, if you want a new category you're just out of luck.

      Thanks, Richard. So my next question then is, what is the mp3tag application doing that does change the GENRE to some arbitrary value? And can I replicate this in Perl some other way?

        In that case, you need to find a way to write id3v2 extended tags, but MP3::Info doesn't support that yet.

Re: use MP3::Info to change .mp3 genre
by poj (Abbot) on Jan 31, 2015 at 15:54 UTC
    Try
    #!perl use strict; use MP3::Tag; use MP3::Info; use Time::Piece; use Data::Dump 'pp'; my $date = localtime->strftime('%Y/%m/%d'); my $file = 'file.mp3'; my $info = get_mp3tag($file); pp $info; my $mp3 = MP3::Tag->new($file); $mp3->album_set($date); $mp3->genre_set('Audiobook'); $mp3->update_tags(); $info = get_mp3tag($file); pp $info;
    poj
      Thanks - that works. Fantastic.