Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

I need help about MP3::Tag and perl script.

by osmy (Initiate)
on Mar 30, 2011 at 00:32 UTC ( [id://896303]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone, I am a newbie on linux and just searching everything about perl scripting and modules nearly 3-4 days. I need a perl script but one of not easy to find on searching google. Okey now I need a perl script which create or recreate (edit) id3 tags (artist,comment,album,year,cover) of mp3 files stored on my linux centos server. I installed MP3::Tag version 1.13 pearl module to my server. I Searched tutorials about how to use it, finally I get through reading id3 tag of mp3 file but not achieve to modify it or create a new id3tag. So I need your help and knowledge about scripting.
These are the details: I have a mp3 file called 1.mp3 this script will process that '1.mp3' file read its id3tag if there is one, than modify or create id3tag for it by my fix artist name for example: '1.mp3' files id3 tag details are like this
Artist: Dj xx
Year:2010
Title:yyyyy
Comment:eerwer
Cover: x.jpg

now via this perl script which uses MP3::TAG I will change it's
artist as YYY
Title:ttt
Comment:cool
Cover:t.jpg
these are gonna be my fixed values. I mean all '1.mp3' file will have same artist based on script value.
The reason of this script is I will share Dj podcasts on my server and Dj's would have upload their mp3's which has got different id3 tags and cover pics. etc. I want to create more organized podcasts of them by the way I would trigger this perl script via Cronjob.
Thank you for your help and sorry for taking up your time

Here is the code which I try:
#!/usr/bin/perl
use MP3::Tag;
$mp3 = MP3::Tag->new('1.mp3'); # create object
$mp3->get_tags(); # read tags
if (exists $mp3->{ID3v1}) {
$mp3->{ID3v1}->title("ttt");
$mp3->{ID3v1}->artist("YYY");
$mp3->{ID3v1}->album("zzz");
$mp3->{ID3v1}->year("2010");
$mp3->{ID3v1}->write_tag();
}
$mp3->close(); # destroy object
Waiting your answers!
  • Comment on I need help about MP3::Tag and perl script.

Replies are listed 'Best First'.
Re: I need help about MP3::Tag and perl script.
by jwkrahn (Abbot) on Mar 30, 2011 at 00:41 UTC
      I got v1 too. But my perl script not working? Any idea? Thanks.
Re: I need help about MP3::Tag and perl script.
by Popcorn Dave (Abbot) on Mar 30, 2011 at 01:54 UTC
    Are you sure you have data in $mp3 to begin with? That's the first place I'd check. Make sure you're actually reading the tag.

    If you've got an ID3v2 tag only you may not have any data in $mp3.


    To disagree, one doesn't have to be disagreeable - Barry Goldwater

      It's Id3tag v2. I want to change all Id3tags (v1 and v2) tag automatically with this script. But how =(
        not tested, just quick look into doc:
        $id3v1 = exists $mp3->{ID3v1} ? $mp3->{ID3v1} : $mp3->new_tag("ID3v1")
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: I need help about MP3::Tag and perl script.
by hawtin (Prior) on Mar 30, 2011 at 13:03 UTC

    Oh, and cover art is a whole different challenge. Here's how I do it (for ID3v2 and jpg images).

    Update: This defines a new subroutine called attach(). The first parameter has no affect (its important in my script but for your purposes you can ignore it). Suppose you want to attach a file "foo/cool.jpg" to an mp3 file called "1.mp3" as the front cover, you would just do (untested)

    attach("","1.mp3","foo/cool.jpg");

    This, of course, requires that you add the appropriate packages and copy the definition of the subroutine somewhere into your script.

      if(!-w $mp3_file) { # This shouldn't happen but... chmod 0755,$mp3_file; }

      Why do you try to make an MP3 file world executable if you can't read it? And why do you make it readable for group and world? Why do you even think about changing the mode of files that are not writeable?

      There are usually good reasons for chmod -w somefile and chmod go-rw somefile: I don't want to accidentally modify a file in the first case, and I don't want others to read my files in the second case. Your hardcoded chmod 0755 ignores both. To make things worse, it sets the executable flag, possibly creating a new vulnerability.

      I won't start with race conditions between stat (implied in -w) and open, because MP3::Tag has the same problem, so your code does not make it worse in this special case. Generally, one should not try to predict if open fails or succeeds by using stat. It can't work reliably (race condition), and it is not needed. open sets errno a.k.a. $! on error, and errno delivers sufficient information why open failed.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        First of all thank you very much about sharing your experiences with me. But could you please try a simple method cuz I do not understand anything at all I am learning perl now. So If you wrilte down the script again with my needs that I mention before. It wil be awesome and I would learn one thing about perl too. Waiting your support..
      wow that's awesome thanks. But I don't know where to edit the code. I understand logically but not enough knowledge. I try to be simple. Where should I write file name artist album song title cover picture name and location. for example: 1.mp3 will be the mp3 file imported and edited , these are the fixed edits
      song title is : xyz
      artist name:xx
      album:tt
      cover picture: /cool.jpg so where should I write these datas on your script. Remember I will set a cron and run this script so mp3 file on my server will have id3tag changed or edited by me more clear and tidy mp3 =)
Re: I need help about MP3::Tag and perl script.
by hawtin (Prior) on Mar 30, 2011 at 11:31 UTC

    One obvious issue would be if you don't have write permission on the file. Check with

    use Carp; my $file = '1.mp3'; croak("Cannot write to file $file") if(!-w $file);

    You might like to check some of the resources here, for example Fixing mp3 tags for Android 2.0 contains a tested script that uses MP3::Tag to set values in pretty much the way you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://896303]
Approved by chrestomanci
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-19 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found