If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I made a function to only fetch existing images from YouTube
If you're using the public api, the best way to do it is using if statements.
If the video is public or unlisted, you set the thumbnail using the url method. If the video is private you use the api to get the thumbnail.
Save file as
.js
Each YouTube video has 4 generated images. They are predictably formatted as follows:
The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of
1.jpg
,2.jpg
,3.jpg
) is:For the high quality version of the thumbnail use a url similar to this:
There is also a medium quality version of the thumbnail, using a url similar to the HQ:
For the standard definition version of the thumbnail, use a url similar to this:
For the maximum resolution version of the thumbnail use a url similar to this:
All of the above urls are available over http too. Additionally, the slightly shorter hostname
i3.ytimg.com
works in place ofimg.youtube.com
in the example urls above.Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.
If you want to get rid of the "black bars" and do it like YouTube does it, you can use :
And if you can't use
.webp
extension you can do it like this :Also, if you need the unscaled version use
maxresdefault
instead ofmqdefault
.Note: I'm not sure about the aspect ratio if you're planning to use
maxresdefault
.You can get the Video Entry which contains the URL to the video's thumbnail. There's example code in the link. Or, if you want to parse XML, there's information here. The XML returned has a
media:thumbnail
element, which contains the thumbnail's URL.