ActiveState Code

Recipe 286234: pyM3U is a very simple Python script (usable also as a module) allowing the generation of M3U playlists


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

Python
  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)

Discussion

I wrote this to handle collections of mp3 with python, it uses getopt module to handle command line parameters, the requirements are:

pymad: a module to decode and get info on mpeg tracks. (http://spacepants.org/src/pymad/)

id3-py: reads ID3 tags from tracks. (http://id3-py.sourceforge.net/)

Comments

  1. 1. At 5:37 a.m. on 22 dec 2004, Blake Revai said:

    I modified this to add a few things.

    I modified this program to add a randomizer and to make it only add files that include at least one of the
    (optional) words at the end of the command line and to also add ogg files.
    
    I have a couple more ideas for features including:
        - allowing python regular expressions instead of search terms
        - limiting the playlist to a certain number of songs
        - limiting the playlist to a certain length in minutes and/or seconds
        - better ogg support and possibly other formats
        - adding CD burning support
    
    I have a feeling I will also use this to make a CGI program to create a playlist and either play it in xmms
    or burn it to a CD, possibly even create a batch file/shell script to download the songs from the playlist.
    
    Anyway here is my code with the mentioned additions
    
    Blake Revai
    -----------
    http://psycosys[a]gmail.com
    http://www.thenme.net
    
    *begin code*
    
    #!/usr/bin/python
    
    import os, sys, re, random, getopt, mad, ID3
    
    __doc__ = "Generate m3u playlists (default: local dir, sorted)"
    __author__ = r"Lawrence Oluyede , modified by Blake Revai "
    __date__ = "Jul 12 2004, modified Dec 22 2004 to add randomize, search and to also add oggs as well as mp3s"
    __version__ = "0.2mod1"
    
    """
    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"
    

    (comment continued...)

  2. 2. At 5:37 a.m. on 22 dec 2004, Blake Revai said:

    (...continued from previous comment)

    def _usage():
        """ print the usage message """
        msg = "Usage:  python pyM3U.py [options] playlist_name [path] [PATTERN]\n"
        msg += __doc__ + "\n\n"
        msg += "Only include files with at least one word from PATTERN in ID3 artist, title or filename.\n"
        msg += "Example: python pyM3U.py playlist_name.m3u /path/to/mp3s music good \n"
        msg += "This example creates a sorted playlist called playlist_name.m3u from any mp3s in /path/to/mp3s/ with the word \"music\" or \"good\" in the ID3 artist, title, or filename.\n\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" % ("-r", "--rand", "randomize list")
        msg += "%5s,\t%s\t\t\t%s\n" % ("-w", "--walk", "walk into subdirs (default: no walk)")
        msg += "%5s,\t%s\t\t\t%s" % ("-h", "--help", "display this help and exit")
    
        print msg
    
    def generate_list(name="songs_list.m3u", path=".",
                      sort=True, walk=False, regExp="", rand=False):
        """ generates the M3U playlist with the given file name
    
        and in the given path """
    
        output = 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" or i[-3:] == "ogg"]
                else:
                    # non recursive version
                    mp3_list = [i for i in os.listdir(path) if i[-3:] == "mp3"]
    
                #print mp3_list
    
                if sort and not rand:
                    mp3_list.sort()
    
                output = file(name, "w")
                output.write(FORMAT_DESCRIPTOR + "\n")
    
            if rand:
            random.shuffle(mp3_list)
    
                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
    
                wanted=re.compile(regExp, re.IGNORECASE)
    

    (comment continued...)

  3. 3. At 5:37 a.m. on 22 dec 2004, Blake Revai said:

    (...continued from previous comment)

    #       if wanted.match(artist)!=None or wanted.match(title)!=None or wanted.match(track)!=None: print artist + " - " +title+":"+track
            # match command line parameters to artist, title or filename
            if wanted.match(artist)!=None or wanted.match(title)!=None or wanted.match(track)!=None:
                if artist and title:
                            output.write(RECORD_MARKER + ":" + str(track_length) + "," + artist + " - " + title + "\n")
                        else:   # use filename if no artist and title
                            output.write(RECORD_MARKER + ":" + str(track_length) + "," + os.path.basename(track)[:-4] + "\n")
    
                        # write the fullpath
                        output.write(track + "\n")
    
            except (OSError, IOError), e:
                print e
        finally:
            if output:
                output.close()
    
    if __name__ == "__main__":
        if len(sys.argv) == 1:
            sys.stderr.write("No playlist name given\n")
            sys.exit(1)
    
        options = "nhwr"
        long_options = ["no-sort", "help", "walk", "rand"]
    
        try:
            opts, args = getopt.getopt(sys.argv[1:], options, long_options)
        except getopt.GetoptError:
            print "error"
            _usage()
            sys.exit(2)
    
        name, path, sort, regExp, rand, walk = "songs_list.m3u", ".", True, "", False, False
    
        #print opts, args
    
        # check cmd line args
        for o, a in opts:
            if o in ("-n", "--no-sort"):
                sort = False
        if o in ("-r", "--rand"):
            rand = True
            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
    
        try:
        regExp = ''
        for i in range(2,len(args)):    #insert regExp characters and params
                regExp += ".*" + args[i] + ".*"
            if i!=len(args)-1:      #insert or between
             regExp+= "|"
        except:
        pass
    
        #print name, path, sort
    
        if os.path.exists(path):
            generate_list(name, path, sort, walk, regExp, rand)
        else:
            sys.stderr.write("Given path does not exist\n")
            sys.exit(2)
    
    *end code*
    

Sign in to comment