How do I get the title of a youtube video if I have the Video Id?

做~自己de王妃 提交于 2019-12-17 22:08:48

问题


I'm playing now with the Youtube API and I began a small project (for fun).

The problem Is that I cant find the way to get Title of a video from the Id. (example: ylLzyHk54Z0)

I have looked in the DATA and PLAYER api documentation and I cannot find it.

If someone knows how to do this or if someone could help me find the way to do this, please help me.

NOTE: I'm using javascript. It will be a web app.

EDIT: I have got an Idea. Maybe using a Regular expresion to parse out the title from the page title. I'm working on this.


回答1:


Not entirely possible in javascript since you are trying to get a document from a different domain. If you are happy to throw in a bit of php try this. Tested ok:

<?
    $vidID = $_POST['vidID'];
    $url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
    $doc = new DOMDocument;
    $doc->load($url);
    $title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
?>

<html>
    <head>
        <title>Get Video Name</title>
    </head>
    <body>
        <form action="test.php" method="post">
            <input type="text" value="ID Here" name="vidID" />
            <input type="submit" value="Get Name" />
        </form>
        <div id="page">URL: [<?= $url ?>]</div>
        <div id="title">Title: [<?= $title ?>]</div>
    </body>
</html>



回答2:


This is how you can do it with JavaScript and the V3 YouTube Data API.

var ytApiKey = "...";
var videoId = "ylLzyHk54Z0";

$.get("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + videoId + "&key=" + ytApiKey, function(data) {
  alert(data.items[0].snippet.title);
});



回答3:


Call http://gdata.youtube.com/feeds/api/videos/ylLzyHk54Z0.

In this XML file, read the value of the <title> tag.

YouTube Api Documentation




回答4:


You can use a JSON request to: http://gdata.youtube.com/feeds/api/videos/ylLzyHk54Z0?v=2&alt=jsonc




回答5:


This answer is accurate as of December 2015.

To get the video title from an YouTube video id, you will have to construct the following URL, using YouTube Data API (you are required to use an API key, otherwise the request will fail):

https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id%2Csnippet)&key={YOUR_API_KEY}

Do a GET request and you will get a JSON response similar to the chunk below. For the title, it exists in the snippet/title key.

{
   "items":[
      {
         "id":"Jglv0A0qLI8",
         "snippet":{
            "publishedAt":"2014-06-30T03:42:20.000Z",
            "channelId":"UCdTU5vd37FlTZ-xoB0xzRDA",
            "title":"AIA Malaysia - A-Plus Venus Plan - Comprehensive Female Protection and Insurance Plan",
            "description":"A comprehensive female protection plan for the modern women\n\nFor more information visit: http://www.aia.com.my/en/individuals/products-and-services/health/a-plus-venus-a-plus-venus-extra.html\n\nFor more products, visit AIA Malaysia's Products and Services playlist:\nhttps://www.youtube.com/playlist?list=PLSrgVT3aQ1fZ3SCe-dEVnFJDApBYkqolP\n\nFor more videos. subscribe to AIA Malaysia's YouTube channel:\nhttps://www.youtube.com/channel/UCdTU5vd37FlTZ-xoB0xzRDA",
            "thumbnails":{
               "default":{
                  "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/default.jpg",
                  "width":120,
                  "height":90
               },
               "medium":{
                  "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/mqdefault.jpg",
                  "width":320,
                  "height":180
               },
               "high":{
                  "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/hqdefault.jpg",
                  "width":480,
                  "height":360
               },
               "standard":{
                  "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/sddefault.jpg",
                  "width":640,
                  "height":480
               },
               "maxres":{
                  "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/maxresdefault.jpg",
                  "width":1280,
                  "height":720
               }
            },
            "channelTitle":"AIA Malaysia",
            "tags":[
               "aia",
               "aia malaysia",
               "a-plus venus",
               "female health insurance",
               "female life insurance",
               "female insurance",
               "female medical insurance"
            ],
            "categoryId":"27",
            "liveBroadcastContent":"none",
            "localized":{
               "title":"AIA Malaysia - A-Plus Venus Plan - Comprehensive Female Protection and Insurance Plan",
               "description":"A comprehensive female protection plan for the modern women\n\nFor more information visit: http://www.aia.com.my/en/individuals/products-and-services/health/a-plus-venus-a-plus-venus-extra.html\n\nFor more products, visit AIA Malaysia's Products and Services playlist:\nhttps://www.youtube.com/playlist?list=PLSrgVT3aQ1fZ3SCe-dEVnFJDApBYkqolP\n\nFor more videos. subscribe to AIA Malaysia's YouTube channel:\nhttps://www.youtube.com/channel/UCdTU5vd37FlTZ-xoB0xzRDA"
            }
         }
      }
   ]
}

For more information, visit the API documentation page.




回答6:


The video title is in the API and reachable in JavaScript using dot notation:

the_name_of_your_video_object.A.videoData.title



回答7:


The answers of Robert Sim and cbaigorri were the best, that's the correct way to do it at this time with JS, do GET request to:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id,snippet)&key={YOUR_API_KEY}

A little specification about this: You can use comma separated youtube video IDs to get multiple videos info in one request.

To get 1 video, replace {YOUTUBE_VIDEO_ID} with video ID (ex: 123456) To get more videos in one request, replace {YOUTUBE_VIDEO_ID} with comma separated IDs (ex: 123456,234567,345678,456789)

This will count as a single request in the Quotas, this way you can get a lot of video details with only 1 quota/request cost.




回答8:


My solution is:

$xmlInfoVideo    = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/".$videoId."?v=2&fields=title");

foreach($xmlInfoVideo->children() as $title) { $videoTitle = strtoupper((string) $title); }

This get the title of the video.




回答9:


Instead of using http://gdata.youtube.com/feeds/api/videos/....

If you have the video loaded, you can use the player object's getVideoData() method to retrieve information on the video, including the title. It will return a object which contains: video_id, author, title.



来源:https://stackoverflow.com/questions/1760231/how-do-i-get-the-title-of-a-youtube-video-if-i-have-the-video-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!