Youtube API https Protocol - http

When I create an iframe player API using the onYouTubeIframeAPIReady, the link is created with the http protocol
Example:
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
width: '560',
height: '600',
videoId: '7j8B_r4OfAw',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
iframe Result:
<iframe id="player" frameborder="0" allowfullscreen="1" title="YouTube video player" width="560" height="600" src="http://www.youtube.com/embed/7j8B_r4OfAw?enablejsapi=1"></iframe>
Does anyone know how to do the video to be created with the https protocol? Need to install the api on a platform.
Please, suggest!

You can specify https if you create the iframe element directly in your html, rather than using the div which gets replaced later on. You can create the iframe tag dynamically if you need to. Look at the bottom of this section, which spells out how to do it.
Beware — even if you load the player over https, the actual video stream may be served over http. That seems to cause a mixed-mode warning in Chrome, though not other browsers (in my experience last year; it may have since changed). See this official blog post, which explains that the player can be loaded over https but warns that the video still won't necessarily be served that way.

Related

WordPress youtube iframe not working after change content output function in post

I have changed in theme file to display post output like below but now if am going to add any youtube URL in post text it is not showing video iframe. also, I can not use the_content() function because there is some other issue with that function.
//the_content();
$post_content .= $post->post_content;
echo $post_content;
can anyone help me to show the iframe if I add the youtube video URL in the post and display the iframe in the frontend? I know this is default WordPress functionality but it is not working.
any solution will be fine if there is a jQuery function also fine.
So, if I've understood correctly, that you can add content to a post and it shows in front end but that youtube url does not display correctly in an iframe (so it's an oembed problem) then it might make sense to think about adding the whole embed code from YouTube.
Instead of pasting https://youtu.be/TE66McLMMEw into your content area, you might paste in this instead:
<iframe width="560" height="315" src="https://www.youtube.com/embed/TE66McLMMEw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
There are ways of using JS, grabbing a YouTube url and dynamically generating an iframe for it using the YouTube Player API (as per the video, or full docs and examples here: https://developers.google.com/youtube/iframe_api_reference ) but honestly, it seems a very complex way to solve the problem. It might be better to try and find a more robust fix that lets you use the_content().
UPDATE: if you cannot use iframe directly
We can use notes from youtube API (and a great regex from this answer here: https://stackoverflow.com/a/8260383/8228720) to generate iframe on the fly. You just need to create a div with id "player" and paste the YT url inside it.
HTML
<div id="player">https://youtube-video-link-goes-here</div>
JS
//Get video url from div
let videoUrl = document.getElementById("player").textContent;
let videoId = youtube_parser(videoUrl);
var tag = document.createElement('script');
//Parse url for video id
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
}
//Dynamically create iframe in the "player" div, adding our video id to it.
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: videoId,
playerVars: {
'playsinline': 1
},
});
}

Google Video embedded in the Youtube IFrame

I am curious if it is possible to use this api with embedded videos that are only on Google Drive? I am trying the code below and the player is loading, but I am unable to get it to control the the iframe. Is it just not possible?
<div class="view" id="viewer">
<iframe id="player"
type="text/html"
width="640" height="390"
src="https://drive.google.com/file/d/<?= vidId ?>/preview?enablejsapi=1"
frameborder="0"></iframe>
</div>
<script src="https://www.youtube.com/iframe_api"></script>
<script>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(e) {
console.log('player ready');
}
</script>
If it is not possible with the youtube iframe api, is there anyway with javascript to start and stop videos?
Thanks.
It was a way to control the iframe player google drive but it's don't work since august 2016. Google drive embed NO iframe
Video just play without api control.

YouTube IFrame Player API - preview frame

