4

There is a way to rsync a m3u playlist into a destination directory.

How can I process the list and number the output so it orders the files to match the playlist order?

For example the m3u:

/music/song-yellow.mp3
/music/song-red.mp3
/music/song-blue.mp3

would copy/rsync the playlist contents, renaming the files to:

/dest-path/01-song-yellow.mp3
/dest-path/02-song-red.mp3
/dest-path/03-song-blue.mp3

This is so I can build mixtape playlists and copy them for people.

Any advice appreciated.

invert
  • 1,763

1 Answers1

3

Try something like this:

#!/usr/bin/python

FILE = '/home/my-home/my-playlist.m3u'
DIR = '/some-target-directory'

import os.path, shutil, sys

for i, s in enumerate(open(FILE)):
    s = s.rstrip()
    try:
        shutil.copy(s, os.path.join(DIR, '%03d-%s' % (i+1, os.path.basename(s))))
    except IOError, e:
        sys.stderr.write('warning: %s\n' % e)

# End of file.

Obviously, set FILE and DIR to what you need. Apologies for going over the top and using Python for this — it helps not to have to escape all those characters shells love to attach special meanings to.

The script won't fail on I/O errors, it'll just output a warning. Other than that, there are no sanity checks (as you can probably see). Nor does it exclude M3U comments. It'll happily copy any character you throw at it, though, with the obvious exceptions of NULs and slashes which are special characters at the filesystem level.

manatwork
  • 31,277
Alexios
  • 19,157