javascript - 我们可以更改返回的 oembed YouTube iframe 宽度和高度吗?

标签 javascript youtube youtube-api

现在 YouTube 返回分钟。宽度和高度。如何调整GET调用改变返回html ?
例如:


"html": "<iframe width=\"400\" height=\"260\" src=\"https://www.youtube.com/embed/kFz9afj8lu0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"

当前的
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=kFz9afj8lu0
{
"title": "Can You Actually Game in 8K? (RTX 3090 Gameplay!)",
"author_name": "Marques Brownlee",
"author_url": "https://www.youtube.com/c/mkbhd",
"type": "video",
"height": 113,
"width": 200,
"version": "1.0",
"provider_name": "YouTube",
"provider_url": "https://www.youtube.com/",
"thumbnail_height": 360,
"thumbnail_width": 480,
"thumbnail_url": "https://i.ytimg.com/vi/kFz9afj8lu0/hqdefault.jpg",
"html": "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/kFz9afj8lu0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
}

最佳答案

看起来有一个 oEmbed 规范:https://oembed.com
如果你往下看

2.2. Consumer Request


你会注意到有 maxwidthmaxheight选项。
添加 &maxwidth=400&maxheight=260到您的 GET 请求,如下所示:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=kFz9afj8lu0&maxwidth=400&maxheight=260
这将返回以下内容:
{
  "title": "Can You Actually Game in 8K? (RTX 3090 Gameplay!)",
  "author_name": "Marques Brownlee",
  "author_url": "https://www.youtube.com/c/mkbhd",
  "type": "video",
  "height": 225,
  "width": 400,
  "version": "1.0",
  "provider_name": "YouTube",
  "provider_url": "https://www.youtube.com/",
  "thumbnail_height": 360,
  "thumbnail_width": 480,
  "thumbnail_url": "https://i.ytimg.com/vi/kFz9afj8lu0/hqdefault.jpg",
  "html": "\u003ciframe width=\u0022400\u0022 height=\u0022225\u0022 src=\u0022https://www.youtube.com/embed/kFz9afj8lu0?feature=oembed\u0022 frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"
}
html属性,在 decodeURIComponent 之后,现在看起来像你的例子:
<iframe width=\"400\" height=\"225\" src=\"https://www.youtube.com/embed/kFz9afj8lu0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>

关于javascript - 我们可以更改返回的 oembed YouTube iframe 宽度和高度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65418566/

相关文章:

javascript - 脚本不使用 jquery 打印

Youtube child API

php - file_get_contents()无法与YouTube一起使用

javascript - 重新访问同一页面时记住复选框值

javascript - 替代更新 parent 高度的绝对定位

android - 如何在 YouTube 中播放私有(private)视频?

YouTube liveStreaming API 方法 LiveStream.list 不返回 YouTube 提议的流

php - 使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息

javascript - 如何使用javascript或jquery动态检查表格每一行中复选框的值

javascript - 社交媒体 JavaScript(按钮)无法与 Google+、Facebook、YouTube 等兼容