Does anyone know if it's possible to define which video-frame is used as a preview, using the YouTube IFrame Player API?
When my video is embedded now, it shows a video-frame near the end of my video, while I want it to be one of the first frames.
I don't think that this is possible using the API, but you could display the preview image yourself. This was discussed on following stackoverflow page: YouTube embedded video: set different thumbnail
No. It's not (yet) possible.
But Niels his answer got me inspired.
Actually his referral was about Youtube video's which are already embedded in the document, while the YouTube IFrame Player API embeds the video by javascript after the document has finished loading.
HTML part:
<div id="ytplayer">
<img id="ytThumb" src="http://placehold.it/640x390&text=Play">
</div>
Javascript part:
$(document).ready(function(){
$('#ytThumb').click(function() {
//Embed the youtube player
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
});
});
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player('ytplayer', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: {
'autoplay': 1,
'rel': 0
}
});
}
While my container, image and video all have the same dimensions.

Using <iframe> as source with clmtrackr

I am working with the example for clmtrackr here. I am trying to use an <iframe> as a source of video for the code (as opposed to using a tag, and doing this returns no results.
The example code :
<div id="container">
<video id="video" width="368" height="288" autoplay="" loop="">
<source src="./media/franck.ogv" type="video/ogg">
</video>
<canvas id="canvas" width="368" height="288"></canvas>
</div>
<script>
var videoInput = document.getElementById('video');
var ctracker = new clm.tracker();
ctracker.init(pModel);
ctracker.start(videoInput);
function positionLoop() {
requestAnimationFrame(positionLoop);
var positions = ctracker.getCurrentPosition();
// do something with the positions ...
// print the positions
var positionString = "";
if (positions) {
for (var p = 0;p < 10;p++) {
positionString += "featurepoint "+p+" : ["+positions[p][0].toFixed(2)+","+positions[p][1].toFixed(2)+"]<br/>";
}
document.getElementById('positions').innerHTML = positionString;
}
}
positionLoop();
var canvasInput = document.getElementById('canvas');
var cc = canvasInput.getContext('2d');
function drawLoop() {
requestAnimationFrame(drawLoop);
cc.clearRect(0, 0, canvasInput.width, canvasInput.height);
ctracker.draw(canvasInput);
}
drawLoop();
</script>
Has anyone attempting this yet? Any help would be much appreciated!
Thank you,
In most cases, this is not possible because of security measures built into the browser/DOM.
clmtrackr needs to access the pixels of the video file, and to do that, it needs direct access to the <video> element, which in your case is inside the iframe. In order to do that, you would need to reach into the DOM elements inside the iframe to find the video element and pass it to clmtrackr. However, it is not possible to do that unless the iframe is being served from the same domain as the outside page.
Presumably, if the iframe is coming from your own site, then you already have a way to access the source URL of the video file and can create your own element. Then you don't need the iframe. So I'm assuming you're trying to access another video hosting site, like YouTube.
Now, to be thorough, even if you could access the video element inside the iframe or if you could somehow infer the url of the video file and create your own element, clmtrackr cannot access the pixels unless that video file is, once again, coming from the same domain. This is another security measure.
The exception is if the video is served with CORS headers, as described here:
http://jbuckley.ca/2012/02/cross-origin-video/
and here:
https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
Unfortunately, few video hosting services serve their videos with CORS headers.

Youtube iframe API with AJAX and autoplay/queuing

I'm using the Youtube iframe API, and I'm trying to get autoplay and queuing, polling, and start/stop working.
I'm starting with just getting autoplay to work. I've looked at the developer documentation so please don't post another link to that.
I include the API:
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
but because this is an AJAX request, and the DIV which holds the player isn't in the DOM when the page is loaded, I don't call function onYouTubePlayerAPIReady()
Instead, when the player is to be loaded (which is always long after the DOM has been loaded), I call
var videoID = video.split('=').pop();
var player = new YT.Player('video_holder', {
width: "480",
height: "295",
videoId: videoID,
events: {'onready': onPlayerReady}
});
function onPlayerReady(event) {
event.target.playVideo();
}
This code loads the video, but the video doesn't play. The only error I have in the console is the unsafe JavaScript attempt to access error, which I understand is common for the iframe API.
The event names are case sensitive so change onready to onReady.

Resources