1

I'm looking for a way to automatically embed YouTube videos solely based on their YouTube URLs, rather than having to go to the video, click share, click embed, and copy-paste the HTML code into my site.

An example:

The YouTube Video is https://www.youtube.com/watch?v=dQw4w9WgXcQ (Warning: Rick Roll)

When I click Share --> Embed --> The code given is

<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>

Notice how they placed embed in there and altered a lot of the URL

I have 100s of videos that I need to do this for, so obviously going in and manually editing them isn't going to work out.

Is there any way to embed the YouTube video without altering the URL?

Thanks for your help

VMoza
  • 157
  • 1
  • 2
  • 16
  • would you consider downloading them off of youtube then embedding as a local source? – tCoe Jul 15 '16 at 21:54
  • Probably not - the point is that this is all automated such that whenever my database (private) has a YouTube video added, it will automatically update. With the method that you're suggesting, I would have to download the YouTube video every time a new one is added. – VMoza Jul 15 '16 at 21:59

2 Answers2

2

From what I have seen there is a pattern that all the embedding follows.

<iframe width="560" height="315" src="https://www.youtube.com/embed/     " title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Within the src = " " and after the embed/ , there is a portion that would be added that is directly from the link. The section that is put after the embed/ in the code comes after a v= in the URL itself.

You could easily create a macro to speed up the process if it is something you will do often.

0

So the part of the YouTube URL after the v= is the video ID. That corresponds to the second after the embed. So if you somehow get a list of those video IDs you should be able to make some type of loop to just run through and put the video ID after the embed section.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • That is exactly what I was thinking, but the problem is that not all of these YouTube videos are formatted in the same way. They don't all have the "v=" element. – VMoza Jul 15 '16 at 21:52
  • Can you provide some more detail about the URLs that won't work then? – Charlie Fish Jul 15 '16 at 21:53
  • For example if the Rick Roll video was shared in this particular way: https://youtu.be/dQw4w9WgXcQ – VMoza Jul 15 '16 at 21:56
  • Ok so same thing. That section right after the /. dQw4w9WgXcQ is the video ID. You probably need to come up with some function to detect which one is which and do it that way. OR you could always just get the last 11 characters as long as all your URLs don't have anything after it http://stackoverflow.com/questions/6180138/whats-the-maximum-length-of-a-youtube-video-id – Charlie Fish Jul 15 '16 at 21:58