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

如何通过API将YouTube直播closedCaptionsType设为自动字幕?

如何通过YouTube Data API设置直播的自动字幕(仅英语)

我想通过Python调用YouTube API创建直播时,将字幕源设置为YouTube Studio直播页面下拉菜单中的「自动字幕(仅英语)」选项。已知原参数enableClosedCaptions已被closedCaptionsType替代,设置closedCaptionsType为closedCaptionsHttpPost对应下拉菜单的「向URL提交字幕」选项。我的目标是使用YouTube官方提供的自动字幕,而非第三方或自研字幕系统,但在公开的API文档中找不到对应的选项。

API文档中contentDetails.closedCaptionsType的定义如下:

contentDetails.closedCaptionsType(字符串)
注意:该属性替代了contentDetails.enableClosedCaptions属性。
该属性表示直播是否启用字幕,以及启用的字幕类型:

  • closedCaptionsDisabled:直播禁用字幕。
  • closedCaptionsHttpPost:通过HTTP POST将字幕发送到直播关联的摄入URL。
  • closedCaptionsEmbedded:字幕以EIA-608和/或CEA-708格式编码到视频流中。

我使用以下Python代码创建直播可成功执行,但字幕类型被设置为「向URL提交字幕」。在Chrome浏览器的JavaScript控制台中,观察到UI设置字幕类型的请求负载包含CAPTIONS_TYPE_ASR和CAPTIONS_TYPE_HTTP_POST两个值,但无法找到API中对应的等效字符串。

broadcast_response = youtube.liveBroadcasts().insert(
    part='snippet,status,contentDetails',
    body={
        'snippet': {
            'title': stream_title,
            'description': stream_description,
            'scheduledStartTime': stream_schedStart,
            'categoryId': category_id,
            'defaultLanguage': 'en-US',
            'defaultAudioLanguage': 'en-US'
        },
        'status': {
            'privacyStatus': 'public',
            'selfDeclaredMadeForKids': False
        },
        'contentDetails': {
            'monitorStream': {
                'enableMonitorStream': True,
                'broadcastStreamDelayMs': 0
            },
            'enableAutoStart': True,
            'enableAutoStop': False,
            'enableDvr': True,
            # 'enableClosedCaptions': True,
            'closedCaptionsType': 'closedCaptionsHttpPost',
            'enableEmbed': stream_embed,
            'enableLowLatency': False,
            'latencyPreference': 'normal',
            'projection': 'rectangular',
            'recordFromStart': True
        }
    }
).execute()

解决方案

YouTube Data API的公开文档未直接列出对应自动字幕的closedCaptionsType枚举值,但实际调用中可使用closedCaptionsAuto来启用YouTube的自动语音识别(ASR)字幕。

需注意:该值属于未正式文档化的参数,后续API版本迭代可能存在变更风险,当前版本下可正常生效。

修改后的代码如下:

broadcast_response = youtube.liveBroadcasts().insert(
    part='snippet,status,contentDetails',
    body={
        'snippet': {
            'title': stream_title,
            'description': stream_description,
            'scheduledStartTime': stream_schedStart,
            'categoryId': category_id,
            'defaultLanguage': 'en-US',
            'defaultAudioLanguage': 'en-US'
        },
        'status': {
            'privacyStatus': 'public',
            'selfDeclaredMadeForKids': False
        },
        'contentDetails': {
            'monitorStream': {
                'enableMonitorStream': True,
                'broadcastStreamDelayMs': 0
            },
            'enableAutoStart': True,
            'enableAutoStop': False,
            'enableDvr': True,
            # 设置为YouTube自动字幕类型
            'closedCaptionsType': 'closedCaptionsAuto',
            'enableEmbed': stream_embed,
            'enableLowLatency': False,
            'latencyPreference': 'normal',
            'projection': 'rectangular',
            'recordFromStart': True
        }
    }
).execute()

同时确保直播的defaultLanguage和defaultAudioLanguage设置为英语(如en-US),以保证自动字幕功能正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 02:35:05