OEmbed – transforming video links to embeds

Since the video sharing phenomenon began, many web developers struggle with the problem of embeding the videos without knowing anything but the url where the video page is. Many users didn’t know anything about embed code, how to put videos on their blog or other tehnical stuff like that, so developers tried to find ways of getting the video embed code from url.

The solution was to parse the html of that page, to look for the <embed> tag. However this solution was not very reliable, because any change to the html of the video website, would mess the parsing, making this method to not work anymore.

Fortunetlly, recently, a new format appear: oembed . There are a number of video sharing sites already implementing this service like:

  • youtube.com
  • dailymotion.com
  • vimeo.com
  • blip.tv
  • hulu.com
  • viddler.com
  • revision3.com
  • qik.com
  • scribd.com
  • wordpress.tv
  • tare.ro

additionally oohembed attempt to offer this functionality for other sites.

So, with this script, we can transform the url that user enters in the text box, into a nice little embed code to put in our site:

<?php
$oembedUrls = array (
  'www.youtube.com' => 'http://www.youtube.com/oembed?url=$1&format=json',
  'www.dailymotion.com' => 'http://www.dailymotion.com/api/oembed?url=$1&format=json',
  'www.vimeo.com' => 'http://vimeo.com/api/oembed.xml?url=$1&format=json',
  'www.blip.tv' => 'http://blip.tv/oembed/?url=$1&format=json',
  'www.hulu.com' => 'http://www.hulu.com/api/oembed?url=$1&format=json',
  'www.viddler.com' => 'http://lab.viddler.com/services/oembed/?url=$1&format=json',
  'www.qik.com' => 'http://qik.com/api/oembed?url=$1&format=json',
  'www.revision3.com' => 'http://revision3.com/api/oembed/?url=$1&format=json',
  'www.scribd.com' => 'http://www.scribd.com/services/oembed?url=$1&format=json',
  'www.wordpress.tv' => 'http://wordpress.tv/oembed/?url=$1&format=json',
  'www.5min.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.collegehumor.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.thedailyshow.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.funnyordie.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.livejournal.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.metacafe.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.xkcd.com' => 'http://www.oohembed.com/oohembed/?url=$1',
  'www.yfrog.com' => 'http://www.oohembed.com/oohembed/?url=$1'
);
?>
<html>
<head>
</head>
<body>
<form name="oembed_form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="url" value="<?=$_POST['url']?>" size="50">
<input type="submit" name="Show" value="Show Video">
</form>
<?
if (!empty($_POST['url'])){
	$parts = parse_url($_POST['url']);
	$host = $parts['host'];
	if (empty($host) || !array_key_exists($host,$oembedUrls)){
		echo 'Unrecognized host';
	} else {
		$oembedContents = @file_get_contents(str_replace('$1',$_POST['url'],$oembedUrls[$host]));
		$oembedData = @json_decode($oembedContents);
		$embedCode =  $oembedData->html;
		echo "Embed code for <a href='".$_POST['url']."' target='_blank'>".$_POST['url']."</a> :<br>".$embedCode;
	}
}
?>

If you want you can test the script here

No related posts.

32 Responses to “OEmbed – transforming video links to embeds”

  1. Cruz Stanier says:

    Amazing!!! Bookmarked this web page that has this remarkable solutions. Will appear back again to see if there are any updates. You, the creator, are a grasp. Thank you

  2. andreea says:

    I don’t say this often… But I love you! I was trying to do something for the hulu videos and never worked. Thank you!

  3. Renaud says:

    Hi, thanks for this quite easy and fast solution. Do you have any solution to pass an autoplay to TRUE in the code ?

  4. Renaud says:

    Also, vimeo links don’t work for me with this process !

  5. Marcher says:

    great work..
    can you help me to do the same thing but to get the video thumbnail

    i need something to get (youtube & metacafe &..)videos thumbnail

    i trying use this ..but didn’t work well ..
    <?

    // we make a function, so it will easy to applicate

    function getinput($url)

    {

    $regexp= "http:\/\/www\.youtube\.com\/watch\?v=(.*)(.*)";
    if(preg_match_all("/$regexp/siu", $url, $matches))

    return $matches[1][0];

    }

    // Catch Youtube Thumbnail Image from IRON MAN 2 UPDATE MOVIE

    $youtube_url = “http://www.youtube.com/watch?v=1TiYlG21VAQ”;
    $thumbnail_of_youtube = getinput($youtube_url);

    // The thumbnail image of Youtube always has a pattern like this : http://i1.ytimg.com/vi/[videoid]/default.jpg

    echo ‘‘;

    ?>

    so can any one help me please ..

  6. business solutions says:

    I’m gone to inform my little brother, that he should also visit this website on regular basis to take updated from most recent information.

  7. magnificent submit, very informative. I wonder why the other specialists of this sector
    do not understand this. You must continue your writing. I am confident, you have a great readers’ base already!

  8. What’s up, of course this article is actually pleasant and I have learned lot of things from it on the topic of blogging. thanks.

  9. My coder is trying to persuade me to move to .net from PHP.

    I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using WordPress on several websites for about a year and am
    concerned about switching to another platform. I have heard very good things about blogengine.
    net. Is there a way I can import all my wordpress posts into it?
    Any kind of help would be really appreciated!

  10. Can I just say what a comfort to discover somebody who truly understands what they
    are discussing on the web. You definitely understand how to bring a problem to light and make it important.
    A lot more people have to read this and understand this
    side of your story. It’s surprising you aren’t more popular since you certainly possess the gift.

  11. Carin says:

    Hi, I think your blog might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a
    quick heads up! Other then that, great blog!

  12. Spela Casino says:

    cegaoqiq.dpef, Spela Casino, izYxLKb.

  13. Charles says:

    Just desire to say your article is as astounding. The clearness for your submit is just spectacular and
    that i can assume you are an expert on this subject.
    Well with your permission allow me to grab your feed to stay updated with drawing close
    post. Thanks 1,000,000 and please keep up the enjoyable work.

  14. Hi my friend! I want to say that this article is
    awesome, great written and include approximately all significant
    infos. I would like to look extra posts like this .

  15. This is a topic which is near to my heart.
    .. Take care! Exactly where are your contact details though?

    Feel free to surf to my blog post; how to grow taller at 18 female

  16. Hello! I could have sworn I’ve visited this website before but after going through a few of the articles I realized it’s new to me.
    Nonetheless, I’m definitely delighted I came across it and I’ll be bookmarking it and checking back often!

  17. Stendra says:

    qxcocqiq.dpef/ofu, Stendra, DCtbhEz, [url=http://stendratreatment.com/]When stendra will be available[/url], MLUjGhN, http://stendratreatment.com/ When stendra will be available, TFmFTqt.

  18. hello! , Nice creating really a great deal of! percentage many of us keep in contact excess around your document about America online? I would like a specialist about this space or room to fix the issue. Possibly that’s a person! Looking forward to look a person.

  19. I was recommended this website by my cousin.
    I am not sure whether this post is written by him as nobody else know such
    detailed about my difficulty. You’re wonderful! Thanks!

    my site; vacuum cleaner quest wonderland online

  20. Hey there! I’ve been following your weblog for some time now and finally got the courage to go ahead and give you a shout out from Huffman Texas! Just wanted to tell you keep up the fantastic work!

  21. Do you have a spam problem on this blog; I also am a blogger, and
    I was curious about your situation; many of us have developed some nice procedures and we are
    looking to swap methods with others, why not shoot me an email
    if interested.

Leave a Reply