Skip to content

Instantly share code, notes, and snippets.

@Deconstrained
Created December 7, 2018 06:30
Show Gist options
  • Save Deconstrained/c3b66ca38721fdab93bcb15e7b80d2df to your computer and use it in GitHub Desktop.
Save Deconstrained/c3b66ca38721fdab93bcb15e7b80d2df to your computer and use it in GitHub Desktop.
Python script to convert a M3U into the XML format that Brasero uses for its project files, so that one can burn a playlist to a data CD. YMMV.
#!/usr/bin/env python3
import argparse
import os
import sys
import urllib.parse as url
import xml.etree.ElementTree as ET
def main():
ap = argparse.ArgumentParser(description="Convert an M3U file to a Brasero "
"data disc project file.")
ap.add_argument('-l', '--label', default="Data disc", help="Disc label")
ap.add_argument('-b', '--basedir', default=os.getcwd(), help="Base "
"directory, relative to which are the paths to the files listed in the "
"playlist file.")
ap.add_argument('m3u', type=argparse.FileType('r'), help="Playlist file")
args = ap.parse_args()
root = ET.Element('braseroproject')
version = ET.Element('version')
version.text = '0.2'
label = ET.Element('label')
label.text = args.label
root.append(version)
root.append(label)
tracklist = ET.Element('track')
datalist = ET.Element('data')
excludes = []
for line in args.m3u:
path = ET.Element('path')
path.text = '/' + line.strip()
uri = ET.Element('uri')
uri.text = url.quote('file://'+os.path.join(args.basedir, path.text))
graft = ET.Element('graft')
graft.append(path)
graft.append(uri)
datalist.append(graft)
excluded = ET.Element('excluded')
excluded.text = uri.text
excludes.append(excluded)
for exclude in excludes:
datalist.append(exclude)
tracklist.append(datalist)
root.append(tracklist)
# Print it all out
print('<?xml version="1.0" encoding="UTF-8"?>')
print(ET.tostring(root).decode('utf-8'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment