YouTube 缩略图链接并非在所有情况下都有效 [英] YouTube thumbnail link doesn't work in all cases
问题描述
我正在开发的应用程序需要获取给定 URL 的 HQ YouTube 缩略图.使用这个答案,我尝试使用 maxresdefault.jpg
来获得缩略图的最大分辨率但在某些视频(看似随机)上,请求 404.
An application I'm working on needs to get HQ YouTube thumbnails given a URL. Using this answer, I tried using the maxresdefault.jpg
to get the maximum resolution of the thumbnail but on some videos (seemingly random), the request 404's.
以下是一些示例:
- http://img.youtube.com/vi/zIez5QVE3NI/maxresdefault.jpg 作品
- http://img.youtube.com/vi/DaAfe5xTm8w/maxresdefault.jpg 不起作用
- http://img.youtube.com/vi/zIez5QVE3NI/maxresdefault.jpg works
- http://img.youtube.com/vi/DaAfe5xTm8w/maxresdefault.jpg doesn't work
使用相同的 URL,使用相同视频键的其他请求确实有效:
Using the same URL's, other requests using the same video keys do work:
- http://img.youtube.com/vi/zIez5QVE3NI/default.jpg 作品
- http://img.youtube.com/vi/DaAfe5xTm8w/default.jpg 作品
- http://img.youtube.com/vi/zIez5QVE3NI/default.jpg works
- http://img.youtube.com/vi/DaAfe5xTm8w/default.jpg works
为什么 maxresdefault.jpg
不能可靠地返回图像?这两个视频都是公开的,可以嵌入.
Why doesn't maxresdefault.jpg
reliably return the image? Both of those videos are public and can be embedded.
我会使用 0.jpg
但它在图像的顶部和底部有黑条.
I'd use 0.jpg
but it has black bars on the top and bottom of the image.
推荐答案
据我所知,YouTube 只为高分辨率视频生成高分辨率剧照.您链接的第一个上传分辨率至少为 1080p,而第二个不到 720p.
As I understand it, YouTube only generates high-res stills for high-res videos. The first one you link was uploaded at a resolution of at least 1080p, while the second was less than 720p.
看起来你唯一能做的就是拉取视频源,检查是否仍然生成了高分辨率,如果没有,则自己放大较小的.
Looks like the only thing you can do is pull the video feed, check that to see if a high-res still has been generated and up-scale the smaller one yourself if not.
所有¹ YouTube 静止图像均以 4:3 的纵横比呈现,因此如果视频为 16:9,则顶部和底部总会出现黑条.在 HTML/CSS 中,可以通过将图像设置为 div 的背景并将 div 的大小调整为正确的方面来轻松解决这一问题.
All¹ YouTube stills are presented in the 4:3 aspect ratio, so will always have black bars at the top and bottom if the video is 16:9. In HTML/CSS, this is easily combated by setting the image as the background of a div and sizing the div to the correct aspect.
¹ 除了 mqdefault.jpg 静止图像,它们都是 16:9 宽高比 (320x180)
¹ Except the mqdefault.jpg stills, which are all in 16:9 aspect (320x180)
这篇关于YouTube 缩略图链接并非在所有情况下都有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!