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

后端 未结 9 1985
忘掉有多难
忘掉有多难 2020-12-13 00:28

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: y

相关标签:
9条回答
  • 2020-12-13 00:57

    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.

    0 讨论(0)
  • 2020-12-13 00:58

    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.

    0 讨论(0)
  • 2020-12-13 00:59

    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>
    
    0 讨论(0)
  • 2020-12-13 01:07

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

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

    YouTube Api Documentation

    0 讨论(0)
  • 2020-12-13 01:09

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

    0 讨论(0)
  • 2020-12-13 01:10

    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);
    });
    
    0 讨论(0)
提交回复
热议问题