Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve YouTube video information (title, author, etc) by its id using YouTube API

Can anybody suggest how to do it or where to learn about how to do it.

like image 838
Frank Avatar asked Jun 10 '11 08:06

Frank


People also ask

How do I get a YouTube video title from the YouTube API?

In order to make a request to the API, you can use jQuery's getJSON() call to request the title from YouTube via AJAX. YouTube's v3 Data API provides 3 endpoints that can be used to get the title: Snippet Title - The video's title.


2 Answers

See if this helps:

<script type="text/javascript">
  function youtubeFeedCallback( data )
  {
    document.writeln( '<img src="' + data.entry[ "media$group" ][ "media$thumbnail" ][ 0 ].url + '" width="' + data.entry[ "media$group" ][ "media$thumbnail" ][ 0 ].width + '" height="' + data.entry[ "media$group" ][ "media$thumbnail" ][ 0 ].height + '" alt="Default Thumbnail" align="right"/>' );
    document.writeln( '<b>Title:</b> ' + data.entry[ "title" ].$t + '<br/>' );
    document.writeln( '<b>Author:</b> ' + data.entry[ "author" ][ 0 ].name.$t + '<br/>' );
    document.writeln( '<b>Published:</b> ' + new Date( data.entry[ "published" ].$t.substr( 0, 4 ), data.entry[ "published" ].$t.substr( 5, 2 ) - 1, data.entry[ "published" ].$t.substr( 8, 2 ) ).toLocaleDateString( ) + '<br/>' );
    document.writeln( '<b>Duration:</b> ' + Math.floor( data.entry[ "media$group" ][ "yt$duration" ].seconds / 60 ) + ':' + ( data.entry[ "media$group" ][ "yt$duration" ].seconds % 60 ) + ' (' + data.entry[ "media$group" ][ "yt$duration" ].seconds + ' seconds)<br/>' );
    document.writeln( '<b>Rating:</b> ' + new Number( data.entry[ "gd$rating" ].average ).toFixed( 1 ) + ' out of ' + data.entry[ "gd$rating" ].max + '; ' + data.entry[ "gd$rating" ].numRaters + ' rating(s)' + '<br/>' );
    document.writeln( '<b>Statistics:</b> ' + data.entry[ "yt$statistics" ].favoriteCount + ' favorite(s); ' + data.entry[ "yt$statistics" ].viewCount + ' view(s)' + '<br/>' );
    document.writeln( '<br/>' + data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, '<br/>' ) + '<br/>' );
    document.writeln( '<br/><a href="' + data.entry[ "media$group" ][ "media$player" ].url + '" target="_blank">Watch on YouTube</a>' );
  }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>

Code copied from here, jsFiddle Demo

As far as where to learn about how to do it is concerned, have a look at:

  • YouTube Data API Protocol Reference Guide
  • Retrieving Data for a Single Video
  • Understanding Video Feeds and Entries
  • JSON/JavaScript Guide
like image 178
Salman A Avatar answered Oct 04 '22 21:10

Salman A


Found it at http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Retrieve_video_entry

like image 43
Bob Avatar answered Oct 04 '22 21:10

Bob