Scrape image from YouTube video page

Go To StackoverFlow.com

2

When I copy a YouTube link, like this one, for example (http://www.youtube.com/watch?v=_Ka2kpzTAL8), and paste it into a Facebook status update, Facebook somehow grabs a still of the video. I'd like to build a feature that does the same thing, but I don't really know how to find the image.

Can anyone point me in the right direction? I'm open to PHP, Python, Ruby or Perl.

2012-04-04 18:27
by Jason Swett
I got a little farther. I thought I might look at the URL of the thumbnail that appears in Facebook. That same URL appears in the markup of the YouTube page as <meta property="og:image" content="http://i4.ytimg.com/vi/_Ka2kpzTAL8/hqdefault.jpg">. Now I just have to figure out how to get a hold of that - Jason Swett 2012-04-04 18:34
Hmm... I can't help you that much, but if you go to Youtube in Chrome, find a video, and then click on your Settings -> Tools -> Developer Tools -> Resources, you can see that each video has a list of frames. Most of them reside, as you pointed out, on ytimg.com - elliottbolzan 2012-04-04 18:35


3

Alright, found it !

Take this link: http://i4.ytimg.com/vi/something/hqdefault.jpg and replace something with the video's ID on Youtube. For a smaller image, just remove the hq part of the URL.

2012-04-04 18:40
by elliottbolzan


0

I can also confirm this implementation is the way two go. I have used it a Ruby screen scraper ( search application ) @ http://stripsearch.me

            # search10b Youtube crawl9
            @search10a1 = search_array[1]
            if search_array[1].present?
                @results_9b = []
                params_9b = search_array[1].split.map(&:downcase).join('+')
                search_url_yt_9b = url_9b << params_9b

                doc_9b = Nokogiri::HTML(open(search_url_yt_9b).read, nil, 'utf-8')

                entries_9b = doc_9b.css('.yt-ui-ellipsis-2.spf-link')

                if entries_9b.size != 0

                    entries_9b.each do |entry_9b|
                        title = entry_9b['title']
                        li = "https://www.youtube.com"
                        nk = entry_9b['href']
                        link = li << nk
                        img_src = entry_9b['href']
                        uid = img_src.split('=').last
                        precede_url = "http://i4.ytimg.com/vi/"
                        append_url = "/hqdefault.jpg"
                        img_url = precede_url << uid << append_url
                        precede_tag = "<img src="
                        append_tag = ">"
                        img = precede_tag << img_url << append_tag
                        @results_9b.push(title, link, img)
                    end
                else
                    @error_9b = "no results found here for " + params_9b
                end
            end
2017-01-18 16:17
by CookieMonstROR-JS
Ads