本文介绍了如何从YouTube iframe获取YouTube缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我有一篇博文,其中包含以下 iframe 。
For instance I have a blog post that has the following iframe.
<iframe width="420" height="315" src="//www.youtube/embed/1sIWez9HAbA" frameborder="0" allowfullscreen></iframe>如何从此 iframe ?
推荐答案YouTube缩略图
可以在此标准路径中找到YouTube缩略图:
YouTube thumbnails can be found in this standard path:
img.youtube/vi/[video-id]/[thumbnail-number].jpg
- [video-id] 是YouTube视频ID,例如 1sIWez9HAbA 。
- [thumbnail-number] 是缩略图的编号每个视频通常有4个,例如 0 。
- [video-id] is the YouTube video ID, e.g. 1sIWez9HAbA.
- [thumbnail-number] is the number of the thumbnail of the 4 each video usually has, e.g. 0.
从iframe获取缩略图
所以,基于 iframe 的 src 属性你可以直接构造缩略图的URL。
So, based on the iframe's src attribute you may construct the URL of the thumbnail directly.
例如,使用jQuery:
For example, using jQuery:
var iframe = $('iframe:first'); var iframe_src = iframe.attr('src'); var youtube_video_id = iframe_src.match(/youtube\.*(\?v=|\/embed\/)(.{11})/).pop(); if (youtube_video_id.length == 11) { var video_thumbnail = $('<img src="//img.youtube/vi/'+youtube_video_id+'/0.jpg">'); $(body).append(video_thumbnail); }注意此示例检查 iframe 获取有效的YouTube视频ID,并假设它长度为11个字符,这是事实上的标准。
Note this example checks the URL of the iframe for a valid YouTube video ID and assumes it to be 11 characters long, which is the de facto standard.
更多推荐
如何从YouTube iframe获取YouTube缩略图?
发布评论