Show Youtube video source into HTML5 video tag?

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?


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&amp;itag=43&amp;ipbits=0&amp;signature=D2BCBE2F115E68C5FF97673F1D797F3C3E3BFB99.59252109C7D2B995A8D51A461FF9A6264879948E&amp;sver=3&amp;ratebypass=yes&amp;expire=1300417200&amp;key=yt1&amp;ip=0.0.0.0&amp;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.


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)/([wW]+)/i);
            if (isYoutube) {
                var id = isYoutube[1].match(/watch?v=|[wW]+/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>
    

    Standart video format.

    Usage (Mini)

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

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

    Hope it helps! :)


    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
        }
    });
    
    链接地址: http://www.djcxy.com/p/28998.html

    上一篇: Youtube API v3,如何获得视频持续时间?

    下一篇: 将Youtube视频源显示为HTML5视频标签?