如何批量更新YouTube视频描述?能否借助谷歌API自动生成?
批量更新YouTube视频描述实现方案
完全可以通过YouTube Data API v3实现批量更新,适配你现有的PHP+jQuery技术栈,不需要手动修改750条视频内容。
前期准备
- 前往Google Cloud Console启用YouTube Data API v3,创建OAuth 2.0凭证,申请
youtube.upload和youtube.readonly两个权限,使用你对应YouTube频道的谷歌账号完成授权,确保获得视频编辑权限。 - 不需要手动整理视频ID,可直接通过API拉取你频道下所有视频的ID、现有描述、标题等基础信息。
核心实现步骤
1. 安装依赖
使用Composer安装官方PHP客户端:composer require google/apiclient:^2.0
2. 拉取频道全量视频数据
require __DIR__ . '/vendor/autoload.php'; $client = new Google_Client(); $client->setClientId('你的Google Cloud Client ID'); $client->setClientSecret('你的Google Cloud Client Secret'); $client->setScopes('https://www.googleapis.com/auth/youtube'); $client->setAccessToken('OAuth授权后获取的访问令牌'); $youtube = new Google_Service_YouTube($client); $videos = []; $nextPageToken = ''; // 分页拉取所有视频,单页最大支持50条 do { $searchResponse = $youtube->search->listSearch('id,snippet', [ 'forMine' => true, 'type' => 'video', 'maxResults' => 50, 'pageToken' => $nextPageToken ]); foreach ($searchResponse['items'] as $item) { $videos[] = [ 'id' => $item['id']['videoId'], 'old_desc' => $item['snippet']['description'], 'title' => $item['snippet']['title'], 'categoryId' => $item['snippet']['categoryId'] ]; } $nextPageToken = $searchResponse['nextPageToken']; } while ($nextPageToken);
3. 拼接新描述批量更新
按照你提供的模板生成对应视频的专属购买链接,按需拼接描述后调用更新接口:
// 固定尾部内容 $fixedContent = " Facebook: fb.com/xxx Pinterest: pinterest.com/xxx don't forget to subscribe and like... "; foreach ($videos as $video) { $videoId = $video['id']; // 生成对应视频的购买链接 $buyLink = "Buy the pdf file: example.com/?v={$videoId}"; // 新描述拼接规则可自行调整,这里示例为购买链接+固定尾部+原有描述 $newDesc = $buyLink . PHP_EOL . $fixedContent . PHP_EOL . $video['old_desc']; // 构建更新参数 $videoSnippet = new Google_Service_YouTube_VideoSnippet(); $videoSnippet->setDescription($newDesc); // 原有标题、分类必须保留,否则会被清空 $videoSnippet->setTitle($video['title']); $videoSnippet->setCategoryId($video['categoryId']); $updateVideo = new Google_Service_YouTube_Video(); $updateVideo->setId($videoId); $updateVideo->setSnippet($videoSnippet); // 执行更新 $youtube->videos->update('snippet', $updateVideo); // 增加1秒延迟避免触发API限流 sleep(1); }
注意事项
- YouTube Data API默认每日配额为10000单位,单次视频更新操作消耗50单位,750条视频总计需要37500单位,默认配额不够的话可以直接在Google Cloud Console提交配额提升申请,如实填写使用场景即可,一般24小时内会审批通过。
- 正式全量执行前,建议先选取1-2条测试视频验证描述格式、链接正确性,避免批量操作出错。
- 如果你需要可视化操作界面,前端可以直接用jQuery调用封装好的PHP接口即可,完全适配你的现有技术栈。
内容的提问来源于stack exchange,提问作者kevinmcurran
相关产品推荐
相关产品推荐

