Simple M3U exporter help needed - 1 track per .m3u


#1

I am trying to write a relatively simple .m3u exporter but I'm having trouble grasping the export syntax.

I am attempting to:

-load a folder of mp3s (and subfolders) into mp3tag (i got that part :wink: )
-export an .m3u file for each file into the same folder as the track itself. Each m3u file will contain only one item.

The URL in the M3U will be a combined path of a URL prefix+subfolder+filename.ext

For example, suppose I edit the .mte and specify that my URL prefix is:

http://www.mysite.com/media/

and mp3tag is showing the contents and subfolders of:

C:\Documents and Settings\User\Desktop<!--coloro:#CC6600-->samples

and I use my M3U exporter on the files:

C:\Documents and Settings\User\Desktop<!--coloro:#CC6600-->samples\album_one\01-The_Good_Song.mp3
C:\Documents and Settings\User\Desktop<!--coloro:#CC6600-->samples\album_one\02-The_Bad_Song.mp3

then these m3u files should be the result:

#EXTM3U
#EXTINF:0,%artist% - %track%

http://www.mysite.com/media/samples/album_one/01-The_Good_Song.mp3

#EXTM3U
#EXTINF:0,%artist% - %track%

http://www.mysite.com/media/samples/album_one/02-The_Bad_Song.mp3

A simple naming for the .m3u files is fine such as %_filename%.m3u, I don't mind the double extension that will result.

The reason for this exporter is I want to link some audio samples in mp3 format to .m3u files rather than directly link to the mp3s. In some browser configurations, when users click the mp3 it just downloads in the background without letting the user know the progress and they seem to give up after a while. This will ensure it opens outside the browser.

If there is anything that I could do to make the code simpler, such as ensuring that the subfolders and filenames could be accurately generated using the tags I would be happy to do so.

Thanks for any help you can provide. My .mte files seem to either create only a single .m3u or crash mp3tag.


#2

Mp3tag can only make one file per directory, but not one file per audio file.


#3

Thanks for the quick reply. Any idea how to achieve this? I thought I could just use the mail merge function of MS Word + a CSV file but it doesn't allow you to export the results as files, you must print them or email them. Otherwise it would work great.


#4

One way to achieve this is with something like python.
Download and install a python interpreter (just go to activestate.com)
Copy/paste the code I've appended below into a python file, call it, say "m3u.py". Change the string that's currently '\BLAH\BLAH\BLAH' to the url you need.
Use Mp3Tag to create mp3tag.m3u (default Mp3Tag name for a m3u file).
on the command line, type m3u mp3tag.m3u - then press [enter].
Have fun!
Bob

PS: you could have done exactly the same thing with a sed script, but sed's a PITA ...

#!/usr/bin/python
import sys
import re
import os

rM3uTuneInfo = re.compile(r"""
  \#EXTINF\:(\d+),(.*)\n(.*)
  """, re.VERBOSE)
  
rFilename = re.compile(r"""
  ^(.*)\.(.*)
  """, re.VERBOSE)
  
sPrefix = 'BLAH\BLAH\BLAH\'

def main():

  if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
    fn = os.path.abspath(sys.argv[1])
    s = os.path.split(fn)
    sPath = s[0]
    fIn=PrivoxyWindowOpen(fn, 'r')
    sBuf = fIn.read()
    fIn.close()
  
    for m in rM3uTuneInfo.finditer(sBuf):
      title = m.group(2)
      length = m.group(1)
      filename = m.group(3)
      f = rFilename.match(m.group(3))
      if f:
        name = f.group(1) # filenanme
        ext = f.group(2) # extension
        fOut = PrivoxyWindowOpen(sPath +'\\\\' + name, 'w')
        fOut.write('#EXTM3U\n')
        sTmp = '#EXTINF:' + length + ',' + title + '\n'
        fOut.write(sTmp)
        sTmp = sPrefix + filename + '\n'
        fOut.write(sTmp)
        fOut.close()

if __name__ == "__main__":
  main()