无需 YouTube API 即可获取 YouTube 元数据
youtube-metadata-swift
是一个 Swift 包和命令行工具,用于获取 YouTube 视频的元数据。
要在您的 Swift 项目中使用 youtube-metadata-swift
作为依赖项,请将以下内容添加到您的 Package.swift
中
dependencies: [
.package(url: "https://github.com/ezefranca/youtube-metadata-swift.git", from: "1.0.0")
],
targets: [
.target(
name: "YourTarget",
dependencies: ["YTMetadataRequesterLib"]),
]
要安装命令行工具,您可以使用提供的 install.sh
脚本
克隆存储库
git clone https://github.com/ezefranca/youtube-metadata-swift.git
cd youtube-metadata-swift
运行安装脚本
./install.sh
这将构建该工具并将其复制到 /usr/local/bin
。
要获取 YouTube 视频的元数据,请运行
ytmeta <YouTube URL>
func getMetadata() async {
let vid = "https://youtu.be/TRqiFPpw2fY"
let req = YTMetadataRequester()
do {
let metadata = try await req.getMetadata(for: vid)
print("Title: \(metadata.title)")
} catch {
print("Failed to fetch metadata: \(error.localizedDescription)")
}
}
{
title: 'Foo Fighters - The Sky Is A Neighborhood (Official Music Video)',
author_name: 'foofightersVEVO',
author_url: 'https://www.youtube.com/user/foofightersVEVO',
type: 'video',
height: 113,
width: 200,
version: '1.0',
provider_name: 'YouTube',
provider_url: 'https://www.youtube.com/',
thumbnail_height: 360,
thumbnail_width: 480,
thumbnail_url: 'https://i.ytimg.com/vi/TRqiFPpw2fY/hqdefault.jpg',
html: '<iframe width="200" height="113" src="https://www.youtube.com/embed/TRqiFPpw2fY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
}
// MARK: -Metadata
struct Metadata: Codable {
let authorName: String
let authorURL: String
let height: Int
let html, providerName: String
let providerURL: String
let thumbnailHeight: Int
let thumbnailURL: String
let thumbnailWidth: Int
let title, type, version: String
let width: Int
}
本项目仅供教育目的提供。它不隶属于 Youtube,也未经 Youtube 批准。