开发者

How to check if a url contains video?

hi i am creating a script ruby on rails in which user shares a link and if the link contains a video, the embed code of the video is extracted. in other words, i am trying to implement a "facebook post link" like feature.. can someone pl开发者_JAVA技巧ease guide me how can this be achieved??


The only way I can think of to do this would be to manually check each post for a link to the video, for example:

  YOUTUBE_EMBED = '<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0" allowfullscreen></iframe>'    
  if comment =~ /.*http:\/\/(\w+\.)?youtube.com\/watch\?v=(\w+).*/
    return YOUTUBE_EMBED.gsub(/VIDEO_ID/, $2)  
  end

Then repeat this process for each video site. I am wrestling with a similar concept so if you figure out a better way to do it let me know!


You can analyze http headers for that link

require "net/http"

Net::HTTP.start('www.jhepple.com', 80) do |http|
 http.head('/support/SampleMovies/MPEG-1.mpg')
end['content-type']

Outputs "video/mpeg"

Now that you know it's really a video, do what you want with it


You could also use a utility like file and fork a system process so that it executes a command like file -b downloaded_file.mpg.

So your code would look something like this:

IO.popen("file -b /path/to/video.mpg") { |stdout| @stdout = stdout.gets }
if not @stdout.grep(/MPEG/).empty?
   puts "MPEG Detected"
end


Flash videos usually have the extension .flv, so you just need to look for files that have it.

html_souce.scan(%r|href\s*=\s*\"[^\"]+\.flv\"|)

If you need other file formats, just change the regexp.


You can use an external service like embedl.ly

Here you're a gem that may help you https://github.com/judofyr/ruby-oembed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