Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- mp3sorter.coffee - Sorts mp3 files into directories as dir/artist/album/title.mp3
- Original written in C by Speed
- by justin_
- Run the program twice if there are sync issues.
- '''
- fs = require 'fs'
- # Trims the junk.
- trimData = (data) ->
- data[0..2] = []
- String::removeJunk = ->
- this.replace /\u0000/gi, ''
- # Parses the MP3s and sorts the songs.
- sort = (filePath, oldDir, destDir) ->
- fs.readFile filePath, (err, data) ->
- parseData = data[(data.length - 128)..]
- change = 28
- offset = change
- trimData parseData
- title = parseData[0..offset].toString('utf-8', 0).removeJunk()
- artist = parseData[offset..(offset + change)].toString('utf-8', 0).removeJunk()
- offset += change
- album = parseData[offset..(offset + change)].toString('utf-8', 0).removeJunk()
- console.log "Title: #{title}\tArtist: #{artist}\tAlbum: #{album}"
- baseDir = "#{__dirname}/#{destDir}"
- fs.exists baseDir, (exists) ->
- if not exists
- fs.mkdir baseDir, (err) ->
- fs.exists "#{baseDir}/#{artist}", (exists) ->
- if not exists
- fs.mkdir "#{baseDir}/#{artist}", (err) ->
- fs.exists "#{baseDir}/#{artist}/#{album}", (exists) ->
- if not exists
- fs.mkdir "#{baseDir}/#{artist}/#{album}", (err) ->
- fs.writeFile "#{baseDir}/#{artist}/#{album}/#{title}.mp3", data, (err) ->
- console.log err
- argc = process.argv.length - 2
- argv = process.argv[2..]
- if argc is 0
- console.log 'Usage: mp3sorter in-dir [out-dir]'
- else if argc >= 1
- dir = if argc is 1 then __dirname else argv[argc - 1]
- console.log "Output directory: #{dir}"
- path = "#{__dirname}/#{argv[0]}"
- fs.lstat "#{path}", (err, stats) ->
- if stats.isDirectory()
- fs.readdir path, (err, files) ->
- for item in files
- if /\.mp3$/.test item
- console.log "Processing #{item}"
- sort "#{path}/#{item}", path, dir
- else
- console.log 'nope'
Advertisement
Add Comment
Please, Sign In to add comment