YouTube API、视频尺寸或纵横比
YouTube API, Video Dimensions or Aspect Ratio
这里有很多词,所以 TL;DR 我正在寻找一种方法来获取任何给定 YouTube 视频的宽度和高度。
我在使用 YouTube API 从 YouTube 视频中获取正确的视频 resolutions/aspect 比率时绝对是最困难的。长话短说,我需要一个用户选择的视频来完全填充一个容器,用作背景视频,内容将悬停在上面——为了做到这一点,同时丢失最少的视频溢出,我需要知道视频的尺寸,以便我可以将其放大并正确居中。
我摆弄过OAuth Playground plenty (you'll probably also want to look at the YouTube Data API), but can't seem to find anything that actually gives me the dimensions of the video. The closest thing that I can find which is correct for SOME videos, but not all, is doing a GET with https://www.googleapis.com/youtube/v3/videos?part=player&id=XJK_uVyTBHc。 part=player 吐出很多我不想要的东西,还有一些 HTML 用于嵌入的 iframe 视频。 iframe 标记包含宽度和高度,但这些并不总是正确的。
例如,ID 为 XJK_uVyTBHc returns 的视频是一个宽度为“480”和高度为“360”的 iframe,但在 YouTube 上观看时,它显示我的视口为 406x722 像素,当前和最佳分辨率为 608x1080px,宽高比大致相同(右键单击视频,选择 stats for nerds 可以看到)。我没有做过非常广泛的测试,但可以确认至少分辨率为 1920x1080px 的视频确实会返回一个 iframe 标签,该标签在宽度和高度属性上具有正确的纵横比。我认为这是由于 YouTube 控件适合视频所需的最小高度和宽度,但这对我的问题没有帮助! :[
所以,我们已经到了 2019 年了......是否仍然没有通过简单的 API 调用来获取 YouTube 视频的宽度和高度的好方法?我是否忽略了某处特定的 API 操作?任何帮助或额外资源将不胜感激!
如果您看到您提供的视频的源代码 - videoId
: XJK_uVyTBHc
:
view-source:https://www.youtube.com/watch?v=XJK_uVyTBHc
您会看到这样一段代码:
fmt_list":"22\/406x720...
我认为这就是您要寻找的价值。正如您提到的,API returns iframe 和这些值与 YouTube 网站上显示的不同。
进行更多测试,我只是随机选择了几个视频并获取了 iframe "with the settings offered by YouTube" - "by clicking the (share) button > insert video":
Microsoft Windows Mixed Reality update | October 2018:
<iframe width="560" height="315" src="https://www.youtube.com/embed/00vnln25HBg" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Test Video Please Ignore - this is your sample video:
<iframe width="560" height="315" src="https://www.youtube.com/embed/XJK_uVyTBHc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
The Rasmus - In the Shadows [Crow Version] (Official Video):
<iframe width="560" height="315" src="https://www.youtube.com/embed/7gwO8-oqwFw" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Super Street Fighter IV Hakan Trailer:
<iframe width="560" height="315" src="https://www.youtube.com/embed/m6uxFzaB4sE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
lofi hip hop radio - beats to relax/study to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/hHW1oY26kxQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
所有以前的 iframe 具有相同的宽度和高度:width="560"
和 height="315"
.
我使用 Google API Explorer 创建了一个 demo 和上面 iframe 中的 videoIds
来检索这些结果:
{
"items": [
{
"snippet": {
"title": "Microsoft Windows Mixed Reality update | October 2018"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/00vnln25HBg\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "Test Video Please Ignore"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/XJK_uVyTBHc\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "The Rasmus - In the Shadows [Crow Version] (Official Video)"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/7gwO8-oqwFw\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "Super Street Fighter IV Hakan Trailer"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/m6uxFzaB4sE\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "lofi hip hop radio - beats to relax/study to"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/hHW1oY26kxQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
}
]
}
YouTube 数据 API 的所有先前结果都会改变其 width
和 height
值,如下所示:
width="480"
和 height="270"
.
width="480"
和 height="360"
.
在我为您准备的 "try-it" 演示中可以得到相同的结果。
我还注意到“embedWidth
”和“embedHeight
”没有返回 - 也许这些字段已弃用,但我找不到任何相关文档。
不幸的是,YouTube 数据 API 某种程度上 对某些任务有限制 - 就像这个一样。
编辑 (26/10/2019): 感谢 :
embedWidth
or embedHeight
is returned if you specify maxWidth
or
maxHeight
as a query parameter to the videos:list
request.
这里有很多词,所以 TL;DR 我正在寻找一种方法来获取任何给定 YouTube 视频的宽度和高度。
我在使用 YouTube API 从 YouTube 视频中获取正确的视频 resolutions/aspect 比率时绝对是最困难的。长话短说,我需要一个用户选择的视频来完全填充一个容器,用作背景视频,内容将悬停在上面——为了做到这一点,同时丢失最少的视频溢出,我需要知道视频的尺寸,以便我可以将其放大并正确居中。
我摆弄过OAuth Playground plenty (you'll probably also want to look at the YouTube Data API), but can't seem to find anything that actually gives me the dimensions of the video. The closest thing that I can find which is correct for SOME videos, but not all, is doing a GET with https://www.googleapis.com/youtube/v3/videos?part=player&id=XJK_uVyTBHc。 part=player 吐出很多我不想要的东西,还有一些 HTML 用于嵌入的 iframe 视频。 iframe 标记包含宽度和高度,但这些并不总是正确的。
例如,ID 为 XJK_uVyTBHc returns 的视频是一个宽度为“480”和高度为“360”的 iframe,但在 YouTube 上观看时,它显示我的视口为 406x722 像素,当前和最佳分辨率为 608x1080px,宽高比大致相同(右键单击视频,选择 stats for nerds 可以看到)。我没有做过非常广泛的测试,但可以确认至少分辨率为 1920x1080px 的视频确实会返回一个 iframe 标签,该标签在宽度和高度属性上具有正确的纵横比。我认为这是由于 YouTube 控件适合视频所需的最小高度和宽度,但这对我的问题没有帮助! :[
所以,我们已经到了 2019 年了......是否仍然没有通过简单的 API 调用来获取 YouTube 视频的宽度和高度的好方法?我是否忽略了某处特定的 API 操作?任何帮助或额外资源将不胜感激!
如果您看到您提供的视频的源代码 - videoId
: XJK_uVyTBHc
:
view-source:https://www.youtube.com/watch?v=XJK_uVyTBHc
您会看到这样一段代码:
fmt_list":"22\/406x720...
我认为这就是您要寻找的价值。正如您提到的,API returns iframe 和这些值与 YouTube 网站上显示的不同。
进行更多测试,我只是随机选择了几个视频并获取了 iframe "with the settings offered by YouTube" - "by clicking the (share) button > insert video":
Microsoft Windows Mixed Reality update | October 2018:
<iframe width="560" height="315" src="https://www.youtube.com/embed/00vnln25HBg" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Test Video Please Ignore - this is your sample video:
<iframe width="560" height="315" src="https://www.youtube.com/embed/XJK_uVyTBHc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
The Rasmus - In the Shadows [Crow Version] (Official Video):
<iframe width="560" height="315" src="https://www.youtube.com/embed/7gwO8-oqwFw" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Super Street Fighter IV Hakan Trailer:
<iframe width="560" height="315" src="https://www.youtube.com/embed/m6uxFzaB4sE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
lofi hip hop radio - beats to relax/study to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/hHW1oY26kxQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
所有以前的 iframe 具有相同的宽度和高度:width="560"
和 height="315"
.
我使用 Google API Explorer 创建了一个 demo 和上面 iframe 中的 videoIds
来检索这些结果:
{
"items": [
{
"snippet": {
"title": "Microsoft Windows Mixed Reality update | October 2018"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/00vnln25HBg\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "Test Video Please Ignore"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/XJK_uVyTBHc\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "The Rasmus - In the Shadows [Crow Version] (Official Video)"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/7gwO8-oqwFw\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "Super Street Fighter IV Hakan Trailer"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/m6uxFzaB4sE\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
},
{
"snippet": {
"title": "lofi hip hop radio - beats to relax/study to"
},
"contentDetails": {
"dimension": "2d"
},
"player": {
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/hHW1oY26kxQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}
}
]
}
YouTube 数据 API 的所有先前结果都会改变其 width
和 height
值,如下所示:
width="480"
和height="270"
.width="480"
和height="360"
.
在我为您准备的 "try-it" 演示中可以得到相同的结果。
我还注意到“embedWidth
”和“embedHeight
”没有返回 - 也许这些字段已弃用,但我找不到任何相关文档。
不幸的是,YouTube 数据 API 某种程度上 对某些任务有限制 - 就像这个一样。
编辑 (26/10/2019): 感谢
embedWidth
orembedHeight
is returned if you specifymaxWidth
ormaxHeight
as a query parameter to thevideos:list
request.