Stream 支持从直播流和录制生成片段,让创作者和观众都能突出较长广播或录制中的简短精彩内容。Live instant clips 可由终端用户创建,不会产生额外存储费用,也不会在视频库中创建新条目。
配置 Live input 时,请确保已启用「Live Playback and Recording」(mode)。
生成预览或片段不需要 API key,但创建 Live Inputs 需要 API key。
Live instant clips 从直播流录制动态生成。生成片段 manifest 或 MP4 时,始终引用 Video ID,而非 Live Input ID。如果录制被删除,instant clip 将不再可用。
为帮助用户重播和定位最近内容,向 HLS manifest URL 添加 duration 参数以请求预览 manifest:
https://customer-<CODE>.cloudflarestream.com/<VIDEO_ID||INPUT_ID>/manifest/video.m3u8?duration=5mduration字符串,预览时长,最长 5 分钟,可以是秒数("30s")或分钟("3m")
交付预览 manifest 时,检查头中的两个属性:
preview-start-secondsfloat,预览 manifest 开始的直播流或录制中的秒数。在用户可以从预览中选择范围的应用中很有用,因为片段需要引用相对于 广播 开始时间的偏移,而非 预览 开始时间。stream-media-idstring,直播流或录制的 video ID。在使用 input ID 渲染播放器的应用中很有用,因为片段 URL 应引用 video ID。
此 manifest 可使用任何兼容 HLS 的播放器播放和定位。
加载 manifest 时读取头需要调整播放器处理响应的方式。例如,如果使用 HLS.js ↗ 和默认 loader,请覆盖 pLoader(playlist loader)类:
let currentPreviewStart;
let currentPreviewVideoID;
// Override the pLoader (playlist loader) to read the manifest headers:
class pLoader extends Hls.DefaultConfig.loader {
constructor(config) {
super(config);
var load = this.load.bind(this);
this.load = function (context, config, callbacks) {
if (context.type == 'manifest') {
var onSuccess = callbacks.onSuccess;
// copy the existing onSuccess handler to fire it later.
callbacks.onSuccess = function (response, stats, context, networkDetails) {
// The fourth argument here is undocumented in HLS.js but contains
// the response object for the manifest fetch, which gives us headers:
currentPreviewStart =
parseFloat(networkDetails.getResponseHeader('preview-start-seconds'));
// Save the start time of the preview manifest
currentPreviewVideoID =
networkDetails.getResponseHeader('stream-media-id');
// Save the video ID in case the preview was loaded with an input ID
onSuccess(response, stats, context);
// And fire the existing success handler.
};
}
load(context, config, callbacks);
};
}
}
// Specify the new loader class when setting up HLS
const hls = new Hls({
pLoader: pLoader,
});要播放直播流或录制的片段,请请求带有 duration 和 start time 的片段 manifest,相对于直播流开始时间。
https://customer-<CODE>.cloudflarestream.com/<VIDEO_ID>/manifest/clip.m3u8?time=600s&duration=30stime字符串,片段起始时间(秒),从直播流或录制开始duration字符串,片段时长(秒),最长 60 秒
此 manifest 可使用任何兼容 HLS 的播放器播放和定位。
也可以动态生成片段 MP4 以保存并在其他平台分享。
https://customer-<CODE>.cloudflarestream.com/<VIDEO_ID>/clip.mp4?time=600s&duration=30s&filename=clip.mp4time字符串,片段起始时间(秒),从直播流或录制开始(示例:"500s")duration字符串,片段时长(秒),最长 60 秒(示例:"60s")filename字符串 (可选) 片段的文件名