我想使用 Ruby 获取 URL 引用的视频的元数据。此时,我找到了很多相关帖子,但找不到如何解决我的问题。
我尝试使用 RVideo,但是当我这样做时:
文件 = RVideo::Inspector.new(:file => 'http://www.agreatsite.com/avideo.mp4')
它抛出
'ArgumentError:找不到文件(http://www.agreatsite.com/avideo.mp4)...
因此,我无法使用 RVideo 获取信息(但当我将文件托管在本地计算机上时,它效果很好)。
然后我尝试使用 ffprobe,但我不知道如何读取输出。
到目前为止,我有以下方法,当我在控制台中运行它时,它“显示”我想要的信息,但它实际上返回“true”,并且我不知道如何捕获我需要的输出......
def media_info
source = self
command = <<-end_command
ffprobe -v quiet -print_format json -show_format -show_streams #{source}
end_command
command.gsub!(/\s+/, " ")
system(command)
end
希望得到一些帮助,使之与 ffprobe 或 Video 一起工作!
更新:
我找到了一种方法来获得我需要的东西。不确定这是最好的方法:
def get_media_duration
source = self.media[0][:url]
command = <<-end_command
ffprobe -v quiet -show_streams #{source}
end_command
command.gsub!(/\s+/, " ")
duration = ""
IO.popen(command) { |io| while (line = io.gets) do
puts "++ "+line.inspect
duration = line.split("duration=")[1].gsub("\n", "") if line.split("duration=").length > 1
end
}
duration
end
我想我可以让它以这种方式工作,但对我来说似乎不太优雅。更好的建议将不胜感激!
如果您仍在寻找解决方案,请检查streamio-ffmpeg。 ffprobe 现在也支持 json 格式:ffprobe -print_format json -show_streams #{input_file}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)