Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env lua
- -- Copy your MP3s from src folder to dest folder, where they will be
- -- sorted into subdirectory structure: Dest_dir/genre/album/song.mp3
- require("lfs") --sudo apt-get install liblua5.1-filesystem0
- math.randomseed(123)
- local function sanitize(str)
- return (str:gsub("[^a-zA-Z0-9. ]","_"))
- end
- local function get_tag(tagname, str)
- local val = str:match(" %("..tagname.."%)%: (.-)>")
- if val=="" then
- val = false
- end
- return val
- end
- local function scan_dir(directory, albums)
- print("Entering directory: "..directory)
- for fname in lfs.dir(directory) do
- if fname ~= "." and fname ~= ".." then
- local attrs = lfs.attributes(directory..fname)
- if attrs.mode == "directory" then
- scan_dir(directory..fname.."/", albums)
- else
- --print (fullname)
- assert(fname:match("mp3$"))
- --print(fullname)
- local escaped = fname:gsub("%$","\\$")
- escaped = escaped:gsub("`","\\`")
- -- print (escaped)
- local fd = assert(io.popen("eyeD3 -v \""..directory..escaped.."\""))
- local txt = fd:read("*a")
- fd:close()
- assert(txt:match("ID3"))
- local tags = {}
- for i,tagname in ipairs{"TPE1", "TPE2", "TALB", "TCON"} do
- tags[tagname] = get_tag(tagname, txt)
- end
- --print(tags.TPE2)
- local album = tags.TALB or "_no_album_"..math.random(999999)
- local genre = tags.TCON or "_no_genre"
- -- local dirname = sanitize(genre).."/"..sanitize(album)
- album = genre.."/"..album
- if not albums[album] then
- albums[album] = {}
- end
- table.insert(albums[album],{dir=directory,fname=escaped, genre=genre})
- end
- end
- end
- end
- local function main()
- local dir_src = assert(arg[1])
- local dir_dest = assert(arg[2])
- assert(dir_src ~= dir_dest)
- assert(dir_src:match("/$"))
- assert(dir_dest:match("/$"))
- local albums = {}
- scan_dir(dir_src, albums)
- for album, songs in pairs(albums) do
- table.sort(songs,function(a,b) return (a.fname < b.fname) end)
- --print(#songs, album)
- local destdir = album
- assert(#destdir > 0)
- -- destdir = dir_dest..destdir:sub(1,1).."/"..destdir.."/"
- destdir = dir_dest..destdir.."/"
- print("===Copying "..#songs.." songs in album: "..destdir)
- local stat, err = os.execute('mkdir -p "'..destdir..'"')
- if not stat then
- print(err)
- else
- for i, song in ipairs(songs) do
- local destfname = destdir..sanitize(song.fname)
- if lfs.attributes(destfname) then
- print("File exists, skipping: "..destfname)
- else
- local command = 'cp "'..song.dir..song.fname..'" "'..destfname..'"'
- print(command)
- os.execute(command)
- end
- end
- end
- end
- end
- main()
- --[[
- -------------------------------------------------------------------------------
- ID3 Frames:
- <Title/songname/content description (TIT2): The Terrace>
- <Lead performer(s)/Soloist(s) (TPE1): John Williams>
- !!! Album artist = TPE2
- <Composer (TCOM): John Williams>
- <Album/Movie/Show title (TALB): Superman>
- <Year (TYER): >
- <Track number/Position in set (TRCK): 3/0>
- <Part of a set (TPOS): 2/0>
- <Content type (TCON): Soundtrack>
- <Attached picture Frame (APIC)>
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement