- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
首先,我将解释如何使用 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/
我致力于收集互联网广播流文件,例如 m3u,其中包含一个流式链接 (例如 http://aska.ru-hoster.com:8053/autodj )。 我没有找到关于如何检查链接是否可用/有效的示
为了学校,我需要建立一个 HTML5 直播网站。他们有一个他们一直在使用的 Flash 流播放器,但现在他们希望它改用 HTML5。我怎样才能做到这一点?我尝试使用视频标签,但无法正常工作。下面是我的
我正在尝试使用 FFmpeg 将视频循环流式传输到 justin.tv?我已经设法循环图像序列并将其与音频中的线条组合起来: ffmpeg -loop 1 -i imageSequence%04d.j
使用 WebAudio API 播放直播流的正确方法是什么。 我正在尝试使用以下代码,但是我看到的只是正在下载 MP3,但没有播放;可能 MediaElementSource 需要一个文件,而不是连续
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我正在开发一个播放实时 HTTP 网络广播流(.pls、.mp3 ...)的应用程序。我可以使用 MPMusicPlayerController 播放流吗?塔克斯。 最佳答案 我认为您不能使用 MPM
好的,我一直在努力解决这个 http 直播。我只是不明白,是的,我已经阅读了所有苹果文档并观看了 wwdc 视频,但仍然非常困惑,所以请帮助想成为程序员的人!!! 你写的代码跑到服务器上了?不在 xc
我正在尝试构建一个移动应用程序,该应用程序从设备的摄像头流式传输视频并将其实时发送到服务器。此外,移动设备应该能够播放从服务器接收到的实况视频。 我正在 Flutter 中构建应用程序,但似乎无法在
我正在使用 ffmpeg sdk 在 youtube live 上流式传输实时视频 av_interleaved_write_frame(m_pAvFmtCntx, pkt); av_interlea
我在 android 上使用 ffmpeg 3.4 版本的源代码。我尝试使用它来复用音频/视频元素流作为 m3u8 输出并使用以下选项: int num = av_opt_set_from_s
我正在尝试使用静态图像和音频文件通过 ffmpeg 进行直播。像这样的ffmpeg命令 ffmpeg -re -loop 1 -f image2 -i '/tmp/11.jpg' -f lavfi -
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我希望能够将 iPhone 摄像头的帧/视频(实时)流式传输到互联网。我在线程(streaming video FROM an iPhone)中看到可以使用AVCaptureSession的begin
我想创建一个网站,每个用户都可以创建/开始直播。 但正如我现在所看到的,我只能为我自己的帐户创建一个流。启用了 Youtube 数据 API 的那个。 key 是用代码编写的。所以我无权从某人的帐户创
我正在构建一个监控系统,它可以在白天将视频流式传输到 YouTube,但在夜间会关闭。 YouTube 直播在停止接收数据时会自动关闭流。 我可以使用 API 开始另一个流,但我想为公众保留相同的 Y
我在 YouTube 上有现场事件,我想在我的网站上播放它。我想将我的事件设为私有(private),获取它的 RTMP 广播 URL 并将其粘贴到我的网站上,在 JWPlayer 中。 那可能吗?
1- 哪一个更适合用于流媒体视频? TCP 或 UDP 套接字以及为什么? 2-直播时,音频和视频分别来自服务器,那么如何确保我显示的视频和设备上播放的音频同步? 最佳答案 我不久前编写了一个语音聊天
一直在使用 ffmpeg 通过 HLS 进行直播并通过 video.js 进行播放。它可以工作,但流开始的时间和实际开始播放的时间之间有 20 秒的延迟。 你知道为什么下面的 ffmpeg 命令不会像
除了无法解释的 Sample TV Input 应用 (https://github.com/googlesamples/androidtv-sample-inputs/),没有很多关于如何在 Liv
我真的厌倦了这个问题。如果有人在这方面帮助我,我将不胜感激。 我的一个应用被拒绝,原因是 Video streaming content over a cellular network longer
我是一名优秀的程序员,十分优秀!