javascript 使用其 url 生成 youtube 视频的嵌入代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21476121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:00:02  来源:igfitidea点击:

generate youtube video's embed code using its url

javascripthtmlapiyoutube

提问by halkujabra

I want to add youtube videos to my site. I want to know, is there a way to generate*'embed code'* by givingthe 'url'(that we input into the browser) as input?

我想将 YouTube 视频添加到我的网站。我想知道,有没有一种方法来生成* “嵌入代码”*通过赋予“网址”(我们输入到浏览器)作为输入

In simpler words, is there a way to get 'embed code'as output by giving the 'url'of the video as input?

简单来说,有没有办法通过将视频的“url”作为输入来获得“嵌入代码”作为输出?

I read the Youtube's developer API docs. It talks about embedding the video but, I couldn't find anything regarding generation of the 'embed code'from the 'video's url'

我阅读了 Youtube 的开发人员 API 文档。它谈到了嵌入视频,但是,我找不到任何关于从“视频的 url”生成“嵌入代码”的信息

I know I can manually copy the 'embed code' from the share link under youtube's video. But I want to generate it using the 'video's url', removing the need of human effort in it.

我知道我可以从 youtube 视频下的共享链接中手动复制“嵌入代码”。但我想使用“视频的网址”来生成它,从而无需人工操作。

Edit:

编辑:

Some videos are blocked by youtube. So, is there a way to show those videos on the site? If not, is there a way to at least detect whether it is 'blocked or not'?

一些视频被 YouTube 屏蔽了。那么,有没有办法在网站上显示这些视频?如果没有,有没有办法至少检测它是否被“阻止”?

For ex.- *"http://www.youtube.com/watch?v=aOWB0yGTrNY&list=PLZnxqowr6IKiDvDkEfptk0lcXuJqzn_dN"*. This link when directly accessed through 'url' in an 'iframe' doesn't work. But if we go to this video on youtube and use its 'embed code url link' in an 'iframe', then it works. Please help me out with this.

例如。- *" http://www.youtube.com/watch?v=aOWB0yGTrNY&list=PLZnxqowr6IKiDvDkEfptk0lcXuJqzn_dN"*. 通过“iframe”中的“url”直接访问时,此链接不起作用。但是,如果我们在 youtube 上访问此视频并在“iframe”中使用其“嵌入代码 URL 链接”,则它可以工作。这个你能帮我吗。

采纳答案by cocco

Embed youtube video

嵌入 YouTube 视频

This creates the embed code... that you then have to append to the body.or wherever

这将创建嵌入代码...然后您必须将其附加到 body.or 任何地方

Function

功能

function yt(url){
return '<iframe width="420" height="315" src="'+url+
'" frameborder="0" allowfullscreen></iframe>'
}

Usage

用法

document.body.appendChild(document.createElement('div')).innerHTML=yt('yturl')

if you have any questions just ask.

如果你有问题,就问吧。

this is prolly not the best solution but it's what you asked.

这不是最好的解决方案,但这是您的要求。

EDIT

编辑

Some videos are blocked from youtube/user and you can't simply add them to you page. btw all videos don't work inside other iframes.

某些视频被 youtube/user 阻止,您不能简单地将它们添加到您的页面。顺便说一句,所有视频都不能在其他 iframe 中工作。

you can check the origin errors in your console.

您可以在控制台中检查源错误。

EDIT2

编辑2

Video is embeddable?

视频可以嵌入吗?

You need to use the api to check for that

您需要使用 api 来检查

http://gdata.youtube.com/feeds/api/videos?v=2&alt=json&q=Main%20Tera%20Hero%20-%20Official%20Trailer%20(HD)

http://gdata.youtube.com/feeds/api/videos?v=2&alt=json&q=Main%20Tera%20Hero%20-%20Official%20Trailer%20(HD)

i think that what you are searching for is inside this tags

我认为您要搜索的内容在此标签内

yt$accessControl

or

或者

app$control

you can read more here

你可以在这里阅读更多

https://stackoverflow.com/a/3937794/2450730

https://stackoverflow.com/a/3937794/2450730

&&

&&

http://apiblog.youtube.com/2011/12/understanding-playback-restrictions.html

http://apiblog.youtube.com/2011/12/understanding-playback-restrictions.html

回答by jawache

You need to add the specific youtube embed link, vs the watch?v= link, will need to parse out the id of the video from whatever url version you have.

您需要添加特定的 youtube 嵌入链接,而不是 watch?v= 链接,需要从您拥有的任何 url 版本中解析出视频的 id。

        <iframe src='http://www.youtube.com/embed/aOWB0yGTrNY/'
                frameborder='0'
                allowfullscreen ></iframe >

回答by Vinícius Ferraz

  1. Extract video ID from its URL (e.g. using regex)
  2. Execute this request using your favorite tools (cURL or any other language/framework specific abstraction for HTTP) GET https://www.googleapis.com/youtube/v3/videos?part=player&id={VIDEO_ID}&maxResults=1&key={YOUR_API_KEY}
  3. Debug the response, your ready-to-use video's embed code will be there.
  1. 从其 URL 中提取视频 ID(例如使用正则表达式)
  2. 使用您喜欢的工具(cURL 或任何其他语言/框架特定的 HTTP 抽象)执行此请求 GET https://www.googleapis.com/youtube/v3/videos?part=player&id={VIDEO_ID}&maxResults=1&key={YOUR_API_KEY}
  3. 调试响应,您即用型视频的嵌入代码将在那里。