christopher-b / flac-to-itunes

A simple automator workflow to import FLAC files to iTunes
1 stars 0 forks source link

no implicit conversion of nil into string typeerror #2

Closed duenni closed 9 years ago

duenni commented 10 years ago

Hi!

I used your workflow to generate an Automator Service for converting FLAC to MP3. It worked until Yosemite came out. I have a "Run Shell Script"-Action with the following:

# Set the following to the folder containing metaflac, flac, flac2mp3 and lame binaries. 
# Install flac from homebrew
flac_bin = "/usr/local/bin"

# Change working folder so the files will be saved in the same folder
export_folder = File.dirname(ARGV.first)
Dir.chdir(export_folder)

# Set path
path = ENV['PATH']
path = "#{path}:#{flac_bin}"
ENV['PATH'] = path
ARGV.each do |file|
    next if File.extname(file) != '.flac'
    `flac2mp3 "#{file}"`
    puts export_folder + "/" + File.basename(file,'.*')+".mp3"
end

And now I get the following error dirname: no implicit conversion of nil into String (TypeError) on line export_folder = File.dirname(ARGV.first) when running this with Automator. Any idea? I am in no way a ruby expert. Thanks in advance!

christopher-b commented 10 years ago

Hi duenni, This action is meant to be run as a folder action, which is triggered when files are added to a specific folder. It looks like Automator doesn't actually pass the contents of the folder into the ruby script when you run it from Automator. I'm not sure if this behaviour is new to Yosemite.

Does it work for you when you run it as a folder action? Some tweaking would need to be done to get it to work in a different context.

duenni commented 9 years ago

Yes it works as a folder action. Ok, to run it from Automator you have to put "Get selected Finder Items" as first step for testing. This works as well. I just like to run the shell script as a Service and I also don't want to delete the FLAC file. I think this worked like a charm before....but now if I right click a FLAC file and select the Service it is instantly done and nothing happens. I double checked the homebrew path containing the flac bin.

christopher-b commented 9 years ago

Ok, this should work:

flac_bin = "/usr/local/bin"
ENV['PATH'] = "#{ENV['PATH']}:#{flac_bin}"

ARGF.each do |file|
  file.chomp!
  next unless File.extname(file) == '.flac'
  export_folder = File.dirname(file)
  Dir.chdir(export_folder)
  system({"PATH"=>ENV['PATH']}, "flac2mp3", file)
  puts export_folder + "/" + File.basename(file,'.*')+".mp3"  
end

screen shot 2014-11-27 at 4 56 15 pm

duenni commented 9 years ago

Thank you very much! Really appreciate it!

tim-tation commented 9 years ago

Thanks alot, but how is it now possible to adjust the quality? Set quality between "#{file}“ and `: V2, V1, V0, 320 doesn't work anymore.

christopher-b commented 9 years ago
system({"PATH"=>ENV['PATH']}, "flac2mp3", file, "V0")

should do it.

tim-tation commented 9 years ago

Wow thanks, that was fast! 320 ist still the best, right?

christopher-b commented 9 years ago

This page explains the differences. 320 will give you a constant 320kbps, while V0 will output a variable bitrate MP3 at around 245kbps. 320 will also increase the file size.

According to the link above, there is rarely a perceptible difference between V0, V1 and V2, and "no one has produced ABX test results demonstrating that perceived quality [of 320 CBR] is ever better than the highest VBR profiles described above".

So, if you don't mind the larger files, go with 320. Otherwise, stick with the default: V0.