首页 > 解决方案 > 如何为 YouTube 直播添加字幕?

问题描述

YouTube 提供了在直播中发送字幕的可能性,如此所述。但是,本指南引用了来自 Youtube Studio Classic 的链接,该链接已不存在。在新的直播控制室中,我只能找到一个字幕链接,看起来像

http://upload.youtube.com/closedcaption?cid=....

并且不包含类似nsor的参数sparams

如何为直播控制室提供字幕?其他页面上也有一些误导性信息 - 我可以只使用简单的 HTTP POST 还是需要购买一个受支持的软件

如果无法使用 POST,我可以使用 Livestreaming API 吗?

标签: httppostyoutubelive-streamingyoutube-livestreaming-api

解决方案


它适用于我使用下面的 python 代码。

import time
import requests
from datetime  import datetime

word = "this is caption " + str(seq)

ytlink = "http://upload.youtube.com/closedcaption?cid=xxx-xxx-xxx-xxx="+str(seq)

post_fields = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + ' \n' + word + '\n' 

headers = {'content-type': 'text/plain'}
r = requests.post(url=ytlink, data=post_fields.encode('utf-8'), headers=headers)
 
print(ytlink)
print(r.text)

基本上,其他人将不再需要。


推荐阅读