How to convert url to embed code from youtube and vimeo url dynamically.

Some video projects includes google api need to embed Youtube video url dynamically. Here is the solution for extracting the unique id from url and convert them to embed form. Follow the step wise step:

Step 1.

First get the video id

$step1=explode('v=', $url);
$step2 =explode('&',$step1[1]);
$vedio_id = $step2[0];

Step 2.

Create embed url from vedio id
The iframe src of youtube is http://www.youtube.com/embed/$vedio_id so the embed code would be

<iframe width="320" height="240" src="http://www.youtube.com/embed/'. $vedio_id.'" frameborder="0"></iframe>

For autoplay you use autoplay=1 parameter.

<iframe width="320" height="240" src="http://www.youtube.com/embed/'. $vedio_id.'?autoplay=1" frameborder="0"></iframe>

Step 3.

Get youtube vedio thumbnail
you can get thumbnail of any youtube vedio with the url
Large thumbnail : http://img.youtube.com/vi/$vedio_id/0.jpgSmall thumbnail : http://img.youtube.com/vi/$vedio_id/1.jpg
and so on…

How to convert url to embed code from vimeo url

Step 1.

Get the video id.

$vedio_id = str_replace('http://vimeo.com/','',$url);

Step 2.

Create embed url from video id.
The embed url for vimeo is
$embedurl = “http://player.vimeo.com/video/”.$vedio_id;
and iframe code for vimeo would be

<iframe width="320" height="240" src="'.$embedurl.'" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

Step 3.

Some video projects need to embed Vimeo video url dynamically. Here is the solution for extracting the unique id from url and convert them to embed form. Follow the step wise step:

Get vedio thumbnail of vimeo.
this step is slightly diffrent then youtube in this case we will  read video files in the vimeo server and locate its thumbnail image.