Reputation: 4908
I want to show YouTube videos through an iFrame on my page, but if I set the iFrame src to the YouTube URL, I get blocked because X-Frames-Options is set to sameorigin. I did find one YouTube video that works. If I set my iFrame src to https://www.youtube.com/embed/LSHuGxB3tTU it plays fine. The key seems to be the embed/ directory. Do all YouTube videos have a version in an embed/ directory that I could pull and show? I don't see anything in the video settings on the YouTube page that mentions embed/.
Thanks for any ideas.
Upvotes: 6
Views: 8641
Reputation: 15936
" If I set my iFrame src to https://www.youtube.com/embed/LSHuGxB3tTU it plays fine.
The key seems to be theembed/
directory"
The whole point of embedding Youtube videos in another, is that iFrames
are used to display their player interface (and run its supporting code). This is done by accessing the embedable player/code by using an iFrame.
Go to any Youtube video and click SHARE
.
In the Share options, click EMBED
and it generates the iFrame code to copy/paste on your site.
"Do all YouTube videos have a version in an embed/ directory that I could pull and show?"
Yes every Youtube video upload gets an embed link auto-generated, just like the video part has multiple resolutions auto-generated downwards from the input size (example: a video of height 360
also generates 240
and 144
sized videos).
To get embed URL (usable in iFrame) :
Use : https://www.youtube.com/embed/
followed by the VIDEO_ID
.
Example:
WhereVIDEO_ID
= wJOfJdWGPMk
.
The iFrame / Embed URL is : https://www.youtube.com/embed/wJOfJdWGPMk
Upvotes: 4