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

如何通过YouTube Data API v3为视频上传缩略图(Python)

解决YouTube Data API上传视频缩略图的问题

你的代码已实现视频上传,但未完成缩略图上传的核心逻辑,且存在权限范围不足的问题,以下是可行的修复方案:

核心问题分析

  1. OAuth权限不足:当前仅使用youtube.upload权限,该权限仅支持视频上传,无法修改视频缩略图
  2. 未调用缩略图专属API:YouTube Data API需要通过thumbnails.set接口单独上传缩略图,而非和视频一起提交

具体修复步骤

1. 调整OAuth权限范围

替换原有的权限范围,使用支持缩略图修改的权限:

YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"

注意:权限变更后,需删除本地生成的%s-oauth2.json授权文件,重新完成OAuth授权流程。

2. 添加缩略图上传函数

新增处理缩略图上传的函数,复用已有的重试逻辑:

def upload_thumbnail(youtube, video_id, image_path):
    if not os.path.exists(image_path):
        print("未找到缩略图文件:", image_path)
        return

    media_body = MediaFileUpload(image_path, chunksize=-1, resumable=True)
    request = youtube.thumbnails().set(
        videoId=video_id,
        media_body=media_body
    )

    response = None
    error = None
    retry = 0
    while response is None:
        try:
            print("正在上传缩略图...")
            status, response = request.next_chunk()
            if response is not None:
                print("缩略图上传成功")
        except HttpError as e:
            if e.resp.status in RETRIABLE_STATUS_CODES:
                error = "可重试HTTP错误 %d:\n%s" % (e.resp.status, e.content)
            else:
                raise
        except RETRIABLE_EXCEPTIONS as e:
            error = "可重试错误: %s" % e

        if error is not None:
            print(error)
            retry += 1
            if retry > MAX_RETRIES:
                exit("已超出最大重试次数,终止缩略图上传")

            max_sleep = 2 ** retry
            sleep_seconds = random.random() * max_sleep
            print("等待 %f 秒后重试..." % sleep_seconds)
            time.sleep(sleep_seconds)

3. 在视频上传成功后触发缩略图上传

修改resumable_upload函数,获取视频ID后立即调用缩略图上传:

def resumable_upload(youtube, insert_request):
    response = None
    error = None
    retry = 0
    while response is None:
        try:
            print("正在上传视频文件...")
            status, response = insert_request.next_chunk()
            if response is not None:
                if 'id' in response:
                    video_id = response['id']
                    print("视频上传成功,ID: '%s'" % video_id)
                    # 调用缩略图上传
                    upload_thumbnail(youtube, video_id, image_path)
                else:
                    exit("视频上传失败,返回异常响应: %s" % response)
        except HttpError as e:
            if e.resp.status in RETRIABLE_STATUS_CODES:
                error = "可重试HTTP错误 %d:\n%s" % (e.resp.status, e.content)
            else:
                raise
        except RETRIABLE_EXCEPTIONS as e:
            error = "可重试错误: %s" % e

        if error is not None:
            print(error)
            retry += 1
            if retry > MAX_RETRIES:
                exit("已超出最大重试次数,终止视频上传")

            max_sleep = 2 ** retry
            sleep_seconds = random.random() * max_sleep
            print("等待 %f 秒后重试..." % sleep_seconds)
            time.sleep(sleep_seconds)

注意:同时需要修改initialize_upload中调用resumable_upload的代码,传入youtube实例:

resumable_upload(youtube, insert_request)

4. 缩略图格式要求

确保缩略图符合YouTube规范:

  • 分辨率不低于1280x720像素
  • 格式为JPG或PNG
  • 文件大小不超过2MB

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 14:55:02