@api.video/media-recorder

中文文档
1.0.11发布于 2024/09/24MITTypeScriptESM + CJS近 30 天下载 2504

api.video media recorder - upload video from your webcam with ease

原始使用文档
文档目录8 节
  1. Table of contents
  2. Project description
  3. Getting started
  4. Installation
  5. Documentation
  6. Instantiation
  7. Methods
  8. Full example

badge   badge   badge

api.video media recorder

npm ts

api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and managing on-demand & low latency live streaming features in your app.

Table of contents

Project description

Typescript library to easily upload data from a MediaStream to api.video. It can be used to upload a video to api.video from the user's webcam with ease, as well as from a screen recording.

Getting started

Installation

Installation method #1: requirejs

If you use requirejs you can add the library as a dependency to your project with

$ npm install --save @api.video/media-recorder

You can then use the library in your script:

var { ApiVideoMediaRecorder } = require('@api.video/media-recorder');

var recorder = new ApiVideoMediaRecorder(mediaStream, {
    uploadToken: "YOUR_DELEGATED_TOKEN"
    // ... other optional options
}); 

Installation method #2: typescript

If you use Typescript you can add the library as a dependency to your project with

$ npm install --save @api.video/media-recorder

You can then use the library in your script:

import { ApiVideoMediaRecorder } from '@api.video/media-recorder'

const recorder = new ApiVideoMediaRecorder(mediaStream, {file: files[0],
    uploadToken: "YOUR_DELEGATED_TOKEN"
    // ... other optional options
});

Simple include in a javascript project

Include the library in your HTML file like so:

<head>
    ...
    <script src="https://unpkg.com/@api.video/media-recorder" defer></script>
</head>

Then, once the window.onload event has been trigered, create your player using new ApiVideoMediaRecorder():

...
<script type="text/javascript"> 
    const recorder = new ApiVideoMediaRecorder(mediaStream, {
        uploadToken: "YOUR_DELEGATED_TOKEN"
    });
    recorder.start();
    // ...
    recorder.stop().then((video) => console.log(video));
</script>

Documentation

Instantiation

Options

The media recorder object is instantiated using a MediaStream and an options object. Options to provide depend on the way you want to authenticate to the API: either using a delegated upload token (recommanded), or using a usual access token.

Using delegated upload tokens for authentication is best options when uploading from the client side. To know more about delegated upload token, read the dedicated article on api.video's blog: Delegated Uploads.

Option nameMandatoryTypeDescription
uploadTokenyesstringyour upload token
videoIdnostringid of an existing video
common options (see bellow)
Using an access token (discouraged):

Warning: be aware that exposing your access token client-side can lead to huge security issues. Use this method only if you know what you're doing :).

Option nameMandatoryTypeDescription
accessTokenyesstringyour access token
videoIdyesstringid of an existing video
common options (see bellow)
Common options
Option nameMandatoryTypeDescription
apiHostnostringapi.video host (default: ws.api.video)
retriesnonumbernumber of retries when an API call fails (default: 5)
videoNamenostringthe name of your recorded video (overrides the default "file" name)

Example

    const mediaRecorder = new ApiVideoMediaRecorder(myMediaStream, {
        uploadToken: "YOUR_DELEGATED_TOKEN",
        retries: 10,
    });

Methods

start(options?: { timeslice?: number })

The start() method starts the upload of the content retrieved from the MediaStream. It takes an optionnal options parameter.

Options
Option nameMandatoryTypeDescription
timesliceno (default: 5000)numberThe number of milliseconds to record into each Blob.

Example

    // ... mediaRecorder instantiation

    mediaRecorder.start();
    // or, with a 2 seconds timeslice:
    // mediaRecorder.start({ timeslice: 2000 });

stop(): Promise<VideoUploadResponse>

The start() method stops the media recording. It upload the last part of content retrieved from the MediaStream (this will start the aggregation of the video parts on the api.video side). It takes no parameter. It returns a Promise that resolves with the newly created video.

addEventListener(event: string, listener: Function)

Define an event listener for the media recorder. The following events are available:

  • "error": when an error occurs
  • "recordingStopped": when the recording is stopped
  • "videoPlayable": when the video is playable

Example

    // ... mediaRecorder instantiation

    mediaRecorder.addEventListener("error", (event) => {
       console.log(event.data);
    });

getMediaRecorderState(): RecordingState

Return the state of the underlaying MediaRecorder. The state can be one of the following: inactive, paused, recording.

Example

    // ... mediaRecorder instantiation

    mediaRecorder.stop()
        .then(video => console.log(video));

Full example

<html>
    <head>
        <script src="../dist/index.js"></script>
        <style>
            #container {
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            #video {
                width: 640;
                height: 480;
                border: 1px solid gray;
            }
            #container div {
                margin: 10px 0;
            }
        </style>
    </head>

    <body>
        <div id="container">
            <div>
                <video id="video"></video>
            </div>
            <div>
                <button id="start" disabled>start recording</button>
                <button id="stop" disabled>stop recording</button>
            </div>
            <div>
                <p>Video link: <span id="video-link"><i>will be displayed when the recording is finished</i></span></p>
            </div>
        </div>
        
        <script> 
            const video = document.querySelector('#video');
            const startButton = document.getElementById("start");
            const stopButton = document.getElementById("stop");
            const videoLink = document.getElementById("video-link");
            let stream, recorder;

            var constraints = window.constraints = {
                audio: true,
                video: true
            };

            navigator.mediaDevices.getUserMedia(constraints).then((s) => {
                stream = s;
                video.srcObject = s;
                video.play();
                startButton.disabled = false;
            });

            document.getElementById("start").addEventListener("click", () => {
                recorder = new ApiVideoMediaRecorder(stream, {
                    uploadToken: "UPLOAD_TOKEN"
                });
                
                recorder.start();
                
                startButton.disabled = true;
                stopButton.disabled = false;
            });

            document.getElementById("stop").addEventListener("click", () => {
                startButton.disabled = false;
                stopButton.disabled = true;

                recorder.stop().then(v => videoLink.innerHTML = v.assets.player);
            });
        </script>
    </body>
</html>

版本与发布记录

@api.video/media-recorder 共发布 12 个版本,首次发布于 2021/11/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.11latest2024/09/242 年前
1.0.102023/02/073 年前
1.0.92023/02/073 年前
1.0.82023/01/243 年前
1.0.72022/10/104 年前
1.0.62022/07/114 年前

关于 @api.video/media-recorder 的常见问题

@api.video/media-recorder 是什么?

api.video media recorder - upload video from your webcam with ease

如何安装 @api.video/media-recorder?

在项目目录运行 npm install @api.video/media-recorder,或使用 pnpm add / yarn add / bun add。

@api.video/media-recorder 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@api.video/media-recorder 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@api.video/media-recorder 可以商用吗?

@api.video/media-recorder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@api.video/media-recorder 的下载量有多少?

根据 npm 官方下载统计,@api.video/media-recorder 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,504 次,日均约 83 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@api.video/media-recorder 还在维护吗?

最新版本 1.0.11 发布于 2024/09/24(2 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。