generate youtube video's embed code using its url

14,084

Solution 1

Embed youtube video

This creates the embed code... that you then have to append to the body.or wherever

Function

function yt(url){
return '<iframe width="420" height="315" src="'+url+
'" frameborder="0" allowfullscreen></iframe>'
}

Usage

document.body.appendChild(document.createElement('div')).innerHTML=yt('yturl')

if you have any questions just ask.

this is prolly not the best solution but it's what you asked.

EDIT

Some videos are blocked from youtube/user and you can't simply add them to you page. btw all videos don't work inside other iframes.

you can check the origin errors in your console.

EDIT2

Video is embeddable?

You need to use the api to check for that

http://gdata.youtube.com/feeds/api/videos?v=2&alt=json&q=Main%20Tera%20Hero%20-%20Official%20Trailer%20(HD)

i think that what you are searching for is inside this tags

yt$accessControl

or

app$control

you can read more here

https://stackoverflow.com/a/3937794/2450730

&&

http://apiblog.youtube.com/2011/12/understanding-playback-restrictions.html

Solution 2

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 >

Solution 3

  1. Extract video ID from its URL (e.g. using regex)
  2. Execute this request using your favorite tools (cURL or any other language/framework specific abstraction for HTTP) GET https://www.googleapis.com/youtube/v3/videos?part=player&id={VIDEO_ID}&maxResults=1&key={YOUR_API_KEY}
  3. Debug the response, your ready-to-use video's embed code will be there.
Share:
14,084
halkujabra
Author by

halkujabra

Updated on June 04, 2022

Comments

  • halkujabra
    halkujabra almost 2 years

    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.

  • halkujabra
    halkujabra over 10 years
    This won't work. Try it for this url - "youtube.com/…".
  • cocco
    cocco over 10 years
    this works.. Some videos are blocked from youtube and you can't simply add them to you page. btw all videos don't work inside other iframes
  • halkujabra
    halkujabra over 10 years
    So, how can that be detected whether youtube has blocked it or not? And is there no way to show these blocked videos?
  • cocco
    cocco over 10 years
    not sure but i think if you use the api there is some value that contains public or not.
  • halkujabra
    halkujabra over 10 years
    I have edited the question. Btw, thanks for your efforts
  • cocco
    cocco over 10 years
    i also edited the question... if you continue to change your question and up/downvote my answer i delete it.i'm trying to help you ... i don't work for you.And you should also try to show some effort.some years ago using youtube api was simple. now most of the videos are restricted and you need to be a developer to understand the code generated by google.one thing is adding a simple iframe another is checking if every video you add is copyrighted or user protected.
  • cocco
    cocco over 10 years
    i have to go for now.. if you have any other questions i will answer you later.