Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto Play YouTube videos via HTML5 video tag

Tags:

This code works for a moment but i think the links changes, cause the next day it is not found?
the video played under Firefox/Chrome/Opera... how to make the video tag play this video permanently?!

<video width="480" height="270" controls="controls" style="color:green;">   <source src="youtubelink" type="video/mp4">   <source src="youtubelink" type="video/ogg">   <source src="youtubelink" type="video/webm"> Your browser does not support the video tag. </video> 
like image 257
blade19899 Avatar asked Nov 22 '12 15:11

blade19899


People also ask

How do I embed a YouTube video in HTML5?

When you click the share button, a share panel will open displaying some more buttons. Now click on the Embed button, it will generate the HTML code to directly embed the video into the web pages. Just copy and paste that code into your HTML document where you want to display the video and you're all set.

Can we play video in HTML5?

With the introduction of HTML5, you can now place videos directly into the page itself. This makes it possible to have videos play on pages that are designed for mobile devices, as plugins like Adobe Flash Player don't work on Android or iOS. The HTML <video> element is used to embed video in web documents.

Is YouTube an HTML5 video?

YouTube now defaults to HTML5 <video> Over the last four years, we've worked with browser vendors and the broader community to close those gaps, and now, YouTube uses HTML5 <video> by default in Chrome, IE 11, Safari 8 and in beta versions of Firefox.


1 Answers

There isn't really a reliable way to actually play a YouTube video inside a real video tag. YouTube doesn't want you doing that, and it's probably against their TOS. In any case, that URL is probably going to change regularly, whether YT adjusts their infrastructure or they go out of their way to stop people from directly accessing the video files.

However, there are a few steps you can take to be able to do just about everything you could do if you were using the video tag. To start, you can add the "html5=1" hint to the embed, which will tell youtube to use html5 video instead of Flash (it usually complies, but not always). The video will be in an iframe, but you can apply all the usual CSS tricks to that iframe - opacity, transforms, etc.

If you're using the YouTube API, add html5: 1 to the playerVars. If you're just doing a straight iframe embed, add it to the query string like this: http://www.youtube.com/embed/okqEVeNqBhc?html5=1

Now, if you want to go one step further, Popcorn.js now has a nifty wrapper object for the YouTube API that will make a YouTube (they have one for Vimeo too) video behave like an HTMLVideoElement, with most of the same properties, methods and events. It isn't perfect, but it's pretty good.

Note: The official source for that file is on the mozilla/popcorn-js repo, but this one is currently ahead with bug fixes and features. You need to include the latest version of Popcorn.js and wrappers/common/popcorn._MediaElementProto.js from that repo. Make sure you add &html5=1 to the YT URL when you set the src.

The differences you'll notice are:

  • Even with the wrapper, the HTML5 video API just performs a little bit better than the YT API. e.g., more responsive and better reporting of buffering.

  • You can't get rid of the YouTube icon in the lower right corner that shows up on pause or mouseover.

  • You can't keep YouTube from showing ads.

  • You can't access the actual video/audio content for things like the Audio API and canvas/webgl drawing. But you couldn't do that anyway because of cross-origin restrictions.

like image 54
brianchirls Avatar answered Oct 09 '22 05:10

brianchirls