Search code examples
ubuntumedia-playervlcplaylistxspf

How to copy all files in an xspf playlist to a directory?


I have created an XSPF playlist of songs that I need to copy to a specific directory, how does one do that? Preferably Python.

EDIT: I have posted this questions several years ago, back when I didn't know much about how things worked. I have managed to do this using a small Python script that I wrote.


Solution

  • Couldn't find this somewhere else myself, so I coded a quick and dirty PHP hack to do it (just now). This is by no means an elegant solution, but does well enough for my purposes. It may be a good idea to comment out the copy part in the very end and replacing it with some echo construction to make sure your code copies the right files.

    Help entry:

    Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar
    -i filename  or --input filename  - XSPF file to parse
    -o directory or --input directory - directory to copy to (needs to exist!)
    --help - display this help and terminate
    

    Script source:

    #!/usr/bin/php
    <?php
    // Quick and dirty hack.
    
    for($i = 1; $i < count($argv); ++$i) {
        if($argv[$i] == '--help' || $argv[$i] == '-e') {
            echo "Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar\n";
            echo "-i filename  or --input filename  - XSPF file to parse\n";
            echo "-o directory or --input directory - directory to copy to (needs to exist!)\n";
            echo "--help - display this help and terminate\n";
            die();
        }
        if($argv[$i] == '--input' || $argv[$i] == '-i') {
            if(!isset($argv[$i+1])) {
                die("No input filename given (xspf file), use -i filename or --input filename\n");
            } else {
                $filename = $argv[$i+1];
            }
        }
        if($argv[$i] == '--output' || $argv[$i] == '-o') {
            if(!isset($argv[$i+1])) {
                die("No output directory given, use -o directory or --output directory\n");
            } else {
                $outputDir = $argv[$i+1];
            }
        }
    }
    if(!isset($filename) || empty($filename)) {
        die("No input filename given (xspf file), use -i filename or -input filename\n");
    }
    if(!isset($outputDir) || empty($outputDir)) {
        die("No output directory given, use -o directory or --output directory\n");
    } else {
        $outputDir = rtrim($outputDir, '/');
    }
    
    $xml = file_get_contents($filename);
    
    preg_match_all('#<location>(.*?)</location>#', $xml, $matches);
    $matches = $matches[1]; // Select only the contents of (.*?), not the entire pattern
    $matches = preg_replace('#file://(.*)#', '\\1', $matches); // Remove file://
    foreach($matches as $key => $value) {
        $matches[$key] = urldecode($value);
        $matches[$key] = html_entity_decode($matches[$key]);
    }
    
    
    foreach($matches as $value) {
        $base = basename($value);
        echo "Copying $base ...\n";
        copy($value, "$outputDir/$base");
    }