With this script you can walk into tree subdirectories and generate a playlist (m3u format) that can be read with players like xmms, winamp and so on.
The description of the format is here: http://hanna.pyxidis.org/tech/m3u.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import os
import sys
import getopt
import mad
import ID3
__doc__ = "Generate m3u playlists (default: local dir)"
__author__ = "Lawrence Oluyede <l.oluyede@gmail.com>"
__date__ = "Jul 12 2004"
__version__ = "0.2"
"""
A simple m3u file is like this:
#EXTM3U
#EXTINF:111,Coldplay - In Myplace
/path/to/the/song/Coldplay - In My Place.mp3
- #EXTM3U is the format descriptor (unchanging)
- #EXTINF is the record marker (with extended info, unchanging)
- : is the separator
- 111 is the length of the track in whole seconds
- , is the other separator
- the name of the track (a good generator parses the ID3,
if there isn't an ID3 use the file name without the extension)
- /path/etc. etc. is the absolute (or relative) path to the file name
of the track
Requirements:
- Python 2.2
- pymad for track length - http://spacepants.org/src/pymad/
- id3-py for reading ID3 infos - http://id3-py.sourceforge.net/
"""
FORMAT_DESCRIPTOR = "#EXTM3U"
RECORD_MARKER = "#EXTINF"
def _usage():
""" print the usage message """
msg = "Usage: pyM3U.py [options] playlist_name [path]\n"
msg += __doc__ + "\n"
msg += "Options:\n"
msg += "%5s,\t%s\t\t%s\n" % ("-n", "--no-sort", "do not sort entries by filename")
msg += "%5s,\t%s\t\t\t%s\n" % ("-w", "--walk", "walk into subdirs (default: no walk)")
msg += "\n%5s,\t%s\t\t\t%s\n\n" % ("-h", "--help", "display this help and exit")
print msg
def generate_list(name="songs_list.m3u", path=".",
sort=True, walk=False):
""" generates the M3U playlist with the given file name
and in the given path """
fp = None
try:
try:
if walk:
# recursive version
mp3_list = [os.path.join(root, i) for root, dirs, files in os.walk(path) for i in files \
if i[-3:] == "mp3"]
else:
# non recursive version
mp3_list = [i for i in os.listdir(path) if i[-3:] == "mp3"]
#print mp3_list
if sort:
mp3_list.sort()
fp = file(name, "w")
fp.write(FORMAT_DESCRIPTOR + "\n")
for track in mp3_list:
if not walk:
track = os.path.join(path, track)
else:
track = os.path.abspath(track)
# open the track with mad and ID3
mf = mad.MadFile(track)
id3info = ID3.ID3(track)
# M3U format needs seconds but
# total_time returns milliseconds
# hence i convert them in seconds
track_length = mf.total_time() / 1000
# get the artist name and the title
artist, title = id3info.artist, id3info.title
# if artist and title are there
if artist and title:
fp.write(RECORD_MARKER + ":" + str(track_length) + "," +\
artist + " - " + title + "\n")
else:
fp.write(RECORD_MARKER + ":" + str(track_length) + "," +\
os.path.basename(track)[:-4] + "\n")
# write the fullpath
fp.write(track + "\n")
except (OSError, IOError), e:
print e
finally:
if fp:
fp.close()
if __name__ == "__main__":
if len(sys.argv) == 1:
sys.stderr.write("No playlist name given\n")
sys.exit(1)
options = "nhw"
long_options = ["no-sort", "help", "walk"]
try:
opts, args = getopt.getopt(sys.argv[1:], options, long_options)
except getopt.GetoptError:
print "error"
_usage()
sys.exit(2)
name, path, sort = "songs_list.m3u", ".", True
walk = False
#print opts, args
# check cmd line args
for o, a in opts:
if o in ("-n", "--no-sort"):
sort = False
if o in ("-w", "--walk"):
walk = True
if o in ("-h", "--help"):
_usage()
sys.exit(1)
try:
name = args[0]
except:
pass
try:
path = args[1]
except:
pass
#print name, path, sort
if os.path.exists(path):
generate_list(name, path, sort, walk)
else:
sys.stderr.write("Given path does not exist\n")
sys.exit(2)
|
I wrote this to handle collections of mp3 with python, it uses getopt module to handle command line parameters, the requirements are:
I modified this to add a few things.
(comment continued...)
(...continued from previous comment)
(comment continued...)
(...continued from previous comment)