带有 API v3 的 Youtube 视频标题没有 API 密钥?

Posted

技术标签:

【中文标题】带有 API v3 的 Youtube 视频标题没有 API 密钥?【英文标题】:Youtube Video title with API v3 without API key? 【发布时间】:2015-07-17 00:38:42 【问题描述】:

是否可以使用带有 API v3 没有 API 密钥的视频 ID 获取视频标题?我在 API 文档中找不到任何获取标题的信息或示例。

【问题讨论】:

【参考方案1】:

无需 API 密钥

要获取视频标题,您将不需要需要 API 密钥,并且您需要向以下人员发出请求:

https://noembed.com/embed?url=https://www.youtube.com/watch?v=dQw4w9WgXcQ

更改所需视频的 YouTube URL。

它也适用于 Vimeo 和许多其他 supported sites,其 URL 如下:

https://noembed.com/embed?url=https://vimeo.com/45196609

详情

如果您直接使用 API,则无法在没有 API 密钥的情况下使用 API v3 的视频 ID 获取视频标题。 YouTube 数据 API v2 已弃用(请参阅:YouTube Data API v2 Deprecation: Frequently Asked Questions),目前 YouTube API 不支持 oEmbed 和 JSONP(请参阅 Issue 4329: oEmbed callback for JSONP)。

但幸运的是,Noembed 服务可让您使用 JSONP 获取 YouTube 视频的标题(和其他数据),而无需 API 密钥。

演示

下面是一个简单的用jQuery获取标题的demo:

var id = 'dQw4w9WgXcQ';
var url = 'https://www.youtube.com/watch?v=' + id;

$.getJSON('https://noembed.com/embed',
    format: 'json', url: url, function (data) 
    alert(data.title);
);

在 JS Bin 上查看 DEMO

另请参阅以下问题:

Get Youtube information via JSON for single video (not feed) in javascript Response for JSONp request to youtube oembed call giving "invalid label" error

【讨论】:

这很棒。还有没有办法进行搜索?【参考方案2】:

要获取视频标题,您需要一个 API 密钥,并且您需要发出以下请求:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=COMMA_DELIMITED_LIST_OF_IDS&key=YOUR_API_KEY

在返回的数据包中,标题为items.snippet.title

如果没有 API 密钥,您将无法检索 任何 API 数据;所有 API 请求都在配额系统上,密钥用于确定对应用的每日限额收取多少费用。然而,像上面这样的电话是相当便宜的。它总共有 3 个单位(2 个用于 sn-p,1 个用于请求本身)。由于您每天获得 50,000,000 个单位,并且可以为同样的 3 个单位检索多达 50 个 sn-ps,因此使用 API 密钥并没有太大的负担。

【讨论】:

