MP3s durchnummerieren

Was mich beim ipod extrem nervt, ist die Sortierung der Songs. Jeder andere Player sortiert im Zweifelsfall die Songs in einem Verzeichnis nach dem Dateinamen. Apple hält sich für besonders schlau und sortiert, wenn keine Tracknummer im ID3-Feld steht, statt dessen nach dem Songtitel. Ahrg, da nützt es natürlich nichts, wenn die Dateien „Track01.mp3“ bis „Track99.mp3“ benannt sind 🙁
Ok, was tun, wenn man 1300 Alben auf der Platte hat, von denen bei etwa der Hälfte die Tracknummer fehlen? Easytag ist zwar gut, aber so viel Zeit habe ich dann doch nicht 😉 Ich habe mir ein olles PHP-Script hingefrickelt, bis es tat, was es sollte. Wer mehr Komfort möchte, darf es schöner machen 😉

#!/usr/bin/php4
< ?php
///////////////////////////////////////////////////////////
// tagtracks.php - a quick hack
//
// Adds a tracknumber to your MP3s. Even if your MP3s are 
// well tagged, often the tracknumber is missing. This 
// script sets a tracknumber based on the order of the
// filenames.
// This script handles only the first level of the given
// directory, your directory structure must look like this:
//
// /path/to/your/mp3s/
// /path/to/your/mp3s/Artist1 - Album1/*.mp3
// /path/to/your/mp3s/Artist1 - Album2/*.mp3
// /path/to/your/mp3s/Artist2 - Album1/*.mp3
//
// There is no support for recursion (yet).
//
// Usage:
// - install pear MP3_ID package
// - adopt cgi-path (first line), Id.php path and MP3_DIR
//   constant (should point to your main MP3-Dir)
// - consider a "dry run": comment the line
//   $mp3->write();
//   and look what tagtracks.php would do
// - run tagtracks.php from commandline
//
// Questions? Drop me a note: melle (at) gmx /d0t/ at
//
// Known Bugs:
// - does not work recursive
// - no error checking...
 
// you need the MP3_ID package from pear repository
// see http://pear.php.net/package/MP3_ID
require_once 'MP3/Id.php';
 
// location of the mp3 collection
define("MP3_DIR", "/mnt/storage3/music/mp3/");
 
// return true if $str ends with $sub
function endsWith( $str, $sub )
{
   return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
}
 
// tags one directory
function tag_directory($path)
{
    echo "Tagging: " . $path . "\n";
 
    // holds all mp3 filenames of the directory
    $arr = array();
 
    // lookup all files in the given directory
    $d = dir($path);
    while (false !== ($entry = $d->read()))
    {
        if (   is_file($path . '/' .$entry)
            && (   endsWith($entry, "MP3")
                || endsWith($entry, "mp3")))
        {
            // check for possible track number in filename
            // if this test fails, you must sort the files
            // yourself.
            if (!ereg("[0-9][0-9]",$entry))
            {
                echo "NOT SORTED: " .$path . "\n";
                return;
            }
 
            // add filename to our array
            $arr[] = $entry;
        }
    }
    $d->close();
 
    // sort filenames
    sort($arr);
 
    // write tracknumber tag ordered by filename
    foreach ($arr as $i => $filename)
    {
        $mp3 = new MP3_Id();
        $mp3->read($path . '/' . $filename);
 
        $trackno = $mp3->getTag("track");
        echo "Track [".$trackno."] " . $filename . "\n";
 
        if (   0  > $trackno
            || 99 < $trackno
            || "" == $trackno)
        {
                $mp3->setTag("track", $i+1);
                $mp3->write();
                echo "Write [".($i+1)."] " . $filename . "\n";
        }
    }
}
 
// open MP3 location and tag all subdirectories
$d = dir(MP3_DIR);
while (false !== ($entry = $d->read()))
{
    if (is_dir(MP3_DIR . $entry))
    {
        tag_directory(MP3_DIR . $entry);
    }
}
$d->close();
 
?>

Download tagtracks.php.

4 Antworten auf „MP3s durchnummerieren“

  1. Hmm, ist es bei der Sortierung nicht egal, ob es „Track00-99“ oder „00-99 bla bla“ im Dateinamen heißt? Sollte doch beides zum selben Ergebnis kommen?!

  2. Ja, das ist egal. Es geht aber bei dem Problem um die Tracknummern in den ID3-Tags. Wenn diese fehlen, sortiert der ipod alphabetisch nach Songtitel. Wenn ich das mal demonstrieren dürfe 😉


    Falsch: ipod sortiert nach Songtitel, weil ID3-Tracknummer fehlt.


    Richtig: ipod mit korrekt getaggten Songs und gewünschter Sortierreihenfolge.

Kommentare sind geschlossen.