You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Bash脚本配合crontab更新YouTube直播的标题与描述?

YouTube直播信息定时自动更新实现指南

第一步:前置准备工作

  • 在Google Cloud控制台开通YouTube Data API v3,创建OAuth 2.0客户端ID,授权时选择https://www.googleapis.com/auth/youtube.force-ssl权限域,否则无权限修改直播信息
  • 提前获取需要修改的直播广播ID,即直播页面URL中v=后的字符串,也可调用liveBroadcasts.list接口批量查询未开播的直播ID列表

第二步:核心接口调用规则

调用liveBroadcasts.update接口需注意两个必填参数要求:

  • part参数固定传入snippet,status,标题、描述存储在snippet字段中,status为接口要求必填参数,缺少会返回报错
  • 请求体必须传完整的snippet结构,不能仅传递需要修改的字段,否则原有字段会被清空,基础请求结构参考:
{
  "id": "你的直播广播ID",
  "snippet": {
    "title": "新的直播标题",
    "description": "新的直播描述",
    "scheduledStartTime": "原有的开播时间,格式为RFC3339",
    // 其他原有snippet字段原样保留,不要删除
  },
  "status": {
    "privacyStatus": "原有的隐私状态,可选值public/unlisted/private"
  }
}

第三步:编写可定时执行的Python脚本

先安装依赖包:pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib,示例脚本如下:

import os
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# 固定权限域,不要修改
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
# 单场直播直接填ID,多场可自行扩展逻辑从配置表读取
BROADCAST_ID = "替换为你的直播广播ID"

def get_credentials():
    creds = None
    # 首次运行生成token.json存储授权凭证,后续定时执行无需重复手动授权
    if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                "client_secret.json", SCOPES
            )
            creds = flow.run_local_server(port=0)
        with open("token.json", "w") as token:
            token.write(creds.to_json())
    return creds

def update_live_info(new_title, new_desc):
    creds = get_credentials()
    youtube = build("youtube", "v3", credentials=creds)
    # 先拉取原有直播完整信息,避免覆盖未修改字段
    broadcast_response = youtube.liveBroadcasts().list(
        part="snippet,status",
        id=BROADCAST_ID
    ).execute()
    if not broadcast_response["items"]:
        print("未找到对应直播")
        return
    broadcast = broadcast_response["items"][0]
    # 仅更新需要修改的字段
    broadcast["snippet"]["title"] = new_title
    broadcast["snippet"]["description"] = new_desc
    # 提交更新请求
    update_response = youtube.liveBroadcasts().update(
        part="snippet,status",
        body=broadcast
    ).execute()
    print(f"更新成功,新标题:{update_response['snippet']['title']}")

if __name__ == "__main__":
    # 可自行扩展为从配置文件/数据库读取对应直播的标题描述
    update_live_info("本周技术干货直播专场", "今晚8点开播,全程答疑,抽送学习资源大礼包")
  • 首次运行脚本时需手动在弹出的浏览器页面登录对应YouTube频道账号完成授权,生成的token.json长期有效,无需重复授权

第四步:配置crontab定时任务

  • 先手动运行脚本确认功能正常,记录脚本的绝对路径和Python解释器的绝对路径(可执行which python3查询)
  • 执行crontab -e编辑定时任务,比如每周五晚7点自动更新周末直播信息,添加如下配置:
0 19 * * 5 /usr/bin/python3 /opt/youtube_update_live.py >> /var/log/youtube_live_update.log 2>&1
  • 末尾追加日志输出可方便排查执行失败问题

注意事项

  • 单账号每日API调用配额为10000点,单次update调用消耗50点,每周更新几十场直播配额完全充足
  • 多场直播场景可把直播ID、更新时间、对应标题描述存入配置表,脚本循环读取执行即可
  • 直播已开播状态下也可调用接口更新标题描述,无状态限制

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 03:18:02