Converting MPD (or any .m3u) playlist to iTunes format
This is hyperspecific to my use case but maybe others will find this useful. I have several iPods that I sync from a Windows VM on my main Linux PC. My music library lives on the Linux host, managed with
beets
and
mpd
. Mpd playlists are standard .m3u files, i.e. a list of file paths, which iTunes can import, but the file paths are Linux file paths and are relative. I needed a way to convert each line from a relative Linux file path to an absolute Windows file path.
My music library /home/yequari/Music is mapped to Z:\ in the Windows VM, so for me it was just a matter of converting the Linux path to a Windows path and prepending the Z:\ root drive to it (e.g. Sleep/Dopesmoker/Dopesmoker.mp3 becomes Z:\Sleep\Dopesmoker\Dopesmoker.mp3 ). There’s probably some sed magic that could do this pretty easily, but I wrote a
Python script
to do it instead.
import ntpath
import argparse
def main(args: argparse.Namespace):
infile = args.f
outfile = args.o
prefix = args.prefix
for line in infile:
# use ntpath since we're specifically writing out Windows paths
relpath = ntpath.normpath(line)
newpath = ntpath.join(prefix, relpath)
print(newpath)
outfile.write(f'{newpath}\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='PlaylistAbs',
description='Converts relative paths to absolute paths')
parser.add_argument('prefix',
help='Absolute filepath to prepend to each entry')
parser.add_argument('-f',
type=argparse.FileType('r'),
help='Playlist file')
parser.add_argument('-o',
type=argparse.FileType('w'),
help='Output file')
args = parser.parse_args()
main(args)