I implemented a new feature over at my Jenny McCarthy and Hayden Panettiere websites this past week.
I've been posting YouTube videos on both sites for quite some time, but have only been providing text descriptions to go along with the links from the main video pages. I finally decided that I wanted to figure out how to dynamically include thumbnail image previews of the clips, not only to display on the main video pages, but also in web feeds where applicable.
First I got all fancy and was sending in YouTube API requests to get at the information. The youtube.videos.get_details function provides thumbnail information in its <thumbnail_url> node.
However, once I had implemented a preliminary approach using that method, I quickly realized there was a very obvious pattern to the thumbnail URLs being returned with each call:
http://img.youtube.com/vi/LiIboq6XCOg/2.jpg
The only portion of the above URL that seems to change is the video ID between the fourth and fifth forward slashes.
So rather than calling the API multiple times to get thumbnails for all the videos presented on the page, I decided to keep all function calls local, opting to create my own internal function for extracting the YouTube video ID from my XHTML-compliant embed code using a regular expression and then inserting that between the http://img.youtube.com/vi/ and /2.jpg portions of the static thumbnail URL.
The result is much faster and works like a charm.
Comments
hello,
I find this post to be very interesting. I am currently using a "related videos" based off a wordpress "related posts" plugin, but that doesn't help me get thumbnails for the related videos. Any suggestions on how I can combine your tips to implement my solution?
Josh, as long as you can somehow extract the video ID from the related videos, you should be able to employ this trick at your site. I'm not sure how much direct access you have to the plugin, though.
I'm pretty sure this trick didn't work a few months ago. I had to use the API because the thumbnails were located on various differently named servers and you couldn't predict the URL. Seems they have simplified things by using a generic URL for their thumbnails, just like they do for videos.
Thanks for the history, Leon. I'm hoping they stick with this new generic URL because hitting the API every time you need to fetch a video's thumbnail feels like overkill to me.
Thanks for writing that URL. I wanted to have a collection of videos on my personal homepage and thought about creating my own thumbnails to link to each video! This URL has just saved me a lot of time.
Perfect, just was I was looking for. Thanks for the legwork and posting the results.
Wow you are awesome, two of my issues I found the answers to on your website. BTW I agree with so many others that your design/colors are really the best I've seen. Even your font choice goes great with the site.
Keep up the good work my friend.
Thanks for the kind words, Ketchman, and thanks for dropping by.
hi guys.
i would really appreciate if you can tell me how to make a out a thumbnail of a youtube video...if possible please give me the full code.i need it...
report... 8 feb 2008, the url works.. :)
yes, using youtube's API kinda overkill if just want to get a thumbnail of one or two video ...
thanks, Sir... :)
Saved me tons of time! Thanks. Hopefully YouTube doesn't change this anytime soon.
Is it okay to link directly to youtube's thumbnails images? Is it allowed?
I've been working for RPTV for some time and its been difficult creating thumbnails for each new video that comes in. Each video is hosted in youtube. We have about 70 videos... so thats a lot of thumbnails.
I wonder if there's a way to make our site automatically load latest videos and thumbnails on to our page. Hopefully youtube / google is okay with that?
Work Site
http://www.regentpark.tv
My Site
http://teabone.awardspace.com
this is exactly what i was looking for
fuck the api
thanks
its not working anymore =(
brilliant :) thanks
great post. thanks.
Man, I love you! Thanks a ton :)
Nice ! Very light compared with the YouTube API.
I hope this will work in the future (since it is undocumented).
By security, when embeeding the video into my (home-made) CMS, the thumbnail is copied to my server.
Good catch.
They're now using http://i1.ytimg.com/vi/LiIboq6XCOg/2.jpg and http://i1.ytimg.com/vi/LiIboq6XCOg/default.jpg on the site itself, but your solution still works.
Great post and really great solution ;)
Thanks mate, your my saviour!
http://img.youtube.com/vi/VIDEO-ID/0.jpg
will get u a 320x240 thumbanil
Thanks!
At last my prayers answered so simple too bernie soooo simple indeed. all that messing around with ffmpeg and php ffmpeg scripts to get thumbnails and long php cron jobs, what a load of wasted time. can just link to youtube and get thumbnail like so. dare i sa again...SIMPLE!
Thanx bernie
Nice, but what is the Regex are you using for this solution?
Thanks for the article! I have spread the word on my blog too :)
Am I missing something or does the newer gdata api not support getting thumbnail image urls? This is a big pain. Also it suggests that the simple fixes above will be broken regularly, if google in fact doesn't want people getting thumbnails.
thanks man, you saved my time :)
Thanks for sharing! This saved me loads of time! I was about to go the API route too.
It's good to know an easy way to get youtube video thumbnails. It can be helpful for my new php scripts
Thanks!
Please can anyone provide me the Source code to create
Dynamic YouTube Image Thumbnails
thank you very much!it saves my time a lot
better quality for all videos using: http://img.youtube.com/vi/VIDEO_ID/hqdefault.jpg
love you man!!!!
What is it,
http://i1.ytimg.com/vi/VIDEO_ID/hq1.jpg
http://i1.ytimg.com/vi/VIDEO_ID/hq2.jpg
http://i1.ytimg.com/vi/VIDEO_ID/hq3.jpg
Also work too :)
Very helpful post - thank you. You just saved me days worth of work. Cheers.
Bernie.
You've just save me from writing my own thumbnail extractor. Thank you!
How can i had a Youtube video thumbnail to a excerpts list using this technique ? Could someone help me with this?
best
Thank you - saved me heaps of time!
Thanks!
http://i3.ytimg.com/vi/video_id/default.jpg - Small
http://i3.ytimg.com/vi/video_id/0.jpg - Large
http://i3.ytimg.com/vi/video_id/1.jpg - Cap 1
http://i3.ytimg.com/vi/video_id/2.jpg - Cap 2
http://i3.ytimg.com/vi/video_id/3.jpg - Cap 3
thx for ur post
I m trying to insert youtube thumbnails into vdo theme
I hope i can get it done
I was wondering... if your
Oops!
I meant your " < embed > " tag.. the website turned it into an actual embedded element! :D
veplcsk, the regular expression wasn't used against the actual embed tag and its contents. It was used against a URL to extract the ID portion.
Thats was a great trick. Saved lot of development time for me. Thanx.
Heya,
Thanks for this tip. Do you know if there's any difference between using the
http://img.youtube.com/vi/jbSsoqdefOA/2.jpg
or the
http://i2.ytimg.com/vi/jbSsoqdefOA/2.jpg
method?
Any signs that one would be more reliable (less likely to stop working) than another?
Thanks again!
Gary
That was one life-saver dude! It works
top marks many thanks saved me some serious leg work!
Works great - many thanks!
good tip, saved me some time!
I've just been doing a lot of research on the YouTube API the past couple of days and this is what I've come up with so far:
@gary, (based on a lot of assumption) I think the img.youtube.com serves more as a kind of CDN, so that address will fetch an image according the best source. If you notice you can access the same thumbnail from the following different URLs
http://i1.ytimg.com/vi/jbSsoqdefOA/2.jpg
http://i2.ytimg.com/vi/jbSsoqdefOA/2.jpg
http://i3.ytimg.com/vi/jbSsoqdefOA/2.jpg
http://i4.ytimg.com/vi/jbSsoqdefOA/2.jpg
In addition to the above post you can now get a hq image:
http://img.youtube.com/vi/jbSsoqdefOA/hqdefault.jpg
Ross
this is great, simple and pretty straight forward.
thanks for sharing.
Thanks dude. You ROCK
amazing, it still works after ~3 years :D
Dude you rock....it works...thanks.
I have no ward to say you . You have done a miracle.Great work....
Thanks for your tip. It's help me a lot.
Does anybody know how to have on the thumbnail the big "Play" button and the control bar on the bottom (including time of the video)?
Thanks bro for this info. Is very helpfull to have people like you around us.
I suck at regular expression can you paste in a sample code please
Im trying to design a youtube gallery for wordpress and this is very useful news.
Thanks for share this info.
I'm never giving comments/feedback to great scripts and tricks but this is brilliant.
I need a code to get youtube thumbnail as by any keywords I search. Could please advice me about the code?
Thanks dear. i was searching like this easy tricks. Thanks tons :)
The thumbnails works great !!! thnx .. help me save some time....
Hi, I have an issue.
If I use the youtube source code
http://img.youtube.com/vi/%VIDEO_ID%/0.jpg
in my source code for an youtube player which I am trying to develop, I get a Thumbnail error:2036
But If I use any other like
http://i1.ytimg.com/vi/LiIboq6XCOg/2.jpg
or any other source code, My player works perfectly.
Any help will be appreciated.
I am stuck on this since ages!!
It doesn't matter that the content is outdated... i found what i was looking for! Thanks!
Post Comments
If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.
Thanks a ton. You saved lot of my time :)
Permalink