获取嵌入式 YouTube 视频的标题和描述 [英] Getting title and description of embedded YouTube video

查看:31
本文介绍了获取嵌入式 YouTube 视频的标题和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的网站上,我嵌入了来自 YouTube 的视频并希望获得视频标题及其说明.

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

我如何获得这些信息?

推荐答案

要获得 DESCRIPTION 元素,您需要访问视频信息的 gdata 版本,您可以使用 alt= 返回 jsonjson 在路径上.在这种情况下,oHg5SJYRHA0 是视频 ID,位于您在 YouTube 上使用的视频网址的末尾,例如www.youtube.com/watch?v=oHg5SJYRHA0

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

(prettyprint 的格式是为了便于阅读,你在做的事情不需要它)

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

您可以获取 JSON,将其添加到变量中并使用 jQuery 访问它:

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");

这篇关于获取嵌入式 YouTube 视频的标题和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