Mp3 umbenennen.pl

From LUGUM

Jump to: navigation, search
#!/usr/bin/perl

##############################################################################
#                                                                            #
#      Script zum Ändern des Dateinamens anhand des Mp3-Tags                 #
#                                                                            #
#           Autor: Andreas Kraus                                             #
#         Version: 1.0                                                       #
#  Ersterstellung: 18.11.2007                                                #
# Letzte Änderung: 18.11.2007                                                #
#                                                                            #
#                                                                            #
# Copyright (C) 2007 Andreas Kraus <lug@ak1301.de>                           #
#                                                                            #
#                                                                            #
# 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.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   #
#                                                                            #
#                                                                            #
##############################################################################



use strict;
use File::Find;
use MP3::Tag;
use File::Basename;
use File::Copy;

# Startverzeichnis ab dem Gesucht werden soll
my $verzeichnis = "/home/ak1301/testordner";

# Suche nach allen .mp3 Dateien
my @mp3s;
find(\&suche,$verzeichnis);


# aus jeder gefundenen .mp3 Datei wird der Künstler und der Titel ausgelesen und die Datei wird
# danach umbenannt.

foreach (@mp3s){
    my %mp3tag;
    getname($_, \%mp3tag);
    my $filename =  dirname($_)."/".$mp3tag{'artist'} ." - " . $mp3tag{'titel'} . ".mp3";
    #print $filename;
    rename($_, $filename) || print "Warnung: Konnte die Datei \"$_\" nicht in \"$filename\" Umbenennen\n" if ($_ ne $filename) ;
}

# Funktion für File::Find
sub suche{
    # Für eine Suche nach Dateiendung .mp3
    push @mp3s, $File::Find::name if $File::Find::name=~ /\.mp3$/i;
}

# Liest Dateiname und Künstler aus der Datei und gibt sie in einem Hash zurück

sub getname{
    my $filename = shift;
    my $mp3tag = shift;
    my $mp3 = MP3::Tag->new($filename);
    (my $title, my $track, my $artist, my $album, my $comment, my $year, my $genre) = $mp3->autoinfo();
    $$mp3tag{'titel'} = $title;
    $$mp3tag{'artist'} = $artist;
    $mp3->close();
}
Persönliche Werkzeuge