#!/usr/bin/perl
use warnings;
use strict;
######################################################################
#
# BCE Project 19, Exercise accessing nested data
#
# Search for *.mp3, build structure of each mp3's info.
#
# print structured info
#
my $Filename = '\*.mp3';
my $MusicRoot = '/Your/Music/Location'; # no trailing slash
# REQUIRES TagLib 1.5 avaliable at
# REQUIRES Audio-TagLib-1.50_01 avaliable at cpan.org
use Audio::TagLib::MPEG::Properties;
use Data::Dumper; # For testing
$Data::Dumper::Indent = 1; # For testing
# Subs expect (Audio::TagLib::MPEG::File->new('Filename'))
sub _Get_Artist { my $t = shift; return $t->tag()->artist()->toCString(); }
sub _Get_Album { my $t = shift; return $t->tag()->album()->toCString(); }
sub _Get_Title { my $t = shift; return $t->tag()->title()->toCString(); }
sub _Get_Comment { my $t = shift; return $t->tag()->comment()->toCString(); }
sub _Get_Genre { my $t = shift; return $t->tag()->genre()->toCString(); }
sub _Get_Year { my $t = shift; return $t->tag()->year(); }
sub _Get_Track { my $t = shift; return $t->tag()->track(); }
sub _Get_Length { my $t = shift; return $t->audioProperties->length(); }
sub _Get_Bitrate { my $t = shift; return $t->audioProperties->bitrate(); }
sub _Get_Samplerate { my $t = shift; return $t->audioProperties->sampleRate(); }
sub _Get_Channels { my $t = shift; return $t->audioProperties->channels(); }
# Sub expects ('Filename')
sub HashMPEG
{
my $f = shift;
my %Artist;
my %Album;
my %Title;
my %TagInfo;
my $m = Audio::TagLib::MPEG::File->new($f);
my %TagsMPEG = ('comment' => \&_Get_Comment, 'genre' => \&_Get_Genre, 'year' => \&_Get_Year, 'track' => \&_Get_Track, 'length' => \&_Get_Length, 'bitrate' => \&_Get_Bitrate, 'samplerate' => \&_Get_Samplerate, 'channels' => \&_Get_Channels);
for my $t (keys %TagsMPEG) {
$TagInfo{$t} = $TagsMPEG{$t}->($m);
}
$Title{_Get_Title($m)} = \%TagInfo;
$Album{_Get_Album($m)} = \%Title;
$Artist{_Get_Artist($m)} = \%Album;
return \%Artist;
}
sub HashFiles
{
my $d = shift;
my $f = shift;
my %FH;
my @flist = `find $d -type f -name $f`;
if ($#flist) {
for my $t (@flist) {
chomp $t;
$FH{$t} = HashMPEG $t;
}
}
return %FH;
}
my %FilesHash = HashFiles $MusicRoot, $Filename;
# for testing
#print Dumper(\%FilesHash);
for my $File (keys %FilesHash) {
print "File : " . $File . "\n";
}
####
File : /Common/Music/Stevie Ray Vaughan and Double Trouble/Live Alive/03-Pride and Joy.mp3
Artist : Stevie Ray Vaughan and Double Trouble
Album : Live Alive
Title : Pride and Joy
Comment :
Genre : Blues
Year : 1986
Track : 3
Length : 304
Bitrate : 256
Samplerate : 44100
Channels : 2
####
'/mnt/RaidOne/Music/Stevie Ray Vaughan and Double Trouble/Live Alive/03-Pride and Joy.mp3' => {
'Stevie Ray Vaughan and Double Trouble' => {
'Live Alive' => {
'Pride and Joy' => {
'bitrate' => 256,
'channels' => 2,
'track' => 3,
'samplerate' => 44100,
'genre' => 'Blues',
'length' => 304,
'comment' => '',
'year' => 1986
}
}
}
},