Skip to content

Commit b42d563

Browse files
committed
Add download() method
1 parent 5d1a596 commit b42d563

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
## [1.2.33] - 2024-08-21
6+
7+
- Add `download()` method
8+
59
## [1.2.32] - 2024-07-16
610

711
- Add `isFullScreen` in `fullscreenchange` event

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ Retrieve the playback rate.
429429

430430
Check whether the video is a live stream.
431431

432+
**`download(filename?: string): void`**
433+
434+
Download the video. If a filename is not provided, the default name 'video.mp4' will be used.
435+
An exception will be thrown if the video cannot be downloaded.
436+
432437
**`destroy()`**
433438

434439
Destroy the player instance.

index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,26 @@ export class PlayerSdk {
376376
getLoop(callback?: (loop: boolean) => void): Promise<boolean> {
377377
return this.postMessage({ message: "getLoop" }, callback);
378378
}
379+
download(filename?: string) {
380+
this.postMessage({ message: "getMp4Url" }, (res) => {
381+
if (!res) {
382+
throw new Error("This video is not downloadable");
383+
}
384+
fetch(res as string).then((response) => {
385+
response.blob().then((blob) => {
386+
const url = URL.createObjectURL(blob);
387+
388+
const link = document.createElement("a");
389+
link.href = url;
390+
link.download = filename || "video.mp4";
391+
link.target = "_blank";
392+
link.click();
393+
394+
URL.revokeObjectURL(url);
395+
});
396+
});
397+
});
398+
}
379399
getVideoSize(
380400
callback?: (size: { width: number; height: number }) => void
381401
): Promise<{ width: number; height: number }> {
@@ -638,6 +658,22 @@ export class PlayerSdk {
638658
});
639659
}
640660

661+
private appendQueryParamIfMissing = (
662+
url: string,
663+
name: string,
664+
value: string
665+
): string => {
666+
const urlObj = new URL(url, document.location.href);
667+
const urlSearchParams = new URLSearchParams(urlObj.search);
668+
if (urlSearchParams.has(name)) {
669+
return url;
670+
}
671+
672+
urlSearchParams.append(name, value);
673+
urlObj.search = urlSearchParams.toString();
674+
return urlObj.toString();
675+
};
676+
641677
private makeId(length: number) {
642678
const characters =
643679
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/player-sdk",
3-
"version": "1.2.32",
3+
"version": "1.2.33",
44
"description": "api.video player SDK",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)