我正在尝试使用'mp3info‘gem读取mp3文件,在一个目录中遍历文件名以.mp3结尾的每个文件,使用Dir.chdir()进入一个目录,重复这个过程并将这些标记存储在数据库中。但我有30 to的音乐收藏,大约需要6-10分钟才能完成整个扫描。有什么方法可以优化这个扫描吗?
def self.gen_list(dir)
prev_pwd=Dir.pwd
begin
Dir.chdir(dir)
rescue Errno::EACCES
end
counter = 0
Dir[Dir.pwd+'/*'].each{|x|
#puts Dir.pwd
if File.directory?(x) then
self.gen_list(x) do |y|
yield y
end
else if File.basename(x).match('.mp3') then
begin
Mp3Info.open(x) do |y|
yield [x,y.tag.title,y.tag.album,y.tag.artist]
end
rescue Mp3InfoError
end
end
end
}
Dir.chdir(prev_pwd)
end
这是生成列表并将标签发送到数据存储在数据库中的块的方法。
发布于 2013-06-20 23:23:52
您是否尝试过将parse_mp3标志设置为false?默认情况下,它是打开的,这意味着您将为每次扫描拉入整个文件,而您所关心的只是信息。我不知道这能为你节省多少时间。有关更多信息,请参阅github源代码。
https://github.com/moumar/ruby-mp3info/blob/master/lib/mp3info.rb#L214
# Specify :parse_mp3 => false to disable processing of the mp3
def initialize(filename_or_io, options = {})
发布于 2013-06-20 14:02:02
您可以:
发布于 2013-06-20 14:36:08
你可以试试taglib-ruby gem,它不像C库上的mp3info包装器,它可以给你带来更多的性能。否则,您必须坚持使用JRuby并运行多个线程(如果您有4个内核,则运行4个线程)。
https://stackoverflow.com/questions/17205362
复制相似问题