Skip to content

Instantly share code, notes, and snippets.

@ruzzbot
Created August 2, 2012 16:48
Show Gist options
  • Save ruzzbot/3238609 to your computer and use it in GitHub Desktop.
Save ruzzbot/3238609 to your computer and use it in GitHub Desktop.
A small script to find music files and store them to couchDB
fs = require 'fs'
ID3 = require 'node-id3'
findit = require 'findit'
cradle = require 'cradle'
db = new(cradle.Connection)().database('music')
allowed_files = [ 'mp3', 'aiff', 'wav', 'ogg' ]
mnt = [
'/Users/<username>/Music',
'/Users/<username>/Podcasts' ]
# Report DB Changes
feed = db.changes( )
feed.on 'change',(change)->
console.log( "\n~~~~ FEED SAYS:\n", change )
# send to storage
uploadDoc = ( doc )->
db.save doc.stats.dev+'-'+doc.stats.ino, doc, (err, res)->
if err then console.log err, res
# preform any cleanup / data-munging
preProcess = ( file )->
fs.readFile file, ( err, data )->
if !err
tags = (new ID3( data )).getTags()
delete tags[ '\u0000\u0000\u0000' ] # removes unused data
uploadDoc({
"filename": file.split('/').pop()
"extension": file.split('.').pop()
"path": file
"stats": fs.statSync file
"id3": tags
}, data )
# Traverse Mount Points
ferretFiles = ( path )->
findit.find path, ( file )->
file_ext = file.split( '.' ).pop()
if file_ext in allowed_files then preProcess( file )
# Loop mount points and process files
ferretFiles path for path in mnt
###
REFERENCES:
AUDIO ENCODING TYPES
:: http://webdesign.about.com/od/sound/a/sound_mime_type.htm
:: http://en.wikipedia.org/wiki/Internet_media_type
audio/basic: mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046
audio/L24: 24bit Linear PCM audio at 8-48kHz, 1-N channels; Defined in RFC 3190
audio/mp4: MP4 audio
audio/mpeg: MP3 or other MPEG audio; Defined in RFC 3003
audio/ogg: Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334
audio/vorbis: Vorbis encoded audio; Defined in RFC 5215
audio/vnd.rn-realaudio: RealAudio; Documented in RealPlayer Help[8]
audio/vnd.wave: WAV audio; Defined in RFC 2361
audio/webm: WebM open media format
LINUX STAT
:: http://linux.die.net/man/2/stat
dev_t - ID of device containing file
ino_t - inode number
mode_t - protection
nlink_t - number of hard links
uid_t - user ID of owner
gid_t - group ID of owner
dev_t - device ID (if special file)
off_t - total size, in bytes
blksize_t - blocksize for file system I/O
blkcnt_t - number of 512B blocks allocated
time_t - time of last access
time_t - time of last modification
time_t - time of last status change
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment