使用Ruby和变量编写bash脚本以运行FFMPEG命令
我试图创建一个简单的ruby脚本,通过bash运行ffmpeg命令,将音频文件从一种格式转换为另一种格式 命令是使用Ruby和变量编写bash脚本以运行FFMPEG命令,ruby,linux,bash,ffmpeg,Ruby,Linux,Bash,Ffmpeg,我试图创建一个简单的ruby脚本,通过bash运行ffmpeg命令,将音频文件从一种格式转换为另一种格式 命令是ffmpeg-i(在文件中)-acodec libmp3lame-ab 64k-ar 48000-ac 1 AAA_S00E00_Podcast.mp3 我有一个具有正确权限的ruby脚本,我可以调用它(在尝试使用ffmpeg之前,使用system ls调用对其进行了测试) 但是当我从bash调用它,试图将名为session.flac的文件转换为smoochie.mp3时,我得到了回报
ffmpeg-i(在文件中)-acodec libmp3lame-ab 64k-ar 48000-ac 1 AAA_S00E00_Podcast.mp3
我有一个具有正确权限的ruby脚本,我可以调用它(在尝试使用ffmpeg之前,使用system ls
调用对其进行了测试)
但是当我从bash调用它,试图将名为session.flac
的文件转换为smoochie.mp3
时,我得到了回报:
mkmp3.rb ('session.flac', 'smoochie')
bash: syntax error near unexpected token `'session.flac','
错误是因为您使用括号
(
和)
向ruby脚本添加参数。删除它们,错误就会消失;事实上,您只需要指定由空格分隔的字符串(否则您将得到逗号,
作为字符串):
现在,要使用这些参数,需要在脚本中添加ARGV
,如下所示:
#!/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 将包含一个字符串数组,其中添加了参数,考虑此脚本(<代码> Test.Rb< /代码>): 执行脚本:
$ ruby so.rb one two
输出将是:
["one", "two"]
因此,您可以使用数组索引访问每个值(即
ARGV[0]
和ARGV[1]
)。错误是因为您使用括号(
和)
向ruby脚本添加参数。删除它们,错误就会消失;事实上,您只需要指定由空格分隔的字符串(否则您将得到逗号,
作为字符串):
现在,要使用这些参数,需要在脚本中添加ARGV
,如下所示:
#!/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 将包含一个字符串数组,其中添加了参数,考虑此脚本(<代码> Test.Rb< /代码>): 执行脚本:
$ ruby so.rb one two
输出将是:
["one", "two"]
因此,您可以使用数组索引访问每个值(即
ARGV[0]
和ARGV[1]
)。您不能通过调用带有括号值的脚本来将参数传递给脚本中的函数。您需要使用ARGV
来处理脚本的参数
我的系统上没有ffmpeg
,因此我将演示如何使用命令行参数和一个简单的函数来报告其参数:
def my_function(first = "default1", second = "default2")
puts "Args were #{first} and #{second}"
end
if __FILE__ == $PROGRAM_NAME # only invoke with ARGV if script is invoked directly
my_function(*ARGV) # flatten the ARGV array and pass all values
end
Desktop$ ruby argv_demo.rb
Args were default1 and default2
Desktop$ ruby argv_demo.rb one
Args were one and default2
Desktop$ ruby argv_demo.rb one two
Args were one and two
Desktop$ ruby argv_demo.rb one two three
argv_demo.rb:1:in `my_function': wrong number of arguments (given 3, expected 0..2) (ArgumentError)
from argv_demo.rb:6:in `<main>'
使用不同数量的参数调用时生成以下内容:
def my_function(first = "default1", second = "default2")
puts "Args were #{first} and #{second}"
end
if __FILE__ == $PROGRAM_NAME # only invoke with ARGV if script is invoked directly
my_function(*ARGV) # flatten the ARGV array and pass all values
end
Desktop$ ruby argv_demo.rb
Args were default1 and default2
Desktop$ ruby argv_demo.rb one
Args were one and default2
Desktop$ ruby argv_demo.rb one two
Args were one and two
Desktop$ ruby argv_demo.rb one two three
argv_demo.rb:1:in `my_function': wrong number of arguments (given 3, expected 0..2) (ArgumentError)
from argv_demo.rb:6:in `<main>'
Desktop$ruby argv_demo.rb
Args是default1和default2
桌面$ruby argv_demo.rb one
Args为1和default2
桌面$ruby argv_demo.rb一两
Args是1和2
桌面$ruby argv_demo.rb一二三
argv_demo.rb:1:在“my_函数”中:参数数目错误(给定3,应为0..2)(ArgumentError)
来自argv_demo.rb:6:in`'
不能通过调用带有括号值的脚本来将参数传递给脚本中的函数。您需要使用ARGV
来处理脚本的参数
我的系统上没有ffmpeg
,因此我将演示如何使用命令行参数和一个简单的函数来报告其参数:
def my_function(first = "default1", second = "default2")
puts "Args were #{first} and #{second}"
end
if __FILE__ == $PROGRAM_NAME # only invoke with ARGV if script is invoked directly
my_function(*ARGV) # flatten the ARGV array and pass all values
end
Desktop$ ruby argv_demo.rb
Args were default1 and default2
Desktop$ ruby argv_demo.rb one
Args were one and default2
Desktop$ ruby argv_demo.rb one two
Args were one and two
Desktop$ ruby argv_demo.rb one two three
argv_demo.rb:1:in `my_function': wrong number of arguments (given 3, expected 0..2) (ArgumentError)
from argv_demo.rb:6:in `<main>'
使用不同数量的参数调用时生成以下内容:
def my_function(first = "default1", second = "default2")
puts "Args were #{first} and #{second}"
end
if __FILE__ == $PROGRAM_NAME # only invoke with ARGV if script is invoked directly
my_function(*ARGV) # flatten the ARGV array and pass all values
end
Desktop$ ruby argv_demo.rb
Args were default1 and default2
Desktop$ ruby argv_demo.rb one
Args were one and default2
Desktop$ ruby argv_demo.rb one two
Args were one and two
Desktop$ ruby argv_demo.rb one two three
argv_demo.rb:1:in `my_function': wrong number of arguments (given 3, expected 0..2) (ArgumentError)
from argv_demo.rb:6:in `<main>'
Desktop$ruby argv_demo.rb
Args是default1和default2
桌面$ruby argv_demo.rb one
Args为1和default2
桌面$ruby argv_demo.rb一两
Args是1和2
桌面$ruby argv_demo.rb一二三
argv_demo.rb:1:在“my_函数”中:参数数目错误(给定3,应为0..2)(ArgumentError)
来自argv_demo.rb:6:in`'