Skip to content

Commit

Permalink
Merge pull request #65 from apivideo/add-download-method
Browse files Browse the repository at this point in the history
Add download() method
  • Loading branch information
olivier-lando authored Aug 21, 2024
2 parents 5d1a596 + b42d563 commit d7640a2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All changes to this project will be documented in this file.

## [1.2.33] - 2024-08-21

- Add `download()` method

## [1.2.32] - 2024-07-16

- Add `isFullScreen` in `fullscreenchange` event
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ Retrieve the playback rate.

Check whether the video is a live stream.

**`download(filename?: string): void`**

Download the video. If a filename is not provided, the default name 'video.mp4' will be used.
An exception will be thrown if the video cannot be downloaded.

**`destroy()`**

Destroy the player instance.
Expand Down
36 changes: 36 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ export class PlayerSdk {
getLoop(callback?: (loop: boolean) => void): Promise<boolean> {
return this.postMessage({ message: "getLoop" }, callback);
}
download(filename?: string) {
this.postMessage({ message: "getMp4Url" }, (res) => {
if (!res) {
throw new Error("This video is not downloadable");
}
fetch(res as string).then((response) => {
response.blob().then((blob) => {
const url = URL.createObjectURL(blob);

const link = document.createElement("a");
link.href = url;
link.download = filename || "video.mp4";
link.target = "_blank";
link.click();

URL.revokeObjectURL(url);
});
});
});
}
getVideoSize(
callback?: (size: { width: number; height: number }) => void
): Promise<{ width: number; height: number }> {
Expand Down Expand Up @@ -638,6 +658,22 @@ export class PlayerSdk {
});
}

private appendQueryParamIfMissing = (
url: string,
name: string,
value: string
): string => {
const urlObj = new URL(url, document.location.href);
const urlSearchParams = new URLSearchParams(urlObj.search);
if (urlSearchParams.has(name)) {
return url;
}

urlSearchParams.append(name, value);
urlObj.search = urlSearchParams.toString();
return urlObj.toString();
};

private makeId(length: number) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@api.video/player-sdk",
"version": "1.2.32",
"version": "1.2.33",
"description": "api.video player SDK",
"repository": {
"type": "git",
Expand Down

0 comments on commit d7640a2

Please sign in to comment.