Skip to content

Instantly share code, notes, and snippets.

@craSH
Last active December 14, 2015 15:49
Show Gist options
  • Save craSH/5111005 to your computer and use it in GitHub Desktop.
Save craSH/5111005 to your computer and use it in GitHub Desktop.
Add support for creating pls format playlists in addition to m3u
#!/usr/bin/env python
"""
Tiny script to split M3U playlist entries from stdin out to multiple playlists.
Handy for iTunes, because it's stupid in how it imports playlists.
Copyleft 2011 Ian Gallagher <[email protected]>
"""
import sys, os
def do_stdin():
pls = False
if len(sys.argv) > 1 and sys.argv[1] == '-pls':
pls = True
fname = ''
extinf = ''
url = ''
title = ''
m3u_template = "#EXTM3U\n{extinf}\n{url}\n"
pls_template = "[playlist]\nFile0={url}\nTitle0={title}\nLength0=0\n"
try:
for line in sys.stdin:
line = line.strip()
if line.startswith('#EXTM3U'):
pass
elif line.startswith('#EXTINF'):
extinf = line
elif not line.startswith('#'):
url = line
if extinf:
title = extinf.split(',', 1)[-1]
fname = title.replace(' ', '_') + ('.pls' if pls else '.m3u')
else:
fname = url + ('.pls' if pls else '.m3u')
with open(fname, 'w') as fh:
if pls:
# Output as pls files instead of m3u
fh.write(pls_template.format(url=url, title=title))
else:
fh.write(m3u_template.format(extinf=extinf, url=url))
except Exception as ex:
print >> sys.stderr, "Error reading/operating on stdin: %s" % str(ex)
def main():
do_stdin()
if '__main__' == __name__:
main()
# vim: tabstop=8 noexpandtab shiftwidth=4 softtabstop=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment