(Dans la série: Je publie mes bouts de code, ça pourrait servir à d'autre:)
Voici un petit bout de code qui liste tous les mp3 présents dans un répertoire (et ses sous-répertoires), extrait artiste/titre/durée en lisant les tags ID3 et créé une playlist m3u. Cette playlist peut ensuite être directement ouverte dans un lecteur audio comme VLC ou Foobar2000. Bien sûr il vous faudra adapter les paramètres de l'appel à buildM3u() tout en bas.
(Je lance ce script pour re-générer la playlist quand j'ajoute des musiques )
Je sais, c'est tout simple mais ça évite d'avoir à chercher quand on veut le faire :-)
Vous aurez besoin de la librairie http://getid3.sourceforge.net/
EDIT: Et enfin, un autre bout de code qui convertit une playlist m3u en xspf:
<?php
/* Generates a xspf playlist from a m3u playlist.
playlist.m3u is read from disk.
xspf playlist is directly sent to browser.
This code is public domaine.
Warning: No error control. m3u files are expected to be clean and respect the standard.
(#EXTINF:duration,artiste - title)
International characters may not be properly handled.
xspf playlist generated by this script is suitable for use in xspf_player.
*/
header('Content-Type: text/plain; charset=utf-8'); // We use UTF-8 for proper international characters handling.
// Tells if a string starts with a substring or not.
function startsWith($haystack,$needle,$case=true) {
if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
}
function buildTrackXspf($artist,$tracktitle,$duration,$url)
{
return "<track>\n<location>".htmlspecialchars(trim($url))."</location>\n<title>".htmlspecialchars(trim($tracktitle))."</title>\n<creator>".htmlspecialchars(trim($artist))."</creator>\n<duration>".$duration."000</duration>\n</track>";
}
$lines = explode("\n",file_get_contents('playlist.m3u'));
$currentline = 0;
$nblines = count($lines);
/* Example line:
#EXTINF:404,Jon Hopkins - The End
^ ^ ^ track title
^ ^ artist name
^ duration (in seconds)
The next line contains the URL.
This has to be transformed to:
<track><location>URL</location><title>Tracktitle</title><creator>Artist</creator><duration>duration</duration></track>
*/
$tracks = array();
while ($currentline<$nblines ) {
$line = $lines[$currentline];
if (startsWith($line,'#EXTINF:')) {
$data = substr($line, 8); // Remove #EXTINF:
// Extract duration (in seconds)
$j = strpos($data,',');
$duration = intval(substr($data, 0, $j));
$data = substr($data, $j+1);
// Extract artist and track title
$k = strpos($data, ' - ');
$artist = substr($data, 0, $k);
$tracktitle = substr($data, $k+3);
// Get URL in next line
$url = $lines[$currentline+1];
$currentline = $currentline + 1;
$tracks[] = buildTrackXspf($artist,$tracktitle,$duration,$url); // Build XSPF XML for this track
}
$currentline = $currentline + 1;
}
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<title>Alternative musics for Minecraft gameplay</title>
<trackList>
XML;
foreach ($tracks as $track) {
echo $track;
echo "\n";
}
echo <<<XML
</trackList>
</playlist>
XML;
?>