Abid Iqbal
Abid Iqbal

Reputation: 773

how to show thumbnails for vimeo and youtube embedded links in ruby on rails?

I have embedded the youtube and vimeo links in my site and trying to show the thumbnail of the video as a link to play while on click.

i tried the gem "has_vimeo_video" but it only accepts the vimeo videos.So i want to show the thumbnails of both the videos i.e youtube and vimeo.

Please need solution.

Thanks

Upvotes: 3

Views: 3137

Answers (4)

user3723326
user3723326

Reputation: 115

Add a new file 'thumbnail_helper.rb' in '/app/helper'. Copy and paste the following code in that file:

module ThumbnailHelper

  # Regex to find YouTube's and Vimeo's video ID
  YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11}))
  VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$)

  # Finds YouTube's video ID from given URL or [nil] if URL is invalid
  # The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\
  def find_youtube_id url
    url = sanitize url
    matches = YOUTUBE_REGEX.match url.to_str
    if matches
      matches[6] || matches[5]
    end
  end

  # Finds youtube video thumbnail
  def get_youtube_thumbnail url
    youtube_id = find_youtube_id url
    result = "http://img.youtube.com/vi/#{youtube_id}/0.jpg"
  end

  # Finds Vimeo's video ID from given URL or [nil] if URL is invalid
  def find_vimeo_id url
    url = sanitize url
    matches = VIMEO_REGEX.match url.to_str
    matches[2] if matches
  end

  # Finds vimeo video thumbnail
  def get_vimeo_thumbnail url
    vimeo_id = find_vimeo_id url
    result = URI.open("http://vimeo.com/api/v2/video/#{vimeo_id}.json").read
    begin
      JSON.parse(result).first['thumbnail_large']
    rescue StandardError
      nil
    end
  end

  # Main function
  # Return a video thumbnail
  # If the url provided is not a valid YouTube or Vimeo url it returns [nil]
  def get_video_thumbnail(url)
    if find_vimeo_id(url)
        get_vimeo_thumbnail(url)
    elsif find_youtube_id(url)
        get_youtube_thumbnail(url)
    end
  end
end

Now call the get_video_thumbnail('video_url') from your view. It will return you the thumbnail of the given video.

Upvotes: 1

shizaa
shizaa

Reputation: 5

You don't need any gems for such a simple task

url = 'https://vimeo.com/126100721'
id = url.partition('vimeo.com/').last
result = URI.open("http://vimeo.com/api/v2/video/#{id}.json").read

begin
  JSON.parse(result).first['thumbnail_large']
rescue StandardError
  nil
end

Upvotes: 0

scientiffic
scientiffic

Reputation: 9415

the video_info gem is great for fetching thumbnails:

https://github.com/thibaudgg/video_info

Upvotes: 4

Deej
Deej

Reputation: 5352

I found out yesterday that there was carrierwave-video-thumbnailer which is pretty cool. It basically does what you want

A thumbnailer plugin for Carrierwave. It mixes into your uploader setup and makes easy thumbnailing of your uploaded videos. This software is quite an alpha right now so any kind of OpenSource collaboration is welcome.

But you will need to of course include carrierwave into your application. Hopefully this helps

Carreierwave is an uploader that enables users to upload content in their app, which I think

Update

Alternatively you can use embedly which allows you to add embedded media into your application. The API provides a lot response options as can be seen here. In doing so you could do something like:

$.embedly('http://www.youtube.com/watch?v=_FE194VN6c4',
           {maxWidth: 600,
               elems: $('#element'),
             success: function(oembed, dict){
                        alert(oembed.title);
                      });

The elems property is where you want the video to be embedded, so that when the link is pasted in it should embed the video. You need to pass the video url and get it using the jQuery selector from wherever you put your url in your html.

Your other alternative is to have a look at the following auto_html gem

Upvotes: 2

Related Questions