简体   繁体   English

使用ffmpeg,ffprobe或rvideo在ruby脚本中获取视频元数据

[英]Getting video metadata in ruby script using ffmpeg, ffprobe or rvideo

I want to get metadata of videos referenced by a URL using Ruby. 我想使用Ruby获取URL引用的视频的元数据。 At this point, I found many related posts, but could not find out how to solve my problem. 此时,我发现了许多相关帖子,但无法找到如何解决我的问题。

I tried to use RVideo, but when I do: 我尝试使用RVideo,但是当我这样做时:

file = RVideo::Inspector.new(:file => 'http://www.agreatsite.com/avideo.mp4') file = RVideo :: Inspector.new(:file =>'http://www.agreatsite.com/avideo.mp4')

It throws 它抛出

'ArgumentError: File not found (http://www.agreatsite.com/avideo.mp4)... 'ArgumentError:找不到文件(http://www.agreatsite.com/avideo.mp4)...

So I can't get the information using RVideo (but it works well when I have the file hosted on my local computer). 所以我无法使用RVideo获取信息(但是当我在本地计算机上托管文件时,它运行良好)。

I then tried to use ffprobe, but I don't know how to read the output. 然后我尝试使用ffprobe,但我不知道如何读取输出。 So far, I have the following method, which "shows" the information I want when I run it in the console, but it actually returns "true" and I can't find out how to capture the output I need... 到目前为止,我有以下方法,当我在控制台中运行它时“显示”我想要的信息,但它实际上返回“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 

Would love some help, to make this work with either ffprobe or RVideo! 希望得到一些帮助,使用ffprobe或RVideo来完成这项工作!

UPDATE: I found a way to get what I needed. 更新:我找到了一种方法来获得我需要的东西。 Not sure this is the best way to do it: 不确定这是最好的方法:

def get_media_duration 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 结束

I guess I could make it work that way, but doesn't seem very elegant to me. 我想我可以让它以这种方式工作,但对我来说似乎并不优雅。 Better suggestions would be greatly appreciated! 更好的建议将不胜感激!

Check streamio-ffmpeg, If you are still looking for a solution. 检查streamio-ffmpeg,如果您仍在寻找解决方案。 Also ffprobe supports json format now: ffprobe -print_format json -show_streams #{input_file} ffprobe现在也支持json格式: ffprobe -print_format json -show_streams #{input_file}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM