Skip to content

Instantly share code, notes, and snippets.

@Decad
Created April 19, 2012 18:43
Show Gist options
  • Save Decad/2422921 to your computer and use it in GitHub Desktop.
Save Decad/2422921 to your computer and use it in GitHub Desktop.
Make a vlc playlist for a directory
from xml.dom.minidom import Document
import os
import sys
validExt = ('.avi') #only add files with these extensions to the playlist
if(len(sys.argv) > 1):
rootdir = sys.argv[1]+'/'
print(sys.argv[1])
else:
rootdir = os.getcwd()+'/'
def createTrack(file, path):
track = doc.createElement("track")
tracklist.appendChild(track)
location = doc.createElement("location")
locationText = doc.createTextNode('file:///'+path+file)
location.appendChild(locationText)
track.appendChild(location)
doc = Document()
playlist = doc.createElement("playlist")
playlist.setAttribute("version","1")
playlist.setAttribute("xmlns","http://xspf.org/ns/0/")
playlist.setAttribute("xmlns:vlc","http://www.videolan.org/vlc/playlist/ns/0/")
doc.appendChild(playlist)
title = doc.createElement("title")
titlet = doc.createTextNode("Playlist")
title.appendChild(titlet)
playlist.appendChild(title)
tracklist = doc.createElement("trackList")
playlist.appendChild(tracklist);
for subdir, dirs, files in os.walk(rootdir):
for file in files:
ext = os.path.splitext(file)
if(ext[1] in validExt):
createTrack(file, subdir)
f = open(rootdir+"playlist.xspf",'w')
f.write(doc.toxml())
f.close()
@BillBlight
Copy link

BillBlight commented Jul 22, 2018

I know that this is an old project, but I have recently found it very useful , with one minor change ..

if you insert

files.sort()

right after line 34 your playlist is created in the same alphabetical order as the directory listing, which makes playing the files in the order intended much easier ..

another better way in the case there are numbers as well as letters is importing natsort and using

sortfiles = natsort.natsorted(files)
    for file in sortfiles:

Thanks for this ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment