#!/usr/bin/perl -w # # attempts to organize mp3s into dirs by: # 1. checking ID3 tag for Artist name # 2. scanning the filename # # it will further organize the mp3s into subdirectories under the # artist name by album # use strict; use MP3::Info; use vars qw( $mp3dir @files %dirs ); # change as necessary # $mp3dir = '/opt/mp3/'; chdir $mp3dir or die "can't chdir to $mp3dir: $!\n\n"; # get file list # opendir(DIR, $mp3dir) or die "can't open $mp3dir for read: $!\n\n"; @files = grep { /\.[Mm][Pp]3$/ } readdir(DIR); closedir(DIR) or warn "error closing $mp3dir: $!\n\n"; &make_dir_list; &move_files; exit; # create dir listing # %dirs = ( 'dirname' => { 'subdir1' => [ file1, file2, ... ], ... }, +... ); # sub make_dir_list { foreach ( @files ) { my ($artist, $album, $tag); # attempt to find artist name through mp3 tag # $tag = get_mp3tag( $_ ); # fall back to scanning filename. we're assuming artist name # is everything up to the first hyphen # unless ( $tag && $tag->{ARTIST} !~ /^\s*$/ && $tag->{ARTIST} ne 'a +rtist' ) { ($artist) = /^([^-]+?)-.+$/; $artist ||= 'unsorted'; } else { $artist = $tag->{ARTIST}; } $album = $tag->{ALBUM} || ""; if ( $album =~ /^\s*$/ || $album eq 'title' ) { $album = 'misc' } $artist = clean_name( $artist ) unless $artist eq 'unsorted'; $album = clean_name( $album ) unless $album eq 'misc'; push @{ $dirs{$artist}{$album} }, $_; } } # sanitize artist name (or filename fragment) for use as a dir name # sub clean_name { my $dir = shift; return 'unsorted' unless $dir; $dir = lc($dir); $dir =~ s/\bthe\b//; $dir =~ s/_/ /g; $dir =~ s/^ +//; $dir =~ s/ +$//; $dir =~ s/ +/ /g; $dir =~ s/[,(){}\[\]]//g; return $dir; } # create dirs, put sorted files into them # sub move_files { foreach my $artist ( keys %dirs ) { # XXX debug # #print " $artist\n"; unless ( -d qq{$artist} ) { mkdir( qq{$artist}, 0755 ) or die "error mkdir($artist): $!\n\n" +; } foreach my $album ( keys %{ $dirs{$artist} } ) { # XXX debug # #print " $album\n", map { "\t$_\n" } @{ $dirs{$artist}{$album +} }, "\n"; unless ( -d qq{$artist/$album} ) { mkdir( qq{$artist/$album}, 0755 ) or die "error mkdir($artist/ +$album): $ !\n\n"; } #system('/bin/mv', @{ $dirs{$artist}{$album} }, qq{$artist/$albu +m/}) == 0 # or die "system('/bin/mv'): $?\n\n"; foreach my $mp3 ( @{ $dirs{$artist}{$album} } ) { rename $mp3, qq{$artist/$album/$mp3} or die "can't rename $mp3 +: $!\n\n"; } } } }

In reply to mp3 organizer by sacked

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.