当前位置:网站首页>JS实现视频录制-以Cesium为例
JS实现视频录制-以Cesium为例
2022-07-27 17:33:00 【giao00000】
JS实现视频录制-以Cesium为例
1. 需求描述
要求能够将Cesium三维地球的运动过程录制成视频。
2. 具体实现
2.1 主要原理
可以直接调用前端中的MediaStream Recording API,实现视频录制。核心示例代码如下:
var canvas = document.querySelector("canvas");
// Optional frames per second argument.
var stream = canvas.captureStream(25);
var recordedChunks = [];
console.log(stream);
var options = {
mimeType: "video/webm; codecs=vp9" };
mediaRecorder = new MediaRecorder(stream, options);
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start();
function handleDataAvailable(event) {
console.log("data-available");
if (event.data.size > 0) {
recordedChunks.push(event.data);
console.log(recordedChunks);
download();
} else {
// ...
}
}
function download() {
var blob = new Blob(recordedChunks, {
type: "video/webm"
});
// 创建指向二进制数据的URL
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = "test.webm";
a.click();
// 释放URL
window.URL.revokeObjectURL(url);
}
// demo: to download after 9sec
setTimeout(event => {
console.log("stopping");
mediaRecorder.stop();
}, 9000);
2.2 完整示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Include the CesiumJS JavaScript and CSS files -->
<script src="https://cesium.com/downloads/cesiumjs/releases/1.95/Build/Cesium/Cesium.js"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.95/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
</head>
<body>
<div id="cesiumContainer">
<button onclick="startRecord()">录制视频</button>
<button onclick="stopRecord()">结束录制</button>
</div>
<script> // Your access token can be found at: https://cesium.com/ion/tokens. // Replace `your_access_token` with your Cesium ion access token. // Cesium.Ion.defaultAccessToken = 'your_access_token'; // Initialize the Cesium Viewer in the HTML element with the `cesiumContainer` ID. const viewer = new Cesium.Viewer('cesiumContainer', {
terrainProvider: Cesium.createWorldTerrain() }); // Fly the camera to San Francisco at the given longitude, latitude, and height. viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(-122.4175, 37.655, 400), orientation: {
heading: Cesium.Math.toRadians(0.0), pitch: Cesium.Math.toRadians(-15.0), } }); var mediaRecorder, recordedChunks; setTimeout(() => {
// 视频录制 var canvas = document.querySelector(".cesium-widget>canvas"); // Optional frames per second argument. var stream = canvas.captureStream(25); recordedChunks = []; console.log(stream); var options = {
mimeType: "video/webm; codecs=vp9" }; mediaRecorder = new MediaRecorder(stream, options); mediaRecorder.ondataavailable = handleDataAvailable; }, 2000); function handleDataAvailable(event) {
console.log("data-available"); if (event.data.size > 0) {
recordedChunks.push(event.data); console.log(recordedChunks); download(); } else {
// ... } } function download() {
var blob = new Blob(recordedChunks, {
type: "video/webm" }); var url = URL.createObjectURL(blob); var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; a.href = url; a.download = "test.webm"; a.click(); window.URL.revokeObjectURL(url); } // 开始录制 function startRecord() {
console.log("starting"); mediaRecorder.start(); } // 结束录制 function stopRecord() {
console.log("stopping"); mediaRecorder.stop(); } </script>
</div>
</body>
</html>
3. 示例效果

4. 参考链接
边栏推荐
- 中国业务型CDP白皮书 | 爱分析报告
- Marqueetextview (running lantern)
- Sharepreference (storage)
- PMP每日一练 | 考试不迷路-7.27(包含敏捷+多选)
- Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
- GridView (implement table display icon)
- 总线Bus是什么意思
- In a real case, college students were cheated when taking orders. I hope you won't be cheated [painful lesson]
- Acwing 692. g bus count difference + prefix and
- NAT 11.16
猜你喜欢

System information function of MySQL function summary

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.

真实案例,大学生接单被骗,希望大家不要被骗了【惨痛教训】

PC博物馆(3) MITS Altair 8800

疫情下我离职一年,收入增长了10倍

PMP practice once a day | don't get lost in the exam -7.27 (including agility + multiple choices)

Online Judge 输出超限

黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。

中国业务型CDP白皮书 | 爱分析报告
随机推荐
AcWing 692. G巴士计数 差分+前缀和
LeetCode练习2——两数之和
信道容量、信道带宽基本概念的理解
总线Bus是什么意思
Sword finger offer 25. merge two sorted linked lists
Chemical giant BASF & Pasqual: using quantum neural network to optimize weather forecast
Broadcastreceiver (broadcast)
How to encrypt the data in MySQL database? Mysql8.0 comes with new features
Rodin 安装 SMT Solvers 插件
Overview of deep active learning 2020
C # find perfect numbers, output daffodils and use of classes
LED高精度体重秤方案规格书
VirtualBox: set shared folder
11.2DHCP
2022 love analysis · smart community manufacturer panoramic report manufacturer solicitation
PMP每日一练 | 考试不迷路-7.27(包含敏捷+多选)
C # network application programming, experiment 1: WPF exercise
[论文阅读] Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
#yy关于鱼的英文学习