将 Youtube 视频源显示到 HTML5 视频标签中?
我正在尝试将 YouTube 视频源放入 HTML5 标记中,但它似乎不起作用。经过一番谷歌搜索后,我发现 HTML5 不支持 YouTube 视频 URL 作为来源。
可以使用 HTML5 嵌入 YouTube 视频吗?如果没有,有什么解决方法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这个答案不再有效,但我正在寻找解决方案。
截至。 2015 / 02 / 24 . 有一个网站 (youtubeinmp4)允许您下载
.mp4 格式
的 YouTube 视频,您可以利用此功能(使用一些 JavaScript)将 YouTube 视频嵌入到标记中。这是一个实际操作的演示。
##优点
##缺点
这显然取决于
youtubeinmp4.com
服务器及其提供下载链接的方式(可以作为源传递) ,所以这个答案将来可能无效。
您无法选择视频质量。
###JavaScript(
加载
后)###Usage(完整)
标准视频格式。
###用法(迷你)
不太常见,但相当小,直接在
标记中使用缩短的 url
youtu.be
作为src
属性。This answer does not work anymore, but I'm looking for a solution.
As of . 2015 / 02 / 24 . there is a website (youtubeinmp4) that allows you to download youtube videos in
.mp4 format
, you can exploit this (with some JavaScript) to get away with embedding youtube videos in<video>
tags. Here is a demo of this in action.##Pros
##Cons
It obviously depends on the
youtubeinmp4.com
servers and their way of providing a downloading link (which can be passed as a<video>
source), so this answer may not be valid in the future.You can't choose the video quality.
###JavaScript (after
load
)###Usage (Full)
Standard video format.
###Usage (Mini)
A little less common but quite smaller, using the shortened url
youtu.be
as thesrc
attribute directly in the<video>
tag.标签用于加载支持格式的视频(可能因浏览器而异)。
YouTube 嵌入链接不仅仅是视频,它们通常是包含逻辑的网页,其中包含用于检测用户支持的内容以及如何使用 HTML5、Flash 或基于用户 PC 上可用的其他插件来播放 YouTube 视频的逻辑。这就是为什么您在使用 YouTube 视频的视频标签时遇到困难。
YouTube 确实提供了一个开发者 API,用于将 YouTube 视频嵌入到您的页面中。
我制作了一个 JSFiddle 作为实例: http://jsfiddle.net/zub16fgt/
你可以阅读更多内容有关 YouTube API 的信息,请访问:https://developers.google.com/youtube/iframe_api_reference#Getting_Started
代码也可以在下面
在您的 HTML 中: 找到
在你的Javascript中:
The
<video>
tag is meant to load in a video of a supported format (which may differ by browser).YouTube embed links are not just videos, they are typically webpages that contain logic to detect what your user supports and how they can play the youtube video, using HTML5, or flash, or some other plugin based on what is available on the users PC. This is why you are having a difficult time using the video tag with youtube videos.
YouTube does offer a developer API to embed a youtube video into your page.
I made a JSFiddle as a live example: http://jsfiddle.net/zub16fgt/
And you can read more about the YouTube API here: https://developers.google.com/youtube/iframe_api_reference#Getting_Started
The Code can also be found below
In your HTML:
In your Javascript:
我为这个确切的功能创建了一个相对较小(4.89 KB)的 JavaScript 库。
在我的 GitHub 上找到:https://github.com/thelevicole/youtube-to- html5-loader/
就像这样简单:
这里的工作示例: https://jsfiddle.net /thelevicole/5g6dbpx3/1/
该库的作用是从数据属性中提取视频 ID 并向
https://www.youtube.com/get_video_info?video_id=
。它对包含流信息的响应进行解码,我们可以使用流信息将源添加到标记。
更新 2021 年 6 月
YouTube 最近更新了其 API,该 API 破坏了此软件包的先前版本。现在请使用
4.0.1
及更高版本!更新的示例:https://jsfiddle.net/thelevicole/5g6dbpx3/2/
I have created a realtively small (4.89 KB) javascript library for this exact functionality.
Found on my GitHub here: https://github.com/thelevicole/youtube-to-html5-loader/
It's as simple as:
Working example here: https://jsfiddle.net/thelevicole/5g6dbpx3/1/
What the library does is extract the video ID from the data attribute and makes a request to the
https://www.youtube.com/get_video_info?video_id=
. It decodes the response which includes streaming information we can use to add a source to the<video>
tag.UPDATE June 2021
YouTube have recently updated their API which has broken previous versions of this package. Please now use versions
4.0.1
and up! Updated example:https://jsfiddle.net/thelevicole/5g6dbpx3/2/
第 1 步:将
&html5=True
添加到您最喜爱的 YouTube 网址第 2 步:在源代码中查找
标记
第 3 步:添加
controls= “controls”
到视频标签:示例:
请注意,似乎有一些
过期
的东西。我不知道src
字符串可以工作多长时间。还在考验自己。
编辑(2011 年 7 月 28 日):请注意,此视频 src 特定于您用于检索页面源的浏览器。我认为 Youtube 会动态生成此 HTML(至少目前是这样),因此在测试中,如果我在 Firefox 中复制,则该 HTML 在 Firefox 中有效,但在 Chrome 中无效。
Step 1: add
&html5=True
to your favorite youtube urlStep 2: Find
<video/>
tag in sourceStep 3: Add
controls="controls"
to video tag:<video controls="controls"..../>
Example:
Note there seems to some
expire
stuff. I don't know how long thesrc
string will work.Still testing myself.
Edit (July 28, 2011): Note that this video src is specific to the browser you use to retrieve the page source. I think Youtube generates this HTML dynamically (at least currently) so in testing if I copy in Firefox this works in Firefox, but not Chrome, for example.
怎么样 hooktube 是吗?他们实际上并不使用 html5 元素的视频 URL,而是调用该视频的 google 视频重定向器 url。看看他们如何呈现一些 despacito 随机视频...
代码适用于以下视频页面 https:/ /hooktube.com/watch?v=72UO0v5ESUo
youtube 到 mp3 另一方面已经变得极其现在一半的视频下载请求返回 download.html 的货币化怪物...烦人...
这个答案中的 2 个链接是我个人对这两种资源的体验。 hooktube 是多么美好和新鲜,实际上有助于避免审查和地理限制.. 看看,它非常酷。 youtubeinmp4 是一个弹出怪物,现在称为 ConvertInMp4...
how about doing it the way hooktube does it? they don't actually use the video URL for the html5 element, but the google video redirector url that calls upon that video. check out here's how they present some despacito random video...
the code is for the following video page https://hooktube.com/watch?v=72UO0v5ESUo
youtube to mp3 on the other hand has turned into extremely monetized monster that returns now download.html on half of video download requests... annoying...
the 2 links in this answer are to my personal experiences with both resources. how hooktube is nice and fresh and actually helps avoid censorship and geo restrictions.. check it out, it's pretty cool. and youtubeinmp4 is a popup monster now known as ConvertInMp4...
如果有人偶然发现这个问题,嵌入 YouTube 视频的一个巧妙方法是使用
embed
标签,如下所示:<嵌入 src="https://www.youtube-nocookie.com/embed/DelkRGZCtTs" width="100%" height="333">
In case anyone stumbles upon this question, a neat way to embed YouTube video is to use
embed
tag, like so:<embed src="https://www.youtube-nocookie.com/embed/DelkRGZCtTs" width="100%" height="333">
W3schools 给出了最简单的答案。
https://www.w3schools.com/html/html_youtube.asp
The easiest answer is given by W3schools.
https://www.w3schools.com/html/html_youtube.asp
通过在您的网站中嵌入新的 iframe 标记,代码将自动检测您是否使用支持 HTML5 的浏览器。
嵌入YouTube视频的iframe代码如下,只需复制视频ID并替换为以下代码:
With the new iframe tag embedded in your website, the code will automatically detect whether you are using a browser that supports HTML5 or not.
The iframe code for embedding YouTube videos is as follows, simply copy the Video ID and replace in the code below: