Ok so not sure about this being the right area to post but its the best i could think of for this topic.
Im working on something that will require a playlist in XML. now i have a giant text file listing all my songs (courtosy of gd ol' winamp) now i could just go through each song and add a <song name=""/> around each of the tracks. But this isnt really practicle for a playlist of over 30,000.
Does anyone know of a program, or a method that would allow me to generate an XML file that listed all of the songs in a folder for example?
Any help or sugestions would be greatly appreciated. thanks in advance.
Reaper
XML question
Started by flamereaper, Feb 04 2007 04:26 PM
4 replies to this topic
#1
Posted 04 February 2007 - 04:26 PM
#2
Posted 04 February 2007 - 07:32 PM
Since my main language of choice is PHP, I would make a script that would read the directory, and then write it to an XML file.
Here's a quick mock-up I'll just write up real quick, untested mind you.
Something like that run under your localhost once should do it for ya. Only thing is it can't read the actual name of the song, only the filename. However, with a little parsing, since your filenames are probably composed of 'Artist - Song Name' format, you could simply split it and use the name and artist as further information in your XML tags.
Here's a quick mock-up I'll just write up real quick, untested mind you.
<?php
$path = 'C:/Documents and Settings/User/My Documents/My Music'; // Needs to be a complete path
$dir = opendir($path) or die("Unable to open '{$path}'"); // Open directory for reading
$files = array(); // Establish files array
while($file = readdir($dir)){ // Iterate the directory for files
if(substr($file, -3) == 'mp3') $files[] = $file; // If file has mp3 extension, add to $files array
}
closedir($dir); // Close directory
$fp = fopen('music.xml', 'wb'); // Open new file for writing (or create)
fwrite($fp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); // Start XML header for file
foreach($files as $file){ // Iterate the $files array
fwrite($fp, "<song name=\"{$file}\" />\n"); // Write each filename in XML format to the file
}
fclose($fp); // Close file
?>
Something like that run under your localhost once should do it for ya. Only thing is it can't read the actual name of the song, only the filename. However, with a little parsing, since your filenames are probably composed of 'Artist - Song Name' format, you could simply split it and use the name and artist as further information in your XML tags.
#3
Posted 05 February 2007 - 06:16 AM
yea the file names are all sorted out as artist first. and im sure if there not i can find out how to sort that out.
Thanks for the help. One more question. would this code (or a slight edit) to this code allow for subfolders (for example music>artist>album>file.mp3)
if so that would be a great help. thanks alot.
Thanks for the help. One more question. would this code (or a slight edit) to this code allow for subfolders (for example music>artist>album>file.mp3)
if so that would be a great help. thanks alot.
#4
Posted 05 February 2007 - 11:31 AM
There's when you get tricky and would need to create a recursive function that keeps calling itself when it encounters a new directory. I'll see if I can make a mock-up from the example I gave you above, but be careful with these. If seems to take a looooong time to process, it could be infinitely looping, which can suck your computer processing up until PHP reaches its time limit for execution (typically 60 seconds by default).
As far as I can tell that should do the trick. Worse case scenario is the infinite loop bit.
<?php
function get_music($path){
$dir = opendir($path) or die("Unable to open '{$path}'"); // Open directory for reading
while($file = readdir($dir)){ // Iterate the directory for files
if(is_dir($file)){ // If the file is actually a directory
get_music($file); // Have function call itself again with this directory
continue; // Continue with the looping and don't worry about checking for music
}
if(substr($file, -3) == 'mp3') $files[] = $file; // If file has mp3 extension, add to $files array
}
closedir($dir); // Close directory
}
$path = 'C:/Documents and Settings/User/My Documents/My Music'; // Needs to be a complete path
$files = array(); // Establish files array
get_music($path); // Run our recursive function with the $path set
$fp = fopen('music.xml', 'wb'); // Open new file for writing (or create)
fwrite($fp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); // Start XML header for file
foreach($files as $file){ // Iterate the $files array
$info = explode(' - ', $file); // An example of splitting the filename to find the artist/song
$artist = $info[0]; // Grab artist from first part of $info
$song = str_replace('.mp3', '', $info[1]); // Grab song from second part and get rid of extension
fwrite($fp, "<song file=\"{$file}\" artist=\"{$artist}\" song=\"{$song}\" />\n"); // Write each filename in XML format to the file
}
fclose($fp); // Close file
?>
As far as I can tell that should do the trick. Worse case scenario is the infinite loop bit.
#5
Posted 05 February 2007 - 01:14 PM
Mkay cheers dude. youve been more than helpfull. Im sure the script will work, if not im sure a lil tweaking can stop any infinite loop that might occur.
Thanks again.
Thanks again.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
