Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@api.video/video-uploader
Advanced tools
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.
Typescript library to upload videos to api.video using delegated upload token (or usual access token) from the front-end.
It allows you to upload videos in two ways:
If you use requirejs you can add the library as a dependency to your project with
$ npm install --save @api.video/video-uploader
You can then use the library in your script:
// standard upload:
var { VideoUploader } = require('@api.video/video-uploader');
var uploader = new VideoUploader({
// ... (see bellow)
});
// progressive upload:
var { ProgressiveUploader } = require('@api.video/video-uploader');
var uploader = new ProgressiveUploader({
// ... (see bellow)
});
If you use Typescript you can add the library as a dependency to your project with
$ npm install --save @api.video/video-uploader
You can then use the library in your script:
// standard upload:
import { VideoUploader } from '@api.video/video-uploader'
const uploader = new VideoUploader({
// ... (see bellow)
});
// progressive upload:
import { ProgressiveUploader } from '@api.video/video-uploader'
const uploader = new ProgressiveUploader({
// ... (see bellow)
});
Include the library in your HTML file like so:
<head>
...
<script src="https://unpkg.com/@api.video/video-uploader" defer></script>
</head>
Then, once the window.onload
event has been trigered, create your player using new VideoUploader()
:
...
<form>
<input type="file" id="input" onchange="uploadFile(this.files)">
</form>
<script type="text/javascript">
function uploadFile(files) {
new VideoUploader({
file: files[0],
uploadToken: "YOUR_DELEGATED_TOKEN"
}).upload();
}
</script>
The upload library is instanciated using 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 name | Mandatory | Type | Description |
---|---|---|---|
uploadToken | yes | string | your upload token |
videoId | no | string | id of an existing video |
common options (see bellow) |
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 name | Mandatory | Type | Description |
---|---|---|---|
accessToken | yes | string | your access token |
refreshToken | no | string | your refresh token (please not that if you don't provide a refresh token, your upload may fails due to the access token lifetime of 60 minutes) |
videoId | yes | string | id of an existing video |
common options (see bellow) |
Warning: be aware that exposing your API key client-side can lead to huge security issues. Use this method only if you know what you're doing :).
Option name | Mandatory | Type | Description |
---|---|---|---|
API Key | yes | string | your api.video API key |
videoId | yes | string | id of an existing video |
common options (see bellow) |
Option name | Mandatory | Type | Description |
---|---|---|---|
file | yes | File | the file you want to upload |
videoName | no | string | the name of your video (overrides the original file name for regular uploads, overrides the default "file" name for progressive uploads) |
chunkSize | no | number | number of bytes of each upload chunk (default: 50MB, min: 5MB, max: 128MB) |
apiHost | no | string | api.video host (default: ws.api.video) |
retries | no | number | number of retries when an API call fails (default: 5) |
retryStrategy | no | (retryCount: number, error: VideoUploadError) => number | null | function that returns the number of ms to wait before retrying a failed upload. Returns null to stop retrying |
maxVideoDuration | no | number | maximum duration allowed for the file (in seconds) |
const uploader = new VideoUploader({
file: files[0],
uploadToken: "YOUR_DELEGATED_TOKEN",
chunkSize: 1024*1024*10, // 10MB
retries: 10,
});
upload()
The upload() method starts the upload. It takes no parameter. It returns a Promise that resolves once the file is uploaded. If an API call fails more than the specified number of retries, then the promise is rejected.
On success, the promise embeds the video
object returned by the API.
On fail, the promise embeds the status code & error message returned by the API.
Example
// ... uploader instanciation
uploader.upload()
.then((video) => console.log(video))
.catch((error) => console.log(error.status, error.message));
onProgress()
The onProgress() method let you defined an upload progress listener. It takes a callback function with one parameter: the onProgress events. An onProgress event contains the following attributes:
Example
// ... uploader instanciation
uploader.onProgress((event) => {
console.log(`total number of bytes uploaded for this upload: ${event.uploadedBytes}.`);
console.log(`total size of the file: ${event.totalBytes}.`);
console.log(`number of upload chunks: ${event.chunksCount} .`);
console.log(`size of a chunk: ${event.chunksBytes}.`);
console.log(`index of the chunk being uploaded: ${event.currentChunk}.`);
console.log(`number of bytes uploaded for the current chunk: ${event.currentChunkUploadedBytes}.`);
});
onPlayable()
The onPlayable() method let you defined a listener that will be called when the video is playable. It takes a callback function with one parameter: the video
object returned by the API.
Example
<div id="player-container"></div>
<script>
// ... uploader instanciation
uploader.onPlayable((video) => {
// the video is playable, we can display the player
document.getElementById('player-container').innerHTML = v.assets.iframe;
});
</script>
The progressive upload object is instanciated using 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 name | Mandatory | Type | Description |
---|---|---|---|
uploadToken | yes | string | your upload token |
videoId | no | string | id of an existing video |
common options (see bellow) |
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 name | Mandatory | Type | Description |
---|---|---|---|
accessToken | yes | string | your access token |
videoId | yes | string | id of an existing video |
common options (see bellow) |
Option name | Mandatory | Type | Description |
---|---|---|---|
apiHost | no | string | api.video host (default: ws.api.video) |
retries | no | number | number of retries when an API call fails (default: 5) |
retryStrategy | no | (retryCount: number, error: VideoUploadError) => number | null | function that returns the number of ms to wait before retrying a failed upload. Returns null to stop retrying |
preventEmptyParts | no | boolean | if true, the upload will succeed even if an empty Blob is passed to uploadLastPart(). This may alter performances a bit in some cases (default: false) |
mergeSmallPartsBeforeUpload | no | boolean | if false, parts smaller than 5MB will not be merged before upload, resulting in an error (default: true) |
const uploader = new ProgressiveUploader({
uploadToken: "YOUR_DELEGATED_TOKEN",
retries: 10,
});
uploadPart(file: Blob)
The upload() method starts the upload. It takes no parameter. It returns a Promise that resolves once the file is uploaded. If an API call fails more than the specified number of retries, then the promise is rejected.
On success, the promise embeds the video
object returned by the API.
On fail, the promise embeds the status code & error message returned by the API.
Example
// ... uploader instanciation
uploader.uploadPart(blob)
.catch((error) => console.log(error.status, error.message));
uploadLastPart(file: Blob)
The upload() method starts the upload. It takes no parameter. It returns a Promise that resolves once the file is uploaded. If an API call fails more than the specified number of retries, then the promise is rejected.
On success, the promise embeds the video
object returned by the API.
On fail, the promise embeds the status code & error message returned by the API.
Example
// ... uploader instanciation
uploader.uploadLastPart(blob)
.then((video) => console.log(video))
.catch((error) => console.log(error.status, error.message));
onProgress()
The onProgress() method let you defined an upload progress listener. It takes a callback function with one parameter: the onProgress events. An onProgress event contains the following attributes:
Example
// ... uploader instanciation
uploader.onProgress((event) => {
console.log(`total number of bytes uploaded for this upload: ${event.uploadedBytes}.`);
console.log(`total size of the file: ${event.totalBytes}.`);
});
onPlayable()
The onPlayable() method let you defined a listener that will be called when the video is playable. It takes a callback function with one parameter: the video
object returned by the API.
Example
<div id="player-container"></div>
<script>
// ... uploader instanciation
uploader.onPlayable((video) => {
// the video is playable, we can display the player
document.getElementById('player-container').innerHTML = v.assets.iframe;
});
</script>
FAQs
api.video video uploader
The npm package @api.video/video-uploader receives a total of 564 weekly downloads. As such, @api.video/video-uploader popularity was classified as not popular.
We found that @api.video/video-uploader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.