How-to: Import Windows .m3u Playlists Into iTunes On a Mac
Your Windows .m3u playlists are text files that contain Windows filepaths. Unfortunately, that makes them unreadable to Mac iTunes without a bit of editing.
Let's suppose you have all of your music neatly organized in a 'Music' folder on Windows and that this folder also contains your .m3u playlist files. An example win_playlist.m3u saved with relative filepaths might look like:
#Meta-description... Dubstep\RayJayRay\B-Side Hammer.mp3 #Another meta-description... Unplugged\Three Chord Digression.mp3 #The greatest song ever written... Electronic\Craig Armstrong\As If to Nothing\Finding Beauty.mp3
Clearly, you have excellent taste. But, if you copy 'Music' directly from Windows to Mac, iTunes will not be able to successfully File > Library > Import Playlist... these .m3u files. The Windows filepaths must first be translated into Mac filepaths for iTunes to find the songs.
Let's suppose you copy this Windows 'Music' folder into '/Users/Shared/' on your Mac. The Mac translation of the above playlist would be:
#Meta-description... /Users/Shared/Music/Dubstep/RayJayRay/B-Side Hammer.mp3 #Another meta-description... /Users/Shared/Music/Unplugged/Three Chord Digression.mp3 #The greatest song ever written... /Users/Shared/Music/Electronic/Craig Armstrong/As If to Nothing/Finding Beauty.mp3
This translation could be done with careful use of Replace All...'s in your favorite text editor, or it could be done in the Mac Terminal application with the commands:
$ cd /Users/Shared/Music $ sed -e 's|^#|&|;t' -e 's|\\|/|g' -e 's|^|/Users/Shared/Music/&' win_playlist.m3u > mac_playlist.m3u
This use of sed is somewhat advanced because it incorporates branching. The first command quotes metadata lines, which start with '#'; if it successfully matches then the remaining commands are skipped. The second and third commands replace '\' with '/' and prepend "/Users/Shared/Music/" on each non-metadata line.
It's tempting to wrap that imposing command inside a shell script. Let's do it.
#! /bin/bash
# Convert Windows .m3u playlists with relative song paths
# for import into MacOS iTunes with absolute song paths
# AUTHOR: Sarah Kabala
set -u # Exit script if an undefined variable is encountered
set -e # Exit script if an error occurs
# Make sure there are at least 2 arguments:
# First argument is base music directory
# Remaining arguments are .m3u playlist files
if [ $# -lt 2 ]; then
echo "Usage: $0 MUSICDIRECTORY PLAYLIST.m3u ..."
echo "Simple example: $0 /Users/Shared/music *.m3u"
echo "Advanced example: ls -1 *.m3u | grep -v '.mac.m3u$' | tr '\n' '\0' | xargs -0 $0 \`pwd\`"
exit
fi
# For each playlist...
for file in "${@:2}"
do
# 1. Quote metadata lines
# 2. On non-metadata lines
# a. Change all '\' to '/'
# b. Add 'MUSICDIRECTORY/' prefix
# 3. Write output to new playlist file with augmented filename
sed -e 's|^#|&|;t' -e 's|\\|/|g' -e "s|^|$1/&|" "$file" > "${file%m3u}"mac.m3u
done
If you save this script file as m3umac.sh in your Mac '/Users/Shared/Music' folder, then all of your Windows playlists can be translated with the Mac Terminal commands:
$ cd /Users/Shared/Music $ chmod 755 m3umac.sh $ ./m3umac.sh /Users/Shared/Music *.m3u
If you would like your Mac translated playlist files to use relative filepaths instead of absolute filepaths, use the alternative final command:
$ ./m3umac.sh . *.m3u
Both of the final commands given above apply the script to all .m3u files in '/Users/Shared/Music'. This might cause an error if there are any .mac.m3u files present from an earlier use of the script. A more complicated but safer final command is:
$ ls -1 *.m3u | grep -v '.mac.m3u$' | tr '\n' '\0' | xargs -0 ./m3umac.sh `pwd`
I encourage you to try to understand the nuances of that command, if you don't already. First, it lists all files in the current directory that end in ".m3u", each on a separate line. Then it removes any lines from ls that end with ".mac.m3u". Then it translates all newline characters from grep into null '\0' characters (this is important if any playlist filename contains a space). Then it passes that stream as trailing arguments to m3umac.sh, separating each argument at each occurance of '\0'. Further, it passes the output of the command pwd as the first argument to m3umac.sh.
Now, in iTunes, File > Library > Import Playlist... should successfully import all of your translated .mac.m3u playlists.