Saving out a playlist to share it

Apple, and to a lesser extent, Microsoft, go out of their way to make sharing music painful – even in the simple case of sharing a playlist of songs with your buddy (which clearly falls under Fair Use!). For example, suppose you made a playlist for yourself (of music you own), put it on an iPod, and then played it for a friend. If they liked it and wanted it on their iPod as well, how would you share it with them? You can’t “send” them a playlist – you’d have to copy the songs one by one to a hard drive or DVD, then hand it to your friend, he plugs it into his computer, he copies over the songs, then he takes the time to recreate your playlist manually in iTunes. It would be funny if the state of the art wasn’t so sad!

In just this situation, I ended up writing a short PHP console script to do this semi-automatically. It parses a playlist file in PLS format, then copies the files to a central directory, which can then be copied to an external hard drive. This would then be plugged into your friend’s PC, and the music copied to the destination of choice. Finally, it outputs a modified PLS file with all the paths changed.

No magic here, just a few lines of code to make the tedious task of sharing music you like, somewhat less so.

  1. // saveplaylist.php
  2. $copy_folder = 'H:\\temp\\'; // path to a central directory
  3. $destination_folder = 'C:\\My Music\\';  // the destination on your friend's PC
  4. $playlist_file = 'C:\Users\JohnDoe\Music\Playlists\myplaylist.pls';  // the playlist file
  5. $contents = file_get_contents($playlist_file);
  6.  
  7. $newplaylist = $contents;
  8.  
  9. $matches = array();
  10. if (preg_match_all('/File(.*)=(.*)$/msU', $contents, $matches, PREG_SET_ORDER))
  11. {
  12. 	foreach ($matches as $match)
  13. 	{
  14. 		$file = $match[2];
  15. 		$file = trim($file);
  16. 		$name = pathinfo($file, PATHINFO_BASENAME);
  17. 		echo "Copying {$file}...\n";
  18. 		$copy_file = $copy_folder . $name;
  19. 		$destination_file = $destination_folder . $name;
  20. 		copy($file, $copy_file);
  21. 		$escaped_filename = str_replace('\\', '\\\\', $match[2]);
  22. 		$escaped_filename = str_replace('.', '\\.', $escaped_filename);
  23. 		$escaped_filename = str_replace('[', '\\[', $escaped_filename);
  24. 		$escaped_filename = str_replace(']', '\\]', $escaped_filename);
  25. 		$escaped_filename = str_replace('(', '\\(', $escaped_filename);
  26. 		$escaped_filename = str_replace(')', '\\)', $escaped_filename);
  27. 		$newplaylist = preg_replace('/File'.$match[1].'='.$escaped_filename."/", 'File'.$match[1].'='.$destination_file, $newplaylist);
  28. 	}
  29. }
  30. echo '<hr>';
  31. echo $newplaylist;

Leave a comment

Your email address will not be published. Required fields are marked *