我正在使用 taglib-ruby 来查看您每天可以听一首歌多少次。我让它适用于单首歌曲,但现在我正在尝试修复它,以便它可以遍历目录并吐出每首歌曲的长度以及每天可以收听多少次。它运行时不会抛出错误,但也不会输出任何内容。我不认为它出于某种原因看到这些文件。我尝试使用 foreach 并不断收到错误消息:
$ ruby songtime.rb /media/ab/storage/Music/Between\ the\ Buried\ and\ Me/[2007]\ Colors/
songtime.rb:9:in `block (2 levels) in <main>': undefined method `length' for nil:NilClass (NoMethodError)
from /home/ab/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/taglib-ruby-0.7.0/lib/taglib/base.rb:8:in `open'
from songtime.rb:7:in `block in <main>'
from songtime.rb:4:in `foreach'
from songtime.rb:4:in `<main>'
如果我只是将目录名称硬编码到程序中,也会出现同样的问题,类似于:
#!/usr/bin/ruby
require "taglib"
Dir.foreach(ARGV[0]) do |songfile|
next if songfile == '.' or songfile == '..'
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{(24*60*60/songLength * 1000).floor / 1000.0} times per day."
end
end
所以我尝试切换到 glob:
#!/usr/bin/ruby
require "taglib"
Dir.glob("#{ARGV[0]}*.mp3") do |songfile|
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{(24*60*60/songLength * 1000).floor / 1000.0} times per day."
end
end
这是行不通的。没有错误消息,但没有打印任何内容。如果我把它也不起作用
#!/usr/bin/ruby
require "taglib"
Dir.glob("/media/ab/storage/Music/Between the Buried and Me/[2007] Colors/*.mp3") do |songfile|
TagLib::FileRef.open(songfile) do |mp3|
properties = mp3.audio_properties
songLength = properties.length
puts "Song length is #{songLength}"
puts "You can listen to this song #{24*60*60/songLength} times per day."
end
end
同意这个吗?对不起,我是一个菜鸟菜鸟。