您可以使用 API 上传视频,也可以直接在 Cloudflare 仪表板的 Stream 页面上传。
Go to Videos ↗有关接受的文件类型列表,请参阅支持的视频格式。
要使用 API,请在下面的示例中将 API_TOKEN 和 ACCOUNT_ID 替换为您的凭据。
curl \
-X POST \
-d '{"url":"https://storage.googleapis.com/stream-example-bucket/video.mp4","meta":{"name":"My First Stream Video"}}' \
-H "Authorization: Bearer <API_TOKEN>" \
https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/copyconst client = new Cloudflare({
apiEmail: process.env['CLOUDFLARE_EMAIL'],
apiKey: process.env['CLOUDFLARE_API_KEY'],
});
const video = await client.stream.copy.create({
account_id: '<ACCOUNT_ID>',
url: 'https://storage.googleapis.com/stream-example-bucket/video.mp4',
meta: { name: 'My First Stream Video' },
});有关从外部应用程序使用 REST API 的详细信息,请参阅完整的 Stream REST API 和 SDK 参考,其中包含适用于外部 TypeScript、Python 或 Go 应用程序的预生成 SDK。
export default {
async fetch(request, env, ctx): Promise<Response> {
const videoDetails = await env.STREAM.upload(
"https://storage.googleapis.com/stream-example-bucket/video.mp4",
{ meta: { name: "My First Stream Video" } }
);
return new Response(JSON.stringify(videoDetails));
},
} satisfies ExportedHandler<{ STREAM: StreamBinding }>;{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "<ENTER_WORKER_NAME>",
"main": "src/index.ts",
"compatibility_date": "$today",
"observability": {
"enabled": true
},
"stream": {
"binding": "STREAM"
}
}请参阅完整的 Workers Stream 绑定 API 参考。
由于 Stream 必须下载和处理视频,根据视频长度,视频可能需要几秒钟才能可用。您应轮询 Stream API 直到 readyToStream 为 true,或使用 webhooks 在视频准备好流式传输时收到通知。
使用第一步中的视频 UID 来轮询视频:
curl \
-H "Authorization: Bearer <API_TOKEN>" \
https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/<VIDEO_UID>{
"result": {
"uid": "6b9e68b07dfee8cc2d116e4c51d6a957",
"preview": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/watch",
"thumbnail": "https://customer-f33zs165nr7gyfy4.cloudflarestream.com/6b9e68b07dfee8cc2d116e4c51d6a957/thumbnails/thumbnail.jpg",
"readyToStream": true,
"status": {
"state": "ready"
},
"meta": {
"downloaded-from": "https://storage.googleapis.com/stream-example-bucket/video.mp4",
"name": "My First Stream Video"
},
"created": "2020-10-16T20:20:17.872170843Z",
"size": 9032701
//...
},
"success": true,
"errors": [],
"messages": []
}上传到 Stream 的视频可以在任何设备和平台上播放,从网站到原生应用。有关跨平台视频播放的详细信息和示例,请参阅播放视频。
要在您的网站上使用 Stream Player 播放视频,请从上述请求中复制视频的 uid 以及您的唯一客户代码,并在下面的嵌入代码中替换 <CODE> 和 <VIDEO_UID>:
<iframe
src="https://customer-<CODE>.cloudflarestream.com/<VIDEO_UID>/iframe"
title="Example Stream video"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
>
</iframe>上述嵌入代码也可以在 Cloudflare 仪表板的 Stream 页面上找到。
Go to Videos ↗- 编辑您的视频并添加字幕或水印
- 自定义 Stream 播放器
您可以使用 API 或 Cloudflare 仪表板的 Live inputs(直播输入) 页面创建 live input。
Go to Live inputs ↗要使用 API,请在下面的示例中将 API_TOKEN 和 ACCOUNT_ID 替换为您的凭据。
curl -X POST \
-H "Authorization: Bearer <API_TOKEN>" \
-D '{"meta": {"name":"test stream"},"recording": { "mode": "automatic" }}' \
https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/stream/live_inputsconst client = new Cloudflare({
apiEmail: process.env['CLOUDFLARE_EMAIL'],
apiKey: process.env['CLOUDFLARE_API_KEY'],
});
const liveInput = await client.stream.liveInputs.create({
account_id: '<ACCOUNT_ID>',
meta: { name: 'test stream' },
recording: { mode: 'automatic' },
});{
"uid": "f256e6ea9341d51eea64c9454659e576",
"rtmps": {
"url": "rtmps://live.cloudflare.com:443/live/",
"streamKey": "MTQ0MTcjM3MjI1NDE3ODIyNTI1MjYyMjE4NTI2ODI1NDcxMzUyMzcf256e6ea9351d51eea64c9454659e576"
},
"created": "2021-09-23T05:05:53.451415Z",
"modified": "2021-09-23T05:05:53.451415Z",
"meta": {
"name": "test stream"
},
"status": null,
"recording": {
"mode": "automatic",
"requireSignedURLs": false,
"allowedOrigins": null
}
}有关从外部应用程序使用 REST API 的详细信息,请参阅完整的 Stream REST API 和 SDK 参考,其中包含适用于外部 TypeScript、Python 或 Go 应用程序的预生成 SDK。
我们建议使用 Open Broadcaster Software (OBS) ↗ 入门。
直播可以在任何设备和平台上播放,从网站到原生应用,使用与上传到 Stream 的视频相同的视频播放器。有关跨平台视频播放的详细信息和示例,请参阅播放视频。
要在您的网站上使用 Stream Player 播放您刚开始的直播,请从上述请求中复制 live input 的 uid 以及您的唯一客户代码,并在下面的嵌入代码中替换 <CODE> 和 <VIDEO_UID>:
<iframe
src="https://customer-<CODE>.cloudflarestream.com/<VIDEO_UID>/iframe"
title="Example Stream video"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
>
</iframe>上述嵌入代码也可以在 Cloudflare 仪表板的 Stream 页面上找到。
Go to Videos ↗