1 Star 0 Fork 0

programmingWindows/mediasource-append-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.js 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Get the duration of video, via its raw blob
* @param {Blob} blob
* @returns {Promise<number>}
*/
const getDuration = (blob) => {
return new Promise((res) => {
const tempVidElem = document.createElement('video');
tempVidElem.onloadedmetadata = () => {
res(tempVidElem.duration);
URL.revokeObjectURL(tempVidElem.src);
};
tempVidElem.src = URL.createObjectURL(blob);
});
};
/**
* Adds (and returns once ready) a SourceBuffer to a MediaSource
* @param {MediaSource} mediaSource
* @param {string} mimeStr Example: `video/webm; codecs="vp9,opus"`
* @param {'sequence' | 'segments'} [mode]
* @returns {Promise<SourceBuffer>}
*/
const addSourceBufferWhenOpen = (mediaSource, mimeStr, mode = 'segments') => {
return new Promise((res, rej) => {
const getSourceBuffer = () => {
try {
const sourceBuffer = mediaSource.addSourceBuffer(mimeStr);
sourceBuffer.mode = mode;
res(sourceBuffer);
} catch (e) {
rej(e);
}
};
if (mediaSource.readyState === 'open') {
getSourceBuffer();
} else {
mediaSource.addEventListener('sourceopen', getSourceBuffer);
}
});
};
/**
* Convert a base64 string into a binary blob
* @param {string} base64Str
* @param {string} mimeTypeStr
*/
const b64toBlob = (base64Str, mimeTypeStr) =>
fetch(`data:${mimeTypeStr};base64,${base64Str}`).then((res) => res.blob());
/**
* Randomly shuffle array *in-place*
* @param {any[]} array - Array to shuffle
*/
const shuffleArr = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/programmingwindows/mediasource-append-examples.git
git@gitee.com:programmingwindows/mediasource-append-examples.git
programmingwindows
mediasource-append-examples
mediasource-append-examples
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385