@remotion/media-utils 4.0.164 → 4.0.166

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAudioDuration = exports.getAudioDurationInSeconds = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-use-before-define */
5
+ const media_tag_error_handling_1 = require("./media-tag-error-handling");
5
6
  const p_limit_1 = require("./p-limit");
6
7
  const limit = (0, p_limit_1.pLimit)(3);
7
8
  const metadataCache = {};
@@ -16,8 +17,13 @@ const fn = (src) => {
16
17
  audio.src = src;
17
18
  return new Promise((resolve, reject) => {
18
19
  const onError = () => {
19
- reject(audio.error);
20
- cleanup();
20
+ (0, media_tag_error_handling_1.onMediaError)({
21
+ error: audio.error,
22
+ src,
23
+ cleanup,
24
+ reject,
25
+ api: 'getAudioDurationInSeconds()',
26
+ });
21
27
  };
22
28
  const onLoadedMetadata = () => {
23
29
  metadataCache[src] = audio.duration;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getVideoMetadata = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-use-before-define */
5
5
  const is_remote_asset_1 = require("./is-remote-asset");
6
+ const media_tag_error_handling_1 = require("./media-tag-error-handling");
6
7
  const p_limit_1 = require("./p-limit");
7
8
  const cache = {};
8
9
  const limit = (0, p_limit_1.pLimit)(3);
@@ -17,8 +18,13 @@ const fn = (src) => {
17
18
  video.src = src;
18
19
  return new Promise((resolve, reject) => {
19
20
  const onError = () => {
20
- reject(video.error);
21
- cleanup();
21
+ (0, media_tag_error_handling_1.onMediaError)({
22
+ error: video.error,
23
+ src,
24
+ cleanup,
25
+ reject,
26
+ api: 'getVideoMetadata()',
27
+ });
22
28
  };
23
29
  const onLoadedMetadata = () => {
24
30
  const pixels = video.videoHeight * video.videoWidth;
@@ -0,0 +1,7 @@
1
+ export declare const onMediaError: ({ error, src, reject, cleanup, api, }: {
2
+ error: MediaError;
3
+ src: string;
4
+ reject: (reason: unknown) => void;
5
+ cleanup: () => void;
6
+ api: string;
7
+ }) => void;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.onMediaError = void 0;
4
+ async function fetchWithTimeout(url, options, timeout = 3000) {
5
+ const controller = new AbortController();
6
+ const id = setTimeout(() => controller.abort(), timeout);
7
+ options.signal = controller.signal;
8
+ try {
9
+ const response = await fetch(url, options);
10
+ clearTimeout(id);
11
+ return response;
12
+ }
13
+ catch (e) {
14
+ clearTimeout(id);
15
+ throw new Error(`Fetch timed out after ${timeout}ms`);
16
+ }
17
+ }
18
+ const checkFor404 = (src) => {
19
+ return fetchWithTimeout(src, {
20
+ method: 'HEAD',
21
+ mode: 'no-cors',
22
+ }).then((res) => res.status);
23
+ };
24
+ const checkFor404OrSkip = async ({ suspecting404, sameOrigin, src, }) => {
25
+ if (!suspecting404) {
26
+ return Promise.resolve(null);
27
+ }
28
+ if (!sameOrigin) {
29
+ return Promise.resolve(null);
30
+ }
31
+ try {
32
+ return await checkFor404(src);
33
+ }
34
+ catch (e) {
35
+ return Promise.resolve(null);
36
+ }
37
+ };
38
+ const onMediaError = ({ error, src, reject, cleanup, api, }) => {
39
+ const suspecting404 = (error === null || error === void 0 ? void 0 : error.MEDIA_ERR_SRC_NOT_SUPPORTED) === (error === null || error === void 0 ? void 0 : error.code);
40
+ const isSrcSameOriginAsCurrent = new URL(src, window.location.origin)
41
+ .toString()
42
+ .startsWith(window.location.origin);
43
+ checkFor404OrSkip({
44
+ suspecting404,
45
+ sameOrigin: isSrcSameOriginAsCurrent,
46
+ src,
47
+ })
48
+ .then((status) => {
49
+ const err = status === 404
50
+ ? new Error([
51
+ `Failed to execute ${api}: Received a 404 error loading "${src}".`,
52
+ 'Correct the URL of the file.',
53
+ ].join(' '))
54
+ : new Error([
55
+ `Failed to execute ${api}, Received a MediaError loading "${src}".`,
56
+ status === null
57
+ ? `Browser error message: ${error === null || error === void 0 ? void 0 : error.message}`
58
+ : `HTTP Status code of the file: ${status}.`,
59
+ 'Check the path of the file and if it is a valid video.',
60
+ ]
61
+ .filter(Boolean)
62
+ .join(' '));
63
+ reject(err);
64
+ cleanup();
65
+ console.log('404', status);
66
+ })
67
+ .catch((e) => {
68
+ reject(e);
69
+ cleanup();
70
+ });
71
+ };
72
+ exports.onMediaError = onMediaError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remotion/media-utils",
3
- "version": "4.0.164",
3
+ "version": "4.0.166",
4
4
  "description": "Utility functions for audio and video",
5
5
  "main": "dist/index.js",
6
6
  "sideEffects": false,
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/remotion-dev/remotion/issues"
14
14
  },
15
15
  "dependencies": {
16
- "remotion": "4.0.164"
16
+ "remotion": "4.0.166"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "react": ">=16.8.0",