Javascript – generate youtube video’s embed code using its url

apihtmljavascriptyoutube

I want to add youtube videos to my site. I want to know, is there a way to generate *'embed code'* by giving the 'url'(that we input into the browser) as input?

In simpler words, is there a way to get 'embed code' as output by giving the 'url' of the video as input?

I read the Youtube's developer API docs. It talks about embedding the video but, I couldn't find anything regarding generation of the 'embed code' from the 'video's url'

I know I can manually copy the 'embed code' from the share link under youtube's video. But I want to generate it using the 'video's url', removing the need of human effort in it.

Edit:

Some videos are blocked by youtube. So, is there a way to show those videos on the site? If not, is there a way to at least detect whether it is 'blocked or not'?

For ex. – *"http://www.youtube.com/watch?v=aOWB0yGTrNY&list=PLZnxqowr6IKiDvDkEfptk0lcXuJqzn_dN"*. This link when directly accessed through 'url' in an 'iframe' doesn't work. But if we go to this video on youtube and use its 'embed code url link' in an 'iframe', then it works. Please help me out with this.

Best Answer

You need to add the specific youtube embed link, vs the watch?v= link, will need to parse out the id of the video from whatever url version you have.

        <iframe src='http://www.youtube.com/embed/aOWB0yGTrNY/'
                frameborder='0'
                allowfullscreen ></iframe >