I'm trying to create a simply ruby script to run an ffmpeg command through bash to convert an audio file from one format to another.
the command is ffmpeg -i (in file) -acodec libmp3lame -ab 64k -ar 48000 -ac 1 AAA_S00E00_Podcast.mp3
I have a ruby script with the right permissions and that I can call (tested it with a system ls
call before moving onto the ffmpeg attempt)
#!/bin/ruby
def mkmp3( one = "", two = "" )
system "ffmpeg -i #{one} -acodec libmp3lame -ab 64k -ar 48000 -ac 1 #{two}.mp3"
end
mkmp3
but when I call it from bash trying to convert a file called session.flac
to smoochie.mp3
I get back:
mkmp3.rb ('session.flac', 'smoochie')
bash: syntax error near unexpected token `'session.flac','
The error is because you are using parenthesis (
and )
to add arguments to your ruby script. Remove them and the error will be gone; in fact, you only need to specify your strings separated by a space (otherwise you will get the comma ,
as a string):
$ mkmp3.rb session.flac smoochie
Now, to use those parameters you need to add ARGV
in your script, like this:
#!/bin/ruby
def mkmp3( one = ARGV[0], two = ARGV[1] )
system "ffmpeg -i #{one} -acodec libmp3lame -ab 64k -ar 48000 -ac 1 #{two}.mp3"
end
mkmp3
ARGV
will contain an array of strings with the arguments you added, consider this script (test.rb
):
#!/bin/ruby
puts ARGV.inspect
Execute the script:
$ ruby so.rb one two
And the output will be:
["one", "two"]
So you access each value using the array index (i.e. ARGV[0]
and ARGV[1]
).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments