这个 YouTube MCP 服务器是一个基于模型上下文协议(MCP)的工具,它能与 YouTube 数据 API 和字幕抓取器进行交互,可通过统一接口访问视频信息和字幕数据,为用户提供便捷的数据获取体验。
此服务器基于模型上下文协议(MCP),可与 YouTube 数据 API 和字幕抓取器交互,为您提供统一接口来访问视频信息和字幕数据。接下来,您可以按照以下步骤进行操作。
npm install youtube-mcp-server
git clone https://github.com/your-repository.git
cd your-repository
npm install
npm start
const { MCP } = require('youtube-mcp-server');
const mcp = new MCP({
apiKey: 'your_api_key', // 替换为你的 YouTube API 密钥
models: ['gpt-3.5-turbo'] // 支持的模型列表
});
async function getVideoInfo(videoId) {
try {
const result = await mcp.query({
type: 'query',
query: {
video: {
id: videoId
}
}
});
return result;
} catch (error) {
console.error('获取视频信息失败:', error.message);
return null;
}
}
async function extractSubtitles(videoId, language = 'zh-CN') {
try {
const result = await mcp.query({
type: 'extract',
extractor: {
youtube: {
videoId,
language
}
}
});
return result;
} catch (error) {
console.error('提取字幕失败:', error.message);
return null;
}
}
const videoInfo = await getVideoInfo('dQw4w9WgXcQ');
console.log(videoInfo);
// 输出示例:
// {
// id: 'dQw4w9WgXcQ',
// title: '什么是什么?', // 示例标题
// channel: 'PewDiePro', // 示例频道名称
// views: '100万', // 示例观看次数
// likes: '10万' // 示例点赞数
// }
const subtitles = await extractSubtitles('dQw4w9WgXcQ');
console.log(subtitles);
// 输出示例:
// [
// {
// timestamp: '00:01:23', // 示例时间戳
// text: '这是第一个字幕条。' // 示例字幕文本
// },
// ...
// ]
该服务器处理多种错误情况,包括:
本项目采用 MIT 许可协议。
感谢以下项目和库的支持: