[Rhythmbox-devel] Manage directory structure?



Hi.

I would like to start by saying that Rhythmbox is a great music player -
keep up the good work. However, I would like to see a feature included
that will structure your music file directories, much like the display
within Rhythmbox. That is:
	Artist
	+-Album
  	  +-01 - Track1
  	    02 - Track2
I have written a small Python script to demonstrate what I mean. It is
attached.

>From Aiden
#!/usr/bin/env python

# musiksort.py

import os
import sys
from mutagen.flac import FLAC
from mutagen.easyid3 import EasyID3
from mutagen.apev2 import APEv2
from mutagen.oggvorbis import OggVorbis
from mutagen.asf import ASF

def main():
    root = sys.argv[1]
    tmp = os.path.join(root, "tmp")
    os.mkdir(tmp)
    
    walklist = os.walk(root)
    
    for dirs in walklist:
        if(dirs[2]):
            for file in dirs[2]:
                os.rename(os.path.join(dirs[0], file), os.path.join(tmp, file))
    
    walklist = os.walk(root, False)
    
    for dirs in walklist:
        if(dirs[1]):
            for folder in dirs[1]:
                if folder != "tmp":
                    os.rmdir(os.path.join(dirs[0], folder))
    
    walklist = os.walk(tmp)
    
    for dir in walklist:
        dir = dir[2]
    
    for file in dir:
        fullpath = os.path.join(tmp, file)
        extension = file[-4:]
        
        if extension == ".mp3":
            try:
                tag = EasyID3(fullpath)
                parseMusic(fullpath, root, tag, "mp3")
            except:
                print "No tag: " + fullpath
        elif extension == ".ogg" or extension == ".vob":
            tag = OggVorbis(fullpath)
            parseMusic(fullpath, root, tag, "ogg")    
        elif extension == "flac":
            tag = FLAC(fullpath)
            parseMusic(fullpath, root, tag, "flac")  
        elif extension == ".mpc":
            tag = APEv2(fullpath)
            parseMusic(fullpath, root, tag, "mpc")
        elif extension == ".asf":
            tag = ASF(fullpath)
            parseMusic(fullpath, root, tag, "asf")
        else:
            parseUnknown(fullpath, root)
            
    os.rmdir(tmp)

def parseUnknown(fullpath, root):
    newpath = os.path.join(root, "Unknown")
    if not os.path.exists(newpath):
        os.makedirs(newpath)  
    file = fullpath.split(os.path.sep)
    file = file[-1]
    os.rename(fullpath, os.path.join(newpath, file))
    
def parseMusic(fullpath, root, tag, extension):
    noTags = False
    
    if len(tag) == 0:
        noTags = True

    if noTags:
        #get some info
        parseUnknown(fullpath, root)
    else:
        #rename file
        #track - title.extension
        
        try:
            artist = tag['ARTIST']
            artist = artist[0]
        except:
            artist = "Unknown"
        
        try:
            album = tag['ALBUM']
            album = album[0]  
        except:
            album = "Unknown"      

        try:
            title = tag['TITLE']
            title = title[0]
        except:
            title = "Unknown"

        try:
            track = tag['TRACKNUMBER']
            track = track[0]
        except:
            track = ""
        
        if len(track) == 1:
            track = "0" + track        
        
        name = track + " - " + title + "." + extension
        
        newpath = os.path.join(root, artist, album)
        
        if not os.path.exists(newpath):
            os.makedirs(newpath)
        name = name.replace("/", "-")
        os.rename(fullpath, os.path.join(newpath, name))  

if __name__ == "__main__":
    main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]