MP3 Sort

от Хоревики, свободната енциклопедия

#!/usr/bin/perl -w
#######################--INFO--################################
# By Joshua Curtis http://www.curtisonline.net
#
# This script was made to help in organizing mp3s. If you have
# one directory with allot of mp3's and the names are not very
# readable you can run this script against them. It will read
# the id tag of a mp3 and rename the file to the title of the
# song but with _ instead of spaces. It can also create a
# directory structure from the id tag if the -c option is used.
# The structure will be put in the same directory the mp3s are.
# It will be as follows: "/ARTISTNAME/CDTITLE/SONG.mp3"
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# You must always use the absolute path to the mp3s
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# COPYRIGHT:
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#######################--REVISION LIST--########################
# 2/11/00 v0.1  Script first made public
# 2/13/00 v0.11 Cleaned up and enhanced by
#               Cristian Ionescu-Idbohrn <cii@axis.com>.
#               Copes with blank instead of null characters
#               as tag separators.
#               Corrected some directory inconsistencies. Absolute
#               paths are now used.
#               Added -l log option.
#               Should work on both UNIX like and windoz systems.
################################################################
#==============================================================#

use strict;
use MP3::Tag;
use vars qw/$createdirs $dir $option $null $show_log/;

####################################################
# Find out what we have and what to do
####################################################
# This is where we check for input and make sure
# we have all needed options.

if (! @ARGV)
{
  &ShowUsage();
  exit;
}

$show_log = 0;
foreach $option (@ARGV)
{
  if ($option =~ /(--help|-h)/)
  {
    &ShowUsage();
    exit;
  }
  elsif ($option =~ /^-d=.*?/)
  {
    ($null, $dir) = split(/=/, $option);
    $dir =~ s/\s*$//;
    $dir =~ s/\/$//;
    $dir =~ s/\s*$//;
  }
  elsif ($option =~ /^-c$/)
  {
    $createdirs = "1";
  }
  elsif ($option =~ /^-l$/)
  {
    $show_log = "1";
  }
  else
  {
    print "*** Bad option \"$option\"\n";
    &ShowUsage();
    exit;
  }
}

if ($dir)
{
  ReadFiles($dir);
}
else
{
  &ShowUsage();
   exit;
}

####################################################
#                    ShowUsage()
####################################################
# Shows the usage of the program.
# Called with no options.

sub ShowUsage
{
  print "
Usage: sort_mp3.pl [options]
Options should be seperated by a space and my be in any order.

-c  Create a directory structure from the mp3 tag.
    It is made inside the directory holding the un-named mp3s.
    If not set the files will just be renamed.
    Example: \"/ARTISTNAME/CDTITLE/SONG.mp3\"

-d= Path to the directory where mp3's can be found.
    There should be no spaces in the path name.
    Example: \"-d=/unsorted_mp3s\"

-l  Show what's goin' on (default: keeps quiet).

CAVEAT
    Tags may be separated by blank instead of null characters.
    More than 1 blank character, anywhere in the \"Artist Name\",
    \"CD Title\" or \"Song Title\" will leed to unpredictible results.
";
}

####################################################
#                   ReadFiles()
####################################################
# Reads all the file names *.mp3 from the directory
# defined with the -d= option. It then passes the
# mp3 name to Rename.

sub ReadFiles
{
  my ($dir) = @_;
  my $file;

  opendir(DIR, $dir) or die "*** Can't open \"$dir\": $!";
  while (defined($file = readdir DIR))
  {
    next if $file !~ /.mp3$/ig;
    &ReNameMP3($dir . "/" . $file);
  }
  close(DIR);
}

####################################################
#                     ReName()
####################################################
# Renames the mp3 by getting the track title from
# id tag inside the mp3. It will also make a path
# to put the file in if the -c option is set.
# the path will be "/ARTISTNAME/CDTITLE/SONG.mp3"

sub ReNameMP3
{
  my $mp3 = MP3::Tag->new(@_);
  my ($mp3_file) = @_;
  my %tag;
  my $path_created = "No";
  my $got_it_all = 0;
  my $n;
  my $a;
  my $c;
  $mp3->get_tags();
  if (exists $mp3->{ID3v1}) {
    
    $tag{songtitle} = $tag{filename} = $n = $mp3->{ID3v1}->song;
    $tag{artist}    = $tag{artistfolder} = $a = $mp3->{ID3v1}->artist;
    $tag{cdtitle}   = $tag{cdfolder} = $c = $mp3->{ID3v1}->album;
    
    $got_it_all = 1 if defined($n) and defined($a) and defined($c);
    
    print "*** Song Title  missing in \"$mp3_file\"\n" if ! defined($n);
    print "*** Artist Name missing in \"$mp3_file\"\n" if ! defined($a);
    print "*** CD Title    missing in \"$mp3_file\"\n" if ! defined($c);

    if ($tag{cdtitle} and $tag{artist} and $tag{songtitle})
    {
      # Remove Slashes
      $tag{filename}     =~ s/[\/]+/_/g;
      $tag{cdfolder}     =~ s/[\/]+/_/g;
      $tag{artistfolder} =~ s/[\/]+/_/g;    
      
      if ($createdirs)
      {
        mkdir("$dir/$tag{artistfolder}", 0777);
        mkdir("$dir/$tag{artistfolder}/$tag{cdfolder}", 0777);
        $tag{filename} =
          "$dir/$tag{artistfolder}/$tag{cdfolder}/$tag{artistfolder} - $tag{filename}";
        $path_created = "Yes";
      }
      else
      {
        $tag{filename} = "$dir/$tag{artistfolder} - $tag{filename}";
      }
      $tag{filename} = "$tag{filename}.mp3";
      if (rename($mp3_file, "$tag{filename}")) {
        print "Renamed $tag{filename}\n" if $show_log;
      } else {
        print "Error renaming $tag{filename}\n" if $show_log;
      }
      # Print a log of the renamed files, if requested.
      print "
            Artist Name:   $tag{artist}
            CD Title:      $tag{cdtitle}
            Song Title:    $tag{songtitle}
            Old File Name: $mp3_file
            New File Name: $tag{filename}
            Created Path:  $path_created\n" if $show_log;
    }
  }
  print "No ID3 Tag for $mp3_file\n" if $show_log;
  $mp3->close();
}
Лични инструменти