Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting title and description of embedded YouTube video

On a site I'm developing I embed videos from YouTube and want to get the video title and its description.

How do I get that information?

like image 222
Syed Qarib Avatar asked Mar 01 '11 13:03

Syed Qarib


People also ask

How do you find out where your YouTube video is embedded?

To see YouTube analytics on embedded videos, click the Reach Viewers tab at the top of YouTube Studio. From here, scroll down and expand the Traffic source: External box.

How do you embed stuff in a YouTube video description?

Copy the full URL that you want to link to, and then paste it into the YouTube video description box. Make sure to hit "Save" afterwards. 7. YouTube should automatically make the text into a URL on the video's description.


3 Answers

Youtube API V2.0 has been deprecated. It shows some wrong value for title "youtube.com/devicesupport" . pLease switch on to API V3.0

YOu can refer the following PHP code and modify yours in js or jquery as per your needs..

function youtube_title($id) {
 $id = 'YOUTUBE_ID';
// returns a single line of JSON that contains the video title. Not a giant request.
$videoTitle = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=YOUR_API_KEY&fields=items(id,snippet(title),statistics)&part=snippet,statistics");
// despite @ suppress, it will be false if it fails
if ($videoTitle) {
$json = json_decode($videoTitle, true);

return $json['items'][0]['snippet']['title'];
} else {
return false;
}
}

update:

Jquery code to get the title-

 $.getJSON('https://www.googleapis.com/youtube/v3/videos?id={VIDEOID}&key={YOUR API KEY}&part=snippet&callback=?',function(data){
    if (typeof(data.items[0]) != "undefined") {
        console.log('video exists ' + data.items[0].snippet.title);
       } else {
        console.log('video not exists');
     }   
    });
like image 51
Prashant Bhujbal Avatar answered Oct 18 '22 02:10

Prashant Bhujbal


You can do it with oembed. Example:

http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json
like image 23
Matej Baćo Avatar answered Oct 18 '22 01:10

Matej Baćo


To get the DESCRIPTION element, you need to access the gdata version of the video's info, and you can return json using alt=json on the path. In this case, oHg5SJYRHA0 is the video ID, found at the end of the url of the video you're working with on YouTube, e.g. www.youtube.com/watch?v=oHg5SJYRHA0

http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json&prettyprint=true

(the prettyprint is formatting to make that easy to read, you don't need it for what you're doing)

You can grab the JSON, add it into a variable and access it using jQuery:

var youTubeURL = 'http://gdata.youtube.com/feeds/api/videos/oHg5SJYRHA0?v=2&alt=json';
var json = (function() {
    var json = null;
    $.ajax({
        'async': false,
        'global': false,
        'url': youTubeURL,
        'dataType': "json",
        'success': function(data) {
            json = data;
        }
    });
    return json;
})();

Then access it using object notation:

alert("Title: " + json.entry.title.$t +"\nDescription:\n " + json.entry.media$group.media$description.$t + "\n");
like image 10
Will Lanni Avatar answered Oct 18 '22 02:10

Will Lanni