Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded YouTube video with custom speed (e.g. 3)

I have an embedded YouTube video in one page and have a slider with which I can set the player speed.

I am using player.setPlaybackRate(value);

The problem is that I want ranges from 0.5 to 3, but the player API restricts the values only to predefined [0.25, 0.5, 1, 1.25, 1.5, 2].

In YouTube I can easily adjust the speed with document.getElementsByTagName("video")[0].playbackRate = 3 but on the iframe I do not have such access.

like image 693
Nikolay Kostov Avatar asked Jan 08 '15 09:01

Nikolay Kostov


People also ask

Can you do a custom playback speed on YouTube?

Youtube Custom Speed lets you play any Youtube video at any speed you like, from 0.0125x to 16x! Use the options menu to set your preferred playback rate values. Then, change the speed using buttons at the bottom of the video. It also cooperates with the default Youtube keyboard shortcuts - Shift + , and Shift + .

How do I speed up YouTube embed?

Click on the video on the editor then select a speed. You can choose from 0.5x, 1x, 1.5x, and so on. Or click on 'Custom' and enter your desired speed.

How do you watch YouTube videos at 3x speed?

Open your YouTube app on your mobile phone and go to the video you want to speed up for watching. Click the video once, and a menu overlay to appear, tap the three dots. Then select the playback speed from the new pop-up. Choose the playback speed you prefer to watch.


2 Answers

Where do you see that the player API restricts the values? In the javascript API, you can use setPlaybackRate to set the suggested playback rate, but it says there is no guarentee that what you send will be set. You should use getAvailablePlaybackRates to get the list of playback rates and then choose an appropriate one. You can figure out what rate it was actually set to by listening to the onPlaybackRateChangeevent. If you try to set it to 3 and that is not one of the available rates, it will round towards 1 to the closest rate.

like image 98
Russ Savage Avatar answered Sep 21 '22 21:09

Russ Savage


EDIT: This doesn't work anymore.

This is due to the same-origin policy. When an iframe gets accessed by the root origin (your website) it seems to also change the origin of the iframe. So the video can't load from a different origin (youtube.com). See my test on JSFiddle.

I think the fact, that it worked before was a XSS security issue which has been fixed recently. So I can't imagine modifying something in the youtube iframe is even possible anymore. At least not in this way.

Thanks @maxple for pointing out!


Original post:

This should be possible with newer Browsers and the HTML5 Iframe Sandbox Attribute:

With the option you can access the iframe DOM node.

<iframe id="myframe" sandbox="allow-scripts" src="about:blank">    </iframe>  <script>     var frame = document.getElementById("myframe");     var fdoc = frame.contentDocument;      fdoc.getElementsByTagName("video")[0].playbackRate = 3; // or whatever </script> 

See this post for more info.

like image 34
Dustin Hoffner Avatar answered Sep 20 '22 21:09

Dustin Hoffner