"Fossies" - the Fresh Open Source Software Archive

Member "namefix.pl/libs/mp3.pm" (13 Dec 2008, 7702 Bytes) of package /linux/privat/old/namefix.pl_4.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mp3.pm" see the Fossies "Dox" file reference documentation.

    1 # -----------------------------------------------------------------------------------
    2 # mems mp3 funcs
    3 # -----------------------------------------------------------------------------------
    4 
    5 use strict;
    6 #use warnings;
    7 
    8 # -----------------------------------------------------------------------------------
    9 # get tags
   10 # -----------------------------------------------------------------------------------
   11 
   12 sub get_tags 
   13 {
   14     &plog(3, "sub get_tags:");
   15     my $file    = shift;
   16 
   17         my $art1    = "";
   18         my $tit1    = "";
   19         my $tra1    = "";
   20         my $alb1    = "";
   21         my $com1    = "";
   22         my $gen1    = "";
   23         my $year1   = "";
   24 
   25         my $art2    = "";
   26         my $tit2    = "";
   27         my $tra2    = "";
   28         my $alb2    = "";
   29         my $com2    = "";
   30         my $gen2    = "";
   31         my $year2   = "";
   32 
   33     my $mp3 = MP3::Tag->new($file);
   34     $mp3->get_tags();
   35 
   36         if (exists $mp3->{ID3v1}) 
   37     {
   38         $art1 = $mp3->{ID3v1}->artist;
   39         $tit1 = $mp3->{ID3v1}->title;
   40         $tra1 = $mp3->{ID3v1}->track;
   41         $alb1 = $mp3->{ID3v1}->album;
   42         $com1 = $mp3->{ID3v1}->comment;
   43                 $gen1 = $mp3->{ID3v1}->genre;
   44                 $year1 = $mp3->{ID3v1}->year;
   45     }
   46 
   47         if (exists $mp3->{ID3v2}) 
   48     {
   49         $art2 = $mp3->{ID3v2}->getFrame("TPE1");
   50         $tit2 = $mp3->{ID3v2}->getFrame("TIT2");
   51         $tra2 = $mp3->{ID3v2}->getFrame("TRCK");
   52         $alb2 = $mp3->{ID3v2}->getFrame("TALB");
   53         my $com = $mp3->{ID3v2}->getFrame("COMM");
   54                 if($com) 
   55         {
   56             $com2 = $com->{Text};
   57         }
   58                 $gen2 = $mp3->{ID3v2}->getFrame("TCON");
   59                 $year2 = $mp3->{ID3v2}->getFrame("TYER");
   60 
   61                 # sort out which tags are complete, file in missing :)
   62             if($art2 && !$art1) 
   63         {
   64                     $art1 = $art2;
   65                         $main::id3_writeme = 1;
   66             }
   67         elsif(!$art2 && $art1) 
   68         {
   69                         $main::id3_writeme = 1;
   70             }
   71 
   72             if($tit2 && !$tit1) 
   73         {
   74                     $tit1 = $tit2;
   75                         $main::id3_writeme = 1;
   76             }
   77                 elsif(!$tit2 && $tit1) 
   78         {
   79                         $main::id3_writeme = 1;
   80             }
   81 
   82             if($tra2 && !$tra1) 
   83         {
   84                     $tra1 = $tra2;
   85                         $main::id3_writeme = 1;
   86             }
   87                 elsif(!$tra2 && $tra1) 
   88         {
   89                         $main::id3_writeme = 1;
   90             }
   91 
   92             if($alb2 && !$alb1) 
   93         {
   94                     $alb1 = $alb2;
   95                         $main::id3_writeme = 1;
   96             }
   97         elsif(!$alb2 && $alb1) 
   98         {
   99                         $main::id3_writeme = 1;
  100             }
  101 
  102             if($com2 && !$com1) 
  103         {
  104                     $com1 = $com2;
  105                         $main::id3_writeme = 1;
  106             }
  107                 elsif(!$com2 && $com1) 
  108         {
  109                         $main::id3_writeme = 1;
  110             }
  111 
  112             if($gen2 && !$gen1) 
  113         {
  114                     $gen1 = $gen2;
  115                         $main::id3_writeme = 1;
  116             }
  117                 elsif(!$gen2 && $gen1) 
  118         {
  119                         $main::id3_writeme = 1;
  120             }
  121             if($year2 && !$year1) 
  122         {
  123                     $year1 = $year2;
  124                         $main::id3_writeme = 1;
  125             }
  126                 elsif(!$year2 && $year1) 
  127         {
  128                         $main::id3_writeme = 1;
  129             }
  130     }
  131     $mp3->close();
  132 
  133         if (exists $mp3->{ID3v1} || exists $mp3->{ID3v2}) 
  134     {
  135         return("id3v1", $art1, $tit1, $tra1, $alb1, $gen1, $year1, $com1);
  136     }
  137     return("notag");
  138 }
  139 
  140 sub rm_tags 
  141 {
  142     my $file    = shift;
  143     my $opt     = shift;
  144     my $mp3 = MP3::Tag->new($file);
  145 
  146     &plog(3, "sub rm_tags: file = \"$file\", opt = \"$opt\"");
  147         $mp3->get_tags();
  148 
  149         if($opt eq "id3v1" && exists $mp3->{ID3v1}) 
  150     {
  151         &plog(4, "sub rm_tags: removing id3v1");
  152             print "id3v1 tag from $file\n";
  153             $mp3->{ID3v1}->remove_tag();
  154                 $main::tags_rm++;
  155         }
  156         if($opt eq "id3v2" && exists $mp3->{ID3v2}) 
  157     {
  158         &plog(4, "sub rm_tags: removing id3v2");
  159             print "id3v2 tag from $file\n";
  160                 $mp3->{ID3v2}->remove_tag();
  161                 $main::tags_rm++;
  162         }
  163         return;
  164 }
  165 
  166 sub write_tags 
  167 {
  168     my $file = shift;
  169 
  170     my $art = shift;
  171     my $tit = shift;
  172     my $tra = shift;
  173     my $alb = shift;
  174     my $com = shift;
  175         my $gen = shift;
  176         my $year = shift;
  177 
  178     &plog(3, "sub write_tags: \"$file\"");
  179     my $mp3 = MP3::Tag->new($file);
  180 
  181     if (!$mp3->{ID3v1}) 
  182     {
  183         &plog(4, "sub write_tags: id3v1 tag did not exist, creating");
  184         $mp3->new_tag("ID3v1");
  185     }
  186 
  187     if (!$mp3->{ID3v2}) 
  188     {
  189         &plog(4, "sub write_tags: id3v2 tag did not exist, creating");
  190         $mp3->new_tag("ID3v2");
  191     }
  192 
  193     $mp3->get_tags();
  194 
  195     if($art) 
  196     {
  197         &plog(4, "sub write_tags: setting tag artist as \"$art\"");
  198         $mp3->{ID3v1}->artist($art);
  199         $mp3->{ID3v2}->remove_frame("TPE1");
  200                 $mp3->{ID3v2}->add_frame("TPE1",$art);
  201     }
  202     if($tit) 
  203     {
  204         &plog(4, "sub write_tags: setting tag title as \"$tit\"");
  205         $mp3->{ID3v1}->title($tit);
  206                 $mp3->{ID3v2}->remove_frame("TIT2");
  207         $mp3->{ID3v2}->add_frame("TIT2",$tit);
  208     }
  209     if($tra) 
  210     {
  211         &plog(4, "sub write_tags: setting tag Track as \"$tra\"");
  212         $mp3->{ID3v1}->track($tra);
  213                 $mp3->{ID3v2}->remove_frame("TRCK");
  214         $mp3->{ID3v2}->add_frame("TRCK",$tra);
  215     }
  216     if($alb) 
  217     {
  218         &plog(4, "sub write_tags: setting tag album as \"$alb\"");
  219         $mp3->{ID3v1}->album($alb);
  220                 $mp3->{ID3v2}->remove_frame("TALB");
  221             $mp3->{ID3v2}->add_frame("TALB",$alb);
  222     }
  223     if($com) 
  224     {
  225         &plog(4, "sub write_tags: setting tag comment as \"$com\"");
  226         $mp3->{ID3v1}->comment($com);
  227                 $mp3->{ID3v2}->remove_frame("COMM");
  228         $mp3->{ID3v2}->add_frame("COMM","ENG","",$com);
  229     }
  230         if($gen) 
  231     {
  232         &plog(4, "sub write_tags: setting genre artist as \"$gen\"");
  233         $mp3->{ID3v1}->genre($gen);
  234                 $mp3->{ID3v2}->remove_frame("TCON");
  235                 $mp3->{ID3v2}->add_frame("TCON",$gen);
  236         }
  237         if($year) 
  238     {
  239         &plog(4, "sub write_tags: setting tag year as \"$year\"");
  240             $mp3->{ID3v1}->year($year);
  241             $mp3->{ID3v2}->remove_frame("TYER");
  242             $mp3->{ID3v2}->add_frame("TYER",$year);
  243         }
  244 
  245     &plog(4, "sub write_tags: writting tags and closing mp3 file");
  246     $mp3->{ID3v1}->write_tag();
  247     $mp3->{ID3v2}->write_tag();
  248 
  249     $mp3->close();
  250 }
  251 
  252 sub guess_tags 
  253 {
  254     &plog(3, "sub guess_tags: \$file\"");
  255 
  256     my $file = shift;
  257 
  258         my $tag = "";
  259         my $art = "";
  260         my $tra = "";
  261         my $tit = "";
  262         my $alb = "";
  263         my $com = "";
  264 
  265     if($file =~ /^(\d+)( - |\. )(.*?)( - )(.*?)(\.mp3)/) 
  266     {
  267         &plog(4, "sub guess_tags: track - artist - title");
  268         $tra = $1;
  269         $art = $3;
  270         $tit = $5;
  271     }
  272 
  273     elsif($file =~ /^(\d+)( - |\. )(.*?)(\.mp3)/) 
  274     {
  275         &plog(4, "sub guess_tags: track - title");
  276         $tra = $1;
  277         $tit = $3;
  278     }
  279 
  280     elsif($file =~ /^(.*?)( - )(.*?)( - )(\d+)( - )(.*)(\.mp3)/) 
  281     {
  282         &plog(4, "sub guess_tags: artist - ablum - track - title");
  283         $art = $1;
  284         $alb = $3;
  285         $tra = $5;
  286         $tit = $7;
  287     }
  288 
  289     # mems prefered format
  290     elsif($file =~ /^(.*?)( - )(\d+)( - )(.*)(\.mp3)/) 
  291     {
  292         &plog(4, "sub guess_tags: artist - track - title");
  293         $art = $1;
  294         $tra = $3;
  295         $tit = $5;
  296         $alb = "";  # get this later
  297     }
  298 
  299     elsif($file =~ /^(.*?)( - )(.*)(\.mp3)/) 
  300     {
  301         &plog(4, "sub guess_tags: artist - title");
  302         $art = $1;
  303         $tit = $3;
  304         $tra = "";
  305         $alb = "";  # get this later
  306     }
  307     &plog(4, "sub guess_tags: returning art = \"$art\", track = \"$tra\", title = \"$tit\", album = \"$alb\"");
  308     return($art, $tra, $tit, $alb);
  309 }
  310 
  311 1;