#!/usr/bin/perl -w use strict; use DBI; use File::Find; use MP3::Info; use Getopt::Std; my (%options, $DBH, $STH, $tmp, %mp3s, $artist, $album, $title); getopts("d:au:p:h", \%options); if ($options{h}) { print <<'eof'; -d dir: Root MP3 directory -u username: Username for mysql -p password: Password for mysql -a: Turn aliasing on -h: This help file eof exit; } my (@dirs) = $options{m} || ("Your mp3 dir"); my ($orig_path) = $dirs[0]; $orig_path =~ /(.+?)(\/(.+?)\/)/; my ($alias) = $2; my ($user_name) = $options{u} || 'user'; my ($password) = $options{p} || 'password'; my ($database) = 'DBI:mysql:mp3'; find(\&id3info, @dirs); sub id3info { return unless $File::Find::name =~ /^.+?\.[Mm][Pp]3$/; $tmp = get_mp3tag($File::Find::name); return unless defined $$tmp{'ARTIST'} && defined $$tmp{'TITLE'}; $$tmp{'ALBUM'} = "Other" unless defined $$tmp{'ALBUM'}; $$tmp{'TRACKNUM'} = 0 unless defined $$tmp{'TRACKNUM'}; unless (exists $mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE +'}}) { %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} = ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACKNUM'}, 'TIME' => $$tmp{'TIME'} ); } else { if ($mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}{'TI +ME'} < $$tmp{'TIME'}) { %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} += ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACK'}, 'TIME' => $$tmp{'TIME'} ); } } if ($options{a}) { $mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}{'PATH'} + =~ s/^$orig_path(.+)$/$alias$1/; } } $DBH = DBI->connect ($database, $user_name, $password, { RaiseError => + 1 }); $STH = $DBH->prepare (qq{ INSERT mp3s (artist,album,title,track,path) +VALUES(?,?,?,?,?)}); foreach $artist (keys %mp3s) { foreach $album (keys %{ $mp3s{$artist} }) { foreach $title (keys %{ $mp3s{$artist}{$album} }) { $STH->execute ($artist, $album, $title, $mp3s{$artist}{$a +lbum}{$title}{'TRACK'}, $mp3s{$artist}{$album}{$title}{'PATH'}); } } } $STH->finish(); $DBH->disconnect ();

In reply to MP3/MySQL Cataloger by smgfc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.