很好,视频长度字段从 API v3 中消失了。为什么他们必须打破这些谷歌人完美运行的东西? >:( 它并没有消失,它只是在一个不同的“部分”——主要是为了让不需要持续时间的人不会为配额付费(V3 更加模块化,而 V2只是将巨大的 XML 文件返回给所有东西,结果非常浪费)。视频时长在 contentDetails 部分,所以像这样的请求:googleapis.com/youtube/v3/videos?part=contentDetails&id=VIDEO_ID&key=YOUR_API_KEY 将允许您访问 items.contentDetails.duration 参数。请注意,该值采用 ISO-8601-Duration 格式,因此您可能需要对其进行转换。 这个答案已经过时了,看这个***.com/a/48884290/5304495【参考方案3】:

这正是oEmbed协议的含义。

oEmbed 是一种允许在 第三方网站。简单的 API 允许网站显示嵌入的 当用户发布指向该内容的链接时(例如照片或视频) 资源,无需直接解析资源。

此文档存储在GitHub。

有关格式的更多信息:https://oembed.com

https://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json

示例输出:


    "version": "1.0",
    "type": "video",
    "provider_name": "YouTube",
    "provider_url": "http://youtube.com/",
    "width": 425,
    "height": 344,
    "title": "Amazing Nintendo Facts",
    "author_name": "ZackScott",
    "author_url": "http://www.youtube.com/user/ZackScott",
    "html":
        "<object width=\"425\" height=\"344\">
            <param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
            <param name=\"allowFullScreen\" value=\"true\"></param>
            <param name=\"allowscriptaccess\" value=\"always\"></param>
            <embed src=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"
                type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
                allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>
        </object>",

以下是声明遵守该格式的内容提供商列表:

    "provider_name": "23HQ",
    "provider_name": "Adways",
    "provider_name": "Alpha App Net",
    "provider_name": "amCharts Live Editor",
    "provider_name": "Animatron",
    "provider_name": "Animoto",
    "provider_name": "Audiomack",
    "provider_name": "Audiosnaps",
    "provider_name": "Blackfire.io",
    "provider_name": "Box Office Buz",
    "provider_name": "Buttondown",
    "provider_name": "Cacoo",
    "provider_name": "Carbon Health",
    "provider_name": "CatBoat",
    "provider_name": "ChartBlocks",
    "provider_name": "chirbit.com",
    "provider_name": "CircuitLab",
    "provider_name": "Clipland",
    "provider_name": "Clyp",
    "provider_name": "Codepen",
    "provider_name": "Codepoints",
    "provider_name": "CodeSandbox",
    "provider_name": "CollegeHumor",
    "provider_name": "Commaful",
    "provider_name": "Coub",
    "provider_name": "Crowd Ranking",
    "provider_name": "Cyrano Systems",
    "provider_name": "Daily Mile",
    "provider_name": "Dailymotion",
    "provider_name": "Deviantart.com",
    "provider_name": "Didacte",
    "provider_name": "Dipity",
    "provider_name": "DocDroid",
    "provider_name": "Docs",
    "provider_name": "Dotsub",
    "provider_name": "edocr",
    "provider_name": "eduMedia",
    "provider_name": "EgliseInfo",
    "provider_name": "Embed Articles",
    "provider_name": "Embedly",
    "provider_name": "Ethfiddle",
    "provider_name": "Eyrie",
    "provider_name": "Facebook (Video)",
    "provider_name": "Flat",
    "provider_name": "Flickr",
    "provider_name": "FOX SPORTS Australia",
    "provider_name": "FrameBuzz",
    "provider_name": "FunnyOrDie",
    "provider_name": "Geograph Britain and Ireland",
    "provider_name": "Geograph Channel Islands",
    "provider_name": "Geograph Germany",
    "provider_name": "Getty Images",
    "provider_name": "Gfycat",
    "provider_name": "GIPHY",
    "provider_name": "Gyazo",
    "provider_name": "HuffDuffer",
    "provider_name": "Hulu",
    "provider_name": "iFixit",
    "provider_name": "IFTTT",
    "provider_name": "Indaco",
    "provider_name": "Infogram",
    "provider_name": "Inoreader",
    "provider_name": "inphood",
    "provider_name": "Instagram",
    "provider_name": "iSnare Articles",
    "provider_name": "ivlismusic",
    "provider_name": "Kickstarter",
    "provider_name": "Kidoju",
    "provider_name": "Kit",
    "provider_name": "Kitchenbowl",
    "provider_name": "Knacki",
    "provider_name": "LearningApps.org",
    "provider_name": "Ludus",
    "provider_name": "MathEmbed",
    "provider_name": "me.me",
    "provider_name": "Meetup",
    "provider_name": "MixCloud",
    "provider_name": "Moby Picture",
    "provider_name": "Modelo",
    "provider_name": "myBeweeg",
    "provider_name": "nanoo.tv",
    "provider_name": "nfb.ca",
    "provider_name": "Odds.com.au",
    "provider_name": "Office Mix",
    "provider_name": "Official FM",
    "provider_name": "On Aol",
    "provider_name": "Ora TV",
    "provider_name": "Orbitvu",
    "provider_name": "Oumy",
    "provider_name": "Pastery",
    "provider_name": "Pixdor",
    "provider_name": "Poll Daddy",
    "provider_name": "Port",
    "provider_name": "Portfolium",
    "provider_name": "Punters",
    "provider_name": "Quiz.biz",
    "provider_name": "Quizz.biz",
    "provider_name": "RapidEngage",
    "provider_name": "Reddit",
    "provider_name": "ReleaseWire",
    "provider_name": "RepubHub",
    "provider_name": "ReverbNation",
    "provider_name": "Roomshare",
    "provider_name": "Rumble",
    "provider_name": "Sapo Videos",
    "provider_name": "Screen9",
    "provider_name": "Screencast.com",
    "provider_name": "Screenr",
    "provider_name": "ScribbleMaps",
    "provider_name": "Scribd",
    "provider_name": "ShortNote",
    "provider_name": "Shoudio",
    "provider_name": "Show the Way, actionable location info",
    "provider_name": "Silk",
    "provider_name": "Simplecast",
    "provider_name": "Sizzle",
    "provider_name": "Sketchfab",
    "provider_name": "SlideShare",
    "provider_name": "SmugMug",
    "provider_name": "SocialExplorer",
    "provider_name": "Songlink",
    "provider_name": "SoundCloud",
    "provider_name": "Soundsgood",
    "provider_name": "SpeakerDeck",
    "provider_name": "Spotful",
    "provider_name": "Spreaker",
    "provider_name": "Streamable",
    "provider_name": "StreamOneCloud",
    "provider_name": "Sutori",
    "provider_name": "Sway",
    "provider_name": "Ted",
    "provider_name": "The New York Times",
    "provider_name": "They Said So",
    "provider_name": "TickCounter",
    "provider_name": "Toornament",
    "provider_name": "Topy",
    "provider_name": "Twitch",
    "provider_name": "Twitter",
    "provider_name": "Ubideo",
    "provider_name": "UOL",
    "provider_name": "Ustream",
    "provider_name": "Utposts",
    "provider_name": "Uttles",
    "provider_name": "VeeR VR",
    "provider_name": "Verse",
    "provider_name": "VEVO",
    "provider_name": "VideoJug",
    "provider_name": "Vidlit",
    "provider_name": "Vimeo",
    "provider_name": "Vlipsy",
    "provider_name": "wecandeo",
    "provider_name": "Wiredrive",
    "provider_name": "wizer.me",
    "provider_name": "Wootled",
    "provider_name": "WordPress.com",
    "provider_name": "Yes, I Know IT!",
    "provider_name": "YFrog",
    "provider_name": "YouTube",
    "provider_name": "ZProvider",

请注意,这并不意味着在此网站上启用了 CORS

这些数据主要用于服务器端解析。

大部分信息也可以通过解析给定页面的&lt;meta og&gt; open graph html 属性来获得。

请参阅http://ogp.me/ 以获取有关打开图形的信息。

【讨论】:

这是一个很好的答案,它不需要 api 密钥,也不依赖第三方服务。【参考方案4】:

是的,您无需任何 API 即可,只需 Requestsre 模块

Python 中的代码:

import requests,re

video_url = "https://www.youtube.com/watch?v=2i2khp_npdE"

response = requests.get(video_url).text

title = re.findall(r'"title":"[^>]*",',response)[0].split(',')[0][9:-1]

print("\n[+] Video Title: ".format(title))

输出:

[+] 视频标题:Alan Walker - 让我入睡

【讨论】:

【参考方案5】:

是的,有可能。

我对@9​​87654321@做了一些修改,并在Python上写了这个sn-p:

import urllib, urllib.request, json

input = "C:\\urls.txt"
output = "C:\\tracks.csv"

urls=[line.strip() for line in open(input)]
for url in urls:
    ID = url.split('=')
    VideoID = ID[1]
    params = "format": "json", "url": "https://www.youtube.com/watch?v=%s" % VideoID
    url = "https://www.youtube.com/oembed"
    query_string = urllib.parse.urlencode(params)
    url = url + "?" + query_string
    with urllib.request.urlopen(url) as response:
        response_text = response.read()
        try:
            data = json.loads(response_text.decode())
        except ValueError as e:
            continue # skip faulty url
        if data is not None:
            author = data['author_name'].split(' - ')
            author = author[0].rstrip()
            f = open(output, "a", encoding='utf-8')
            print(author, ',', data['title'], sep="", file=f)

它会选择一个包含 Youtube URL 列表的文本文件:

https://www.youtube.com/watch?v=F_Vfgdfgg
https://www.youtube.com/watch?v=RndfgdfN8
...

并返回一个包含艺术家-标题对的 CSV 文件:

Beyonce,Pretty hurts
Justin Timberlake,Cry me a river

附: sn-p 中的文件路径格式适用于 Windows,对于 Linux,只需使用 ~/tracks.csv

【讨论】:

抱歉,@Suncatcher,但如果某些标题包含逗号怎么办? 刚刚尝试使用逗号标题,一切正常。在编写 CSV 时不应该有问题,在解析生成的 CSV 时也不应该有问题,您可以将标题括在引号中,例如 print(author, ",'", data['title'], "'", "sep="", file=f) 或应用您想要的任何规则

以上是关于带有 API v3 的 Youtube 视频标题没有 API 密钥?的主要内容,如果未能解决你的问题,请参考以下文章

Youtube Data API V3 Javascript - 将音频和图像上传为视频

翻译 YouTube 视频标题和说明 | V3 API

Youtube /v3/search API 不再返回直播视频

Youtube API v3:使用服务帐户访问 youtube 视频

YouTube API V3 - 为新提要获取推荐视频

如何使用 php 使用 Youtube Api v3 喜欢 Youtube 视频