java 如何以编程方式获取托管在 youtube 中的视频的嵌入 HTML 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4825251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the embed HTML code for a video hosted in youtube programmatically
提问by Jason
How to get the embed HTML code for a video hosted in youtube programmatically. What Java API is available
如何以编程方式获取托管在 youtube 中的视频的嵌入 HTML 代码。有哪些 Java API 可用
采纳答案by probabilityzero
Assuming you have the URL of the video, it's fairly simple to generate one. You need the end of the URL (the part after the /watch?v=, let's call it ID). To generate the iframe embed html, just place it in the appropriate place (in the src attribute, don't include the brackets):
假设您有视频的 URL,生成一个相当简单。您需要 URL 的结尾(在/watch?v=之后的部分,我们称之为ID)。要生成 iframe 嵌入 html,只需将其放在适当的位置(在 src 属性中,不要包含括号):
<iframe title="YouTube video player" class="youtube-player" type="text/html" width="640"
height="390" src="http://www.youtube.com/embed/{ID}" frameborder="0"
allowFullScreen></iframe>
There are a couple of ways to get the v parameter from the URL. A regular expression would work.
有几种方法可以从 URL 获取 v 参数。正则表达式会起作用。
回答by ephemient
Use the YouTube Data API(there's pre-built GData client libraries, or you can do the HTTP/XML stuff yourself).
使用YouTube 数据 API(有预先构建的GData 客户端库,或者您可以自己做 HTTP/XML 的东西)。
One of the <media:content/>
entries will contain a URL for the embeddable SWF, if the video is embeddable.
<media:content/>
如果视频可嵌入,其中一个条目将包含可嵌入 SWF 的 URL。
回答by billynoah
Though the accepted answer works, if you want to do this programmatically you need the correct aspect ratio in order to generate optimal iframe dimensions for your video. I wrote the following php function that can generate a link for you on the fly. It uses the bash utility youtube-dlto get information about the video from any youtube link, so you'll need to make sure that's installed (apt-get install youtube-dl
should work on Ubuntu or other debian flavors)
尽管接受的答案有效,但如果您想以编程方式执行此操作,则需要正确的纵横比才能为您的视频生成最佳 iframe 尺寸。我编写了以下 php 函数,可以即时为您生成链接。它使用 bash 实用程序youtube-dl从任何 youtube 链接获取有关视频的信息,因此您需要确保已安装(apt-get install youtube-dl
应适用于 Ubuntu 或其他 debian 版本)
function getYoutubeEmbed($link, $size = [], $options = [], $privacy = false) {
$options += [
'rel' => true, // Show suggested videos when the video finishes.
'controls' => true, // Show player controls.
'showinfo' => true, // Show video title and player actions.
];
$json = json_decode(exec('youtube-dl -j --no-warnings ' . $link . ' 2>/dev/null'));
if ($json && !empty($id = $json->id) && !empty($width = $json->width) && !empty($height = $json->height)) {
$args = [];
foreach ($options as $option => $value) {
if (!$value) {
$args[] = $option . '=0';
}
}
if ($size) {
if (!empty($size['width']) && !empty($size['height'])) {
$width = $size['width'];
$height = $size['height'];
} else if (!empty($size['width'])) {
$height = ceil(($height * $size['width']) / $width);
$width = $size['width'];
} else if (!empty($size['height'])) {
$width = ceil(($width * $size['height']) / $height);
$height = $size['height'];
}
}
$url = ($privacy ? 'www.youtube-nocookie.com/embed/' : 'www.youtube.com/embed/') . $id . ($args ? '?' . implode('&',$args) : '');
$iframe = '<iframe width="' . $width . '" height="' . $height . '" src="//' . $url . '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
return $iframe;
} else {
return false;
}
}
The function is fairly self explanatory but here's the breakdown:
该功能是不言自明的,但这里是细分:
- At the minimum you need to supply a link for the first argument.
- The second argument is an array of width, height or both. If you only specify one it will treat keep the default aspect ratio and calculate the other dimension for you (this is how I'd typically use it).
- The third argument is an optional array of arguments which are documented in the function itself.
- The fourt is an optional boolean argument for 'privacy' which is explained as:
- 您至少需要为第一个参数提供一个链接。
- 第二个参数是宽度、高度或两者的数组。如果您只指定一个,它将保留默认纵横比并为您计算另一个尺寸(这是我通常使用的方式)。
- 第三个参数是一个可选的参数数组,它们记录在函数本身中。
- Fourt 是 'privacy' 的可选布尔参数,其解释如下:
Enable privacy-enhanced mode. When you turn on privacy-enhanced mode, YouTube won't store information about visitors on your website unless they play the video.
启用隐私增强模式。当您打开隐私增强模式时,除非访问者播放视频,否则 YouTube 不会在您的网站上存储有关访问者的信息。
Usage example:
用法示例:
$link = 'https://www.youtube.com/watch?v=adAqQct3vRI';
echo getYoutubeEmbed($link, ['width' => 560], ['rel' => false]);
Output:
输出:
<iframe width="560" height="315" src="//www.youtube.com/embed/605gdJGdaPE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="560" height="315" src="//www.youtube.com/embed/605gdJGdaPE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>