将 Youtube 视频源显示到 HTML5 视频标签中?

发布于 2024-10-20 04:39:01 字数 169 浏览 6 评论 0 原文

我正在尝试将 YouTube 视频源放入 HTML5 标记中,但它似乎不起作用。经过一番谷歌搜索后,我发现 HTML5 不支持 YouTube 视频 URL 作为来源。

可以使用 HTML5 嵌入 YouTube 视频吗?如果没有,有什么解决方法吗?

I'm trying to put a YouTube video source into the HTML5 <video> tag, but it doesn't seem to work. After some Googling, I found out that HTML5 doesn't support YouTube video URLs as a source.

Can you use HTML5 to embed YouTube videos? If not, is there any workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

背叛残局 2024-10-27 04:39:01

这个答案不再有效,但我正在寻找解决方案。

截至。 2015 / 02 / 24 . 有一个网站 (youtubeinmp4)允许您下载 .mp4 格式 的 YouTube 视频,您可以利用此功能(使用一些 JavaScript)将 YouTube 视频嵌入到 标记中。这是一个实际操作的演示

##优点

  • 相当易于实施
  • 实际上服务器响应速度非常快(检索视频不需要太多时间)。
  • 抽象(接受的解决方案,即使工作正常,也只有在您事先知道要播放哪些视频的情况下才适用,这适用于任何用户输入的网址)。

##缺点

  • 这显然取决于 youtubeinmp4.com 服务器及其提供下载链接的方式(可以作为 源传递) ,所以这个答案将来可能无效。

  • 您无法选择视频质量。


###JavaScript(加载后)

videos = document.querySelectorAll("video");
for (var i = 0, l = videos.length; i < l; i++) {
    var video = videos[i];
    var src = video.src || (function () {
        var sources = video.querySelectorAll("source");
        for (var j = 0, sl = sources.length; j < sl; j++) {
            var source = sources[j];
            var type = source.type;
            var isMp4 = type.indexOf("mp4") != -1;
            if (isMp4) return source.src;
        }
        return null;
    })();
    if (src) {
        var isYoutube = src && src.match(/(?:youtu|youtube)(?:\.com|\.be)\/([\w\W]+)/i);
        if (isYoutube) {
            var id = isYoutube[1].match(/watch\?v=|[\w\W]+/gi);
            id = (id.length > 1) ? id.splice(1) : id;
            id = id.toString();
            var mp4url = "http://www.youtubeinmp4.com/redirect.php?video=";
            video.src = mp4url + id;
        }
    }
}

###Usage(完整)

<video controls="true">
        <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>

标准视频格式。

###用法(迷你)

<video src="youtu.be/MLeIBFYY6UY" controls="true"></video>

不太常见,但相当小,直接在 标记中使用缩短的 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

  • Fairly easy to implement.
  • Quite fast server response actually (it doesn't take that much to retrieve the videos).
  • Abstraction (the accepted solution, even if it worked properly, would only be applicable if you knew beforehand which videos you were going to play, this works for any user inputted url).

##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)

videos = document.querySelectorAll("video");
for (var i = 0, l = videos.length; i < l; i++) {
    var video = videos[i];
    var src = video.src || (function () {
        var sources = video.querySelectorAll("source");
        for (var j = 0, sl = sources.length; j < sl; j++) {
            var source = sources[j];
            var type = source.type;
            var isMp4 = type.indexOf("mp4") != -1;
            if (isMp4) return source.src;
        }
        return null;
    })();
    if (src) {
        var isYoutube = src && src.match(/(?:youtu|youtube)(?:\.com|\.be)\/([\w\W]+)/i);
        if (isYoutube) {
            var id = isYoutube[1].match(/watch\?v=|[\w\W]+/gi);
            id = (id.length > 1) ? id.splice(1) : id;
            id = id.toString();
            var mp4url = "http://www.youtubeinmp4.com/redirect.php?video=";
            video.src = mp4url + id;
        }
    }
}

###Usage (Full)

<video controls="true">
        <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>

Standard video format.

###Usage (Mini)

<video src="youtu.be/MLeIBFYY6UY" controls="true"></video>

A little less common but quite smaller, using the shortened url youtu.be as the src attribute directly in the <video> tag.

困倦 2024-10-27 04:39:01

标签用于加载支持格式的视频(可能因浏览器而异)。

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 中: 找到

<div id="player"></div>

在你的Javascript中:

var onPlayerReady = function(event) {
    event.target.playVideo();  
};

// The first argument of YT.Player is an HTML element ID. 
// YouTube API will replace my <div id="player"> tag 
// with an iframe containing the youtube video.

var player = new YT.Player('player', {
    height: 320,
    width: 400,
    videoId : '6Dc1C77nra4',
    events : {
        'onReady' : onPlayerReady
    }
});

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:

<div id="player"></div>

In your Javascript:

var onPlayerReady = function(event) {
    event.target.playVideo();  
};

// The first argument of YT.Player is an HTML element ID. 
// YouTube API will replace my <div id="player"> tag 
// with an iframe containing the youtube video.

var player = new YT.Player('player', {
    height: 320,
    width: 400,
    videoId : '6Dc1C77nra4',
    events : {
        'onReady' : onPlayerReady
    }
});
握住你手 2024-10-27 04:39:01

我为这个确切的功能创建了一个相对较小(4.89 KB)的 JavaScript 库。

在我的 GitHub 上找到:https://github.com/thelevicole/youtube-to- html5-loader/

就像这样简单:

<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4"></video>

<script src="https://cdn.jsdelivr.net/gh/thelevicole/[email protected]/dist/YouTubeToHtml5.js"></script>
<script>new YouTubeToHtml5();</script>

这里的工作示例: https://jsfiddle.net /thelevicole/5g6dbpx3/1/

该库的作用是从数据属性中提取视频 ID 并向 https://www.youtube.com/get_video_info?video_id=。它对包含流信息的响应进行解码,我们可以使用流信息将源添加到 标记。


更新 2021 年 6 月

YouTube 最近更新了其 API,该 API 破坏了此软件包的先前版本。现在请使用 4.0.1 及更高版本!更新的示例:

<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4"></video>

<script src="https://cdn.jsdelivr.net/gh/thelevicole/[email protected]/dist/YouTubeToHtml5.js"></script>
<script>new YouTubeToHtml5();</script>

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:

<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4"></video>

<script src="https://cdn.jsdelivr.net/gh/thelevicole/[email protected]/dist/YouTubeToHtml5.js"></script>
<script>new YouTubeToHtml5();</script>

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:

<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4"></video>

<script src="https://cdn.jsdelivr.net/gh/thelevicole/[email protected]/dist/YouTubeToHtml5.js"></script>
<script>new YouTubeToHtml5();</script>

https://jsfiddle.net/thelevicole/5g6dbpx3/2/

抚笙 2024-10-27 04:39:01

第 1 步:将 &html5=True 添加到您最喜爱的 YouTube 网址

第 2 步:在源代码中查找 标记

第 3 步:添加 controls= “controls” 到视频标签:

示例:

<video controls="controls" class="video-stream" x-webkit-airplay="allow" data-youtube-id="N9oxmRT2YWw" src="http://v20.lscache8.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Cratebypass%2Coc%3AU0hPRVRMVV9FSkNOOV9MRllD&itag=43&ipbits=0&signature=D2BCBE2F115E68C5FF97673F1D797F3C3E3BFB99.59252109C7D2B995A8D51A461FF9A6264879948E&sver=3&ratebypass=yes&expire=1300417200&key=yt1&ip=0.0.0.0&id=37da319914f6616c"></video>

请注意,似乎有一些过期的东西。我不知道 src 字符串可以工作多长时间。

还在考验自己。

编辑(2011 年 7 月 28 日)请注意,此视频 src 特定于您用于检索页面源的浏览器。我认为 Youtube 会动态生成此 HTML(至少目前是这样),因此在测试中,如果我在 Firefox 中复制,则该 HTML 在 Firefox 中有效,但在 Chrome 中无效。

Step 1: add &html5=True to your favorite youtube url

Step 2: Find <video/> tag in source

Step 3: Add controls="controls" to video tag: <video controls="controls"..../>

Example:

<video controls="controls" class="video-stream" x-webkit-airplay="allow" data-youtube-id="N9oxmRT2YWw" src="http://v20.lscache8.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Cratebypass%2Coc%3AU0hPRVRMVV9FSkNOOV9MRllD&itag=43&ipbits=0&signature=D2BCBE2F115E68C5FF97673F1D797F3C3E3BFB99.59252109C7D2B995A8D51A461FF9A6264879948E&sver=3&ratebypass=yes&expire=1300417200&key=yt1&ip=0.0.0.0&id=37da319914f6616c"></video>

Note there seems to some expire stuff. I don't know how long the src 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.

嗳卜坏 2024-10-27 04:39:01

怎么样 hooktube 是吗?他们实际上并不使用 html5 元素的视频 URL,而是调用该视频的 google 视频重定向器 url。看看他们如何呈现一些 despacito 随机视频...

<video id="player-obj" controls="" src="https://redirector.googlevideo.com/videoplayback?ratebypass=yes&mt=1510077993----SKIPPED----amp;utmg=ytap1,,hd720"><source>Your browser does not support HTML5 video.</video>

代码适用于以下视频页面 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...

<video id="player-obj" controls="" src="https://redirector.googlevideo.com/videoplayback?ratebypass=yes&mt=1510077993----SKIPPED----amp;utmg=ytap1,,hd720"><source>Your browser does not support HTML5 video.</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...

迷途知返 2024-10-27 04:39:01

如果有人偶然发现这个问题,嵌入 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">

你是暖光i 2024-10-27 04:39:01

W3schools 给出了最简单的答案。
https://www.w3schools.com/html/html_youtube.asp

  1. 上传您的视频到 Youtube
  2. 记下视​​频 ID
  3. 现在在 HTML5 中编写此代码。
<iframe width="640" height="520"
src="https://www.youtube.com/embed/<VideoID>">
</iframe>

The easiest answer is given by W3schools.
https://www.w3schools.com/html/html_youtube.asp

  1. Upload your video to Youtube
  2. Note the Video ID
  3. Now write this code in your HTML5.
<iframe width="640" height="520"
src="https://www.youtube.com/embed/<VideoID>">
</iframe>
迷爱 2024-10-27 04:39:01

通过在您的网站中嵌入新的 iframe 标记,代码将自动检测您是否使用支持 HTML5 的浏览器。

嵌入YouTube视频的iframe代码如下,只需复制视频ID并替换为以下代码:

<iframe type="text/html" 
    width="640" 
    height="385" 
    src="http://www.youtube.com/embed/VIDEO_ID"
    frameborder="0">
</iframe>

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:

<iframe type="text/html" 
    width="640" 
    height="385" 
    src="http://www.youtube.com/embed/VIDEO_ID"
    frameborder="0">
</iframe>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文