This blog is subject the DISCLAIMER below.

Monday, August 25, 2008

Playlist creation Mobile App in Python !

This is a simple program i have made for my mobile phone (Nokia N70).

To cut a long story short, i have a a 1GB memory card more than half of them are mp3's, and i wanted a simple program to organize them (Playlist for each folder). At the same time, i wanted to play with something new and non-usual ( ! J2ME), i wanted to write it in python :) , and it turned out to be very easy.



  • At first i needed to install the suitable python runtime for my phone, and found this link (provides some information about your device, including the development platform). Then i had to download it from here . You just need to install it on your cell phone just as any other program.

  • Then i wrote the program in python and tested it on my Ubuntu PC and here is the code. I forgot to mention that playlist files have a ".m3u" extension and are only a text file containing a list of mp3(|| m4a) files separated by a new line character.


    import os
    import sys

    # Test folder
    searchDirectory="/media/sda6/Songs/fdsfsd"

    if not os.path.isdir ( searchDirectory ) :
    print searchDirectory + " is not a directory."
    sys.exit ( 0 )

    m3uFilename = os.path.split ( searchDirectory )[1] + '.m3u'

    # Get all files.
    dirList=os.listdir(searchDirectory)

    fileList = []

    # Get mp3 or m4a files only.
    for fname in dirList:
    if fname.lower().endswith ( '.mp3' ) or fname.lower().endswith ( '.m4a' ) :
    fileList.append ( fname )

    # Begin : Generate the m3u file.
    m3uFile = open( searchDirectory + os.path.normcase('/') + m3uFilename ,'w')

    filesAdded = 0

    for aFile in fileList:
    print 'Adding ' + aFile
    m3uFile.write ( aFile + '\n' )
    filesAdded = filesAdded + 1

    print 'Files Added : ' + str ( filesAdded )

    m3uFile.close()
    # End : Generate the m3u file.

  • Third, I needed a simple control to select the files visually, after a 5 minute googling i found this fileselector control. and I needed to add only two extra lines of code :D , Those -->

    import fileselector
    searchDirectory = fileselector.fileselect()

  • Fourth, Put them on a folder on your memory card, then from your cell phone install fileselector.py (remember you got it from the third step) and install it as a python lib, and install m3ucreator as a script. Then run python on your phone, select options --> Run Script and you will find it as my\m3ucreator.py . When you run it the file selector should open, after you select your folder it will report how many files are added to the playlist. And when you open Nokia Music Manager the next time, you will find it in the list of playlists.

  • Fifth, There is no fifth step :) . I just want to note that there is no code changes from the desktop version except in the folder selection (THIS IS GREAT) . Here is the code listing

    import os
    import sys

    # Begin : Mobile Specific Code.
    import fileselector
    searchDirectory = fileselector.fileselect()
    # End : Mobile Specific Code.

    if not os.path.isdir ( searchDirectory ) :
    print searchDirectory + " is not a directory."
    sys.exit ( 0 )

    m3uFilename = os.path.split ( searchDirectory )[1] + '.m3u'

    # Get all files.
    dirList=os.listdir(searchDirectory)

    fileList = []

    # Get mp3 or m4a files only.
    for fname in dirList:
    if fname.lower().endswith ( '.mp3' ) or fname.lower().endswith ( '.m4a' ) :
    fileList.append ( fname )

    # Begin : Generate the m3u file.
    m3uFile = open( searchDirectory + os.path.normcase('/') + m3uFilename ,'w')

    filesAdded = 0

    for aFile in fileList:
    print 'Adding ' + aFile
    m3uFile.write ( aFile + '\n' )
    filesAdded = filesAdded + 1

    print 'Files Added : ' + str ( filesAdded )

    m3uFile.close()
    # End : Generate the m3u file.

    Enjoy it :)
Note that the code is not displayed properly here. Python code blocks depend on indentation.

1 comment:

Shady M. Najib said...

mmmm.. python!! Sheref fe thawbehe l jaded :D