Saad
Saad

Reputation: 51

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 please guide me how can this be achieved??

Upvotes: 0

Views: 2107

Answers (5)

mohamagdy
mohamagdy

Reputation: 2093

You can use an external service like embedl.ly

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

Upvotes: 0

sawa
sawa

Reputation: 168239

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.

Upvotes: 0

hb922
hb922

Reputation: 999

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!

Upvotes: 1

Dex
Dex

Reputation: 12759

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

Upvotes: 0

egze
egze

Reputation: 3236

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

Upvotes: 0

Related Questions