gpt4 book ai didi

node.js - Youtube API无法同时创建直播和直播的两个绑定(bind)(1对1)

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:46 25 4
gpt4 key购买 nike

首先,我将解释如何使用 Youtube API 创建实时流媒体,然后,我将解释用例,第三,我将粘贴我的代码片段。

工作原理:

基本上,您需要创建一个直播 (youtube.liveBroadcasts.insert) 并获取直播 ID (1)然后创建一个直播 (youtube.liveStreams.insert) 并从中获取直播 ID (2) 一旦您拥有了两者,您需要将直播绑定(bind)到直播 (youtube.liveBroadcasts.bind):

绑定(bind)的结果基本上包含流 URL。

用例:

基本上,它是一个直播 API:用户注册一个或多个 YouTube 帐户,我运行以下代码来创建直播并获取 rtmp 网址,以便我可以在多个 YouTube channel 上同时进行直播。

代码:

oauth2Client.setCredentials({
access_token: youtube_tokens.access_token,
refresh_token: youtube_tokens.refresh_token,
expiry_date: true
});

var youtube_broadcasts_body = {
snippet: {
// "scheduledEndTime": "2016-11-23T20:00:00.0+08",
"scheduledStartTime": start_date,
"title": target.stream_title
},
status: {
"privacyStatus": "private"
},
contentDetails: {
"projection": youtubeProjection
}
}
var youtube_livebroadcast_params = {
part: "id,snippet,status, contentDetails",
resource: youtube_broadcasts_body
}
var youtube_stream_body = {
snippet: {
"title": target.stream_title
},
cdn: {
"ingestionType": "rtmp",
"frameRate": "30fps",
"resolution": target.youtube_resolution
}
}
var youtube_stream_params = {
part: "id,snippet,cdn,status",
resource: youtube_stream_body
}
var youtube = google.youtube({ version: 'v3', auth: oauth2Client });

youtube.liveBroadcasts.insert(youtube_livebroadcast_params, function(err, res) {
if (err) {
logger.fatal(err)
return callback(err)
}
target.broadcast = res
target.status = "livebroadcast_event_created"
var broadcast_id = res.id

youtube.liveStreams.insert(youtube_stream_params, function(err, res) {
if (err) {
logger.fatal(err)
return callback(err)
}
var stream_id = res.id
res.broadcast_id = broadcast_id
target.stream = res
target.status = "livestream_created"
var youtube_livebroadcast_bind_params = {
part: "id,contentDetails",
streamId: stream_id,
id: broadcast_id
}

youtube.liveBroadcasts.bind(youtube_livebroadcast_bind_params, function(err, res) {
if (err) return callback(err)
target.status = "livebroadcast_bound_to_livestream"
target.ingest_url = target.stream.cdn.ingestionInfo.ingestionAddress + "/" + target.stream.cdn.ingestionInfo.streamName
target._id = broadcast_id
return callback(null, target)
})
})
})

发生的情况是,当我调用此代码两次(在 for 循环中)时,Youtube api 返回错误:未找到 Livebroadcast 就好像某处存在竞争条件导致创建 1 个单曲,而所有其他单曲都失败了。我必须做的一个肮脏的解决方法是添加一个超时(每 2 秒),但如果多个用户使用“我的 API”,这将导致整个事情失败。

现在我正在考虑创建一个队列来管理 YouTube 直播的创建。想知道是否有人遇到这个问题...

最佳答案

问题是此代码包装在一个函数中,该函数使用在其外部初始化的外部 oauth2Client 对象:

 const oauth2Client = new OAuth2( // initialize the object outside is wrong 
YOUTUBE_CLIENT_ID, // when you wanna use new tokens
YOUTUBE_CLIENT_SECRET, // inside the function.
YOUTUBE_REDIRECT_URL
);

var createLiveEvent = function(target, callback) {

var youtubeDestination = target.destination
var youtube_tokens = youtubeDestination.user_data;

oauth2Client.setCredentials({
access_token: youtube_tokens.access_token, //tokens got from oauth2 authentication
refresh_token: youtube_tokens.refresh_token,
expiry_date: true
});


var youtube = google.youtube({
version: 'v3',
auth: oauth2Client
});

logger.debug("Youtube: Creating event on Channel ")
....
....

})

解决方案实际上是在函数内部为每个不同的 access_token/refresh_token 使用不同的 oauth2Client 对象:

var createLiveEvent = function(target, callback) {
let oauth2Client = new OAuth2(
YOUTUBE_CLIENT_ID,
YOUTUBE_CLIENT_SECRET,
YOUTUBE_REDIRECT_URL
);


var youtubeDestination = target.destination
var youtube_tokens = youtubeDestination.user_data;

oauth2Client.setCredentials({
access_token: youtube_tokens.access_token,
refresh_token: youtube_tokens.refresh_token,
expiry_date: true
});

....
....
});

关于node.js - Youtube API无法同时创建直播和直播的两个绑定(bind)(1对1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42793385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com