Skip to content

Instantly share code, notes, and snippets.

@shenqi
Created November 15, 2010 21:57
Show Gist options
  • Save shenqi/701022 to your computer and use it in GitHub Desktop.
Save shenqi/701022 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -Ku
require 'rubygems'
require 'meow'
LIBDIR = '/Users/shenqi/Media/Music/' #set the path for itunes music library
def gomi_remover(string)
return (`echo "#{string}" | iconv -f UTF-8-MAC -t UTF-8`).chomp
end
def get_files(dir)
d = Dir::chdir(LIBDIR)
@dir = File::expand_path(dir)
return Dir.open(@dir).to_a.delete_if{|file|file =~ /^\./}
d.close
end
def play(path, artist, album, title, type)
puts 'Now Playing "' + title + '" from "' + album +'" by "' + artist + '" (' + type + ')'
Meow.new("playaplay").notify(artist + ' / ' + album, title )
system("afplay \"#{path}\"")
end
lists = []
files = get_files(ARGV[0])
files.each do |file|
path = @dir + '/' + file
if file =~ /(aiff|aif)/
artist = path.sub(/#{LIBDIR}(.*?)\/.*/){$1}
album = path.sub(/.*\/(.*)\/\d.*$/){$1}
t = file.sub(/(\d+?|\d-\d+?) (.*)\..*/){$2}.sub(/(.*) \d$/){$1}
title = gomi_remover(t)
n = lists.index{|item| item[3] == title}
lists.delete(n.to_i) if n
puts title + ' - aiff'
lists << [path, artist, album, title, 'aiff']
else
info = (`ffmpeg -i "#{path}" 2>&1`)
artist = info.sub(/.*artist * : (.*?)\n.*/m){$1}
t = info.sub(/.*title * : (.*?)\n.*/m){$1} #なぜかファイル名が文字化けするので。
title = gomi_remover(t)
album = info.sub(/.*album * : (.*?)\n.*/m){$1}
type = info.sub(/.*Audio: (.*), \d+? Hz.*/m){$1}
n = lists.index{|item| title.downcase === item[3].downcase}
if n
if lists[n][4] == 'aiff'
elsif type == 'alac'
lists.delete(n)
puts title + ' - alac'
lists << [path, artist, album, title, type] #使用不可文字が_に変わってると検索されない
end
else
puts title + ' - aac'
lists << [path, artist, album, title, type]
end
end
end
lists.each do |item|
play(item[0], item[1], item[2], item[3], item[4])
end
Meow.new("playaplay").notify('playaplay.rb', 'has come to the end of the playlist')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment