@remotion/webcodecs 4.0.210
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.
- package/LICENSE.md +49 -0
- package/README.md +18 -0
- package/dist/audio-decoder-config.d.ts +2 -0
- package/dist/audio-decoder-config.js +13 -0
- package/dist/audio-decoder.d.ts +15 -0
- package/dist/audio-decoder.js +85 -0
- package/dist/audio-encoder-config.d.ts +2 -0
- package/dist/audio-encoder-config.js +13 -0
- package/dist/audio-encoder.d.ts +16 -0
- package/dist/audio-encoder.js +90 -0
- package/dist/codec-id.d.ts +2 -0
- package/dist/codec-id.js +2 -0
- package/dist/convert-media.d.ts +26 -0
- package/dist/convert-media.js +86 -0
- package/dist/create-audio-decoder.d.ts +7 -0
- package/dist/create-audio-decoder.js +18 -0
- package/dist/create-decoder.d.ts +6 -0
- package/dist/create-decoder.js +32 -0
- package/dist/create-encoder.d.ts +9 -0
- package/dist/create-encoder.js +46 -0
- package/dist/create-video-decoder.d.ts +6 -0
- package/dist/create-video-decoder.js +18 -0
- package/dist/decoder.d.ts +7 -0
- package/dist/decoder.js +44 -0
- package/dist/encoder.d.ts +7 -0
- package/dist/encoder.js +43 -0
- package/dist/error-cause.d.ts +8 -0
- package/dist/error-cause.js +3 -0
- package/dist/esm/index.mjs +793 -0
- package/dist/get-config.d.ts +1 -0
- package/dist/get-config.js +21 -0
- package/dist/get-description.d.ts +6 -0
- package/dist/get-description.js +20 -0
- package/dist/get-samples.d.ts +6 -0
- package/dist/get-samples.js +24 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +13 -0
- package/dist/load-mp4-file.d.ts +8 -0
- package/dist/load-mp4-file.js +37 -0
- package/dist/on-audio-track.d.ts +15 -0
- package/dist/on-audio-track.js +105 -0
- package/dist/on-video-track.d.ts +15 -0
- package/dist/on-video-track.js +101 -0
- package/dist/reencode-video.d.ts +1 -0
- package/dist/reencode-video.js +68 -0
- package/dist/resolve-audio-action.d.ts +16 -0
- package/dist/resolve-audio-action.js +30 -0
- package/dist/resolve-video-action.d.ts +15 -0
- package/dist/resolve-video-action.js +30 -0
- package/dist/video-decoder-config.d.ts +1 -0
- package/dist/video-decoder-config.js +24 -0
- package/dist/video-decoder.d.ts +14 -0
- package/dist/video-decoder.js +87 -0
- package/dist/video-encoder-config.d.ts +1 -0
- package/dist/video-encoder-config.js +24 -0
- package/dist/video-encoder.d.ts +13 -0
- package/dist/video-encoder.js +90 -0
- package/dist/video-parser.d.ts +1 -0
- package/dist/video-parser.js +51 -0
- package/dist/wait-for-dequeue.d.ts +5 -0
- package/dist/wait-for-dequeue.js +51 -0
- package/dist/with-resolvers.d.ts +5 -0
- package/dist/with-resolvers.js +13 -0
- package/package.json +37 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Remotion License
|
|
2
|
+
|
|
3
|
+
In Remotion 5.0, the license will slightly change. [View the changes here](https://github.com/remotion-dev/remotion/pull/3750).
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Depending on the type of your legal entity, you are granted permission to use Remotion for your project. Individuals and small companies are allowed to use Remotion to create videos for free (even commercial), while a company license is required for for-profit organizations of a certain size. This two-tier system was designed to ensure funding for this project while still allowing the source code to be available and the program to be free for most. Read below for the exact terms of use.
|
|
8
|
+
|
|
9
|
+
- [Free license](#free-license)
|
|
10
|
+
- [Company license](#company-license)
|
|
11
|
+
|
|
12
|
+
## Free license
|
|
13
|
+
|
|
14
|
+
Copyright © 2024 [Remotion](https://www.remotion.dev)
|
|
15
|
+
|
|
16
|
+
### Eligibility
|
|
17
|
+
|
|
18
|
+
You are eligible to use Remotion for free if you are:
|
|
19
|
+
|
|
20
|
+
- an individual
|
|
21
|
+
- a for-profit organization with up to 3 employees
|
|
22
|
+
- a non-profit or not-for-profit organization
|
|
23
|
+
- evaluating whether Remotion is a good fit, and are not yet using it in a commercial way
|
|
24
|
+
|
|
25
|
+
### Allowed use cases
|
|
26
|
+
|
|
27
|
+
Permission is hereby granted, free of charge, to any person eligible for the "Free license", to use the software non-commercially or commercially for the purpose of creating videos and images and to modify the software to their own liking, for the purpose of fulfilling their custom use case or to contribute bug fixes or improvements back to Remotion.
|
|
28
|
+
|
|
29
|
+
### Disallowed use cases
|
|
30
|
+
|
|
31
|
+
It is not allowed to copy or modify Remotion code for the purpose of selling, renting, licensing, relicensing, or sublicensing your own derivate of Remotion.
|
|
32
|
+
|
|
33
|
+
### Warranty notice
|
|
34
|
+
|
|
35
|
+
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the author or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
|
|
36
|
+
|
|
37
|
+
### Support
|
|
38
|
+
|
|
39
|
+
Support is provided on a best-we-can-do basis via GitHub Issues and Discord.
|
|
40
|
+
|
|
41
|
+
## Company license
|
|
42
|
+
|
|
43
|
+
You are required to obtain a company license to use Remotion if you are not within the group of entities eligible for a free license. This license will enable you to use Remotion for the allowed use cases specified in the free license, and give you access to prioritized support (read the [Support Policy](https://www.remotion.dev/docs/support)).
|
|
44
|
+
|
|
45
|
+
Visit [remotion.pro](https://www.remotion.pro/license) for pricing and to buy a license.
|
|
46
|
+
|
|
47
|
+
### FAQs
|
|
48
|
+
|
|
49
|
+
Are you not sure whether you need a company license because of an edge case? Here are some [frequently asked questions](https://www.remotion.pro/faq).
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @remotion/webcodecs
|
|
2
|
+
|
|
3
|
+
Media conversion in the browser
|
|
4
|
+
|
|
5
|
+
[](https://npmcharts.com/compare/@remotion/webcodecs?minimal=true)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @remotion/webcodecs --save-exact
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
When installing a Remotion package, make sure to align the version of all `remotion` and `@remotion/*` packages to the same version.
|
|
14
|
+
Remove the `^` character from the version number to use the exact version.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
This is an internal package and has no documentation.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAudioDecoderConfig = void 0;
|
|
4
|
+
const getAudioDecoderConfig = async (config) => {
|
|
5
|
+
if (typeof AudioDecoder === 'undefined') {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
if ((await AudioDecoder.isConfigSupported(config)).supported) {
|
|
9
|
+
return config;
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
};
|
|
13
|
+
exports.getAudioDecoderConfig = getAudioDecoderConfig;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="dom-webcodecs" />
|
|
2
|
+
import type { AudioSample } from '@remotion/media-parser';
|
|
3
|
+
export type WebCodecsAudioDecoder = {
|
|
4
|
+
processSample: (audioSample: AudioSample) => Promise<void>;
|
|
5
|
+
waitForFinish: () => Promise<void>;
|
|
6
|
+
close: () => void;
|
|
7
|
+
getQueueSize: () => number;
|
|
8
|
+
flush: () => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
export declare const createAudioDecoder: ({ onFrame, onError, signal, config, }: {
|
|
11
|
+
onFrame: (frame: AudioData) => Promise<void>;
|
|
12
|
+
onError: (error: DOMException) => void;
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
config: AudioDecoderConfig;
|
|
15
|
+
}) => WebCodecsAudioDecoder;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAudioDecoder = void 0;
|
|
4
|
+
const createAudioDecoder = ({ onFrame, onError, signal, config, }) => {
|
|
5
|
+
if (signal.aborted) {
|
|
6
|
+
throw new Error('Not creating audio decoder, already aborted');
|
|
7
|
+
}
|
|
8
|
+
let outputQueue = Promise.resolve();
|
|
9
|
+
let outputQueueSize = 0;
|
|
10
|
+
let dequeueResolver = () => { };
|
|
11
|
+
const audioDecoder = new AudioDecoder({
|
|
12
|
+
output(inputFrame) {
|
|
13
|
+
outputQueueSize++;
|
|
14
|
+
outputQueue = outputQueue
|
|
15
|
+
.then(() => onFrame(inputFrame))
|
|
16
|
+
.then(() => {
|
|
17
|
+
dequeueResolver();
|
|
18
|
+
outputQueueSize--;
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
error(error) {
|
|
23
|
+
onError(error);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
const close = () => {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
28
|
+
signal.removeEventListener('abort', onAbort);
|
|
29
|
+
if (audioDecoder.state === 'closed') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
audioDecoder.close();
|
|
33
|
+
};
|
|
34
|
+
const onAbort = () => {
|
|
35
|
+
close();
|
|
36
|
+
};
|
|
37
|
+
signal.addEventListener('abort', onAbort);
|
|
38
|
+
const getQueueSize = () => {
|
|
39
|
+
return audioDecoder.decodeQueueSize + outputQueueSize;
|
|
40
|
+
};
|
|
41
|
+
audioDecoder.configure(config);
|
|
42
|
+
const waitForDequeue = async () => {
|
|
43
|
+
await new Promise((r) => {
|
|
44
|
+
dequeueResolver = r;
|
|
45
|
+
// @ts-expect-error exists
|
|
46
|
+
audioDecoder.addEventListener('dequeue', () => r(), {
|
|
47
|
+
once: true,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const waitForFinish = async () => {
|
|
52
|
+
while (getQueueSize() > 0) {
|
|
53
|
+
await waitForDequeue();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const processSample = async (audioSample) => {
|
|
57
|
+
if (audioDecoder.state === 'closed') {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
while (getQueueSize() > 10) {
|
|
61
|
+
await waitForDequeue();
|
|
62
|
+
}
|
|
63
|
+
// Don't flush, it messes up the audio
|
|
64
|
+
const chunk = new EncodedAudioChunk(audioSample);
|
|
65
|
+
audioDecoder.decode(chunk);
|
|
66
|
+
};
|
|
67
|
+
let queue = Promise.resolve();
|
|
68
|
+
return {
|
|
69
|
+
processSample: (sample) => {
|
|
70
|
+
queue = queue.then(() => processSample(sample));
|
|
71
|
+
return queue;
|
|
72
|
+
},
|
|
73
|
+
waitForFinish: async () => {
|
|
74
|
+
await audioDecoder.flush();
|
|
75
|
+
await waitForFinish();
|
|
76
|
+
await outputQueue;
|
|
77
|
+
},
|
|
78
|
+
close,
|
|
79
|
+
getQueueSize,
|
|
80
|
+
flush: async () => {
|
|
81
|
+
await audioDecoder.flush();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
exports.createAudioDecoder = createAudioDecoder;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAudioEncoderConfig = void 0;
|
|
4
|
+
const getAudioEncoderConfig = async (config) => {
|
|
5
|
+
if (typeof AudioEncoder === 'undefined') {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
if ((await AudioEncoder.isConfigSupported(config)).supported) {
|
|
9
|
+
return config;
|
|
10
|
+
}
|
|
11
|
+
return null;
|
|
12
|
+
};
|
|
13
|
+
exports.getAudioEncoderConfig = getAudioEncoderConfig;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="dom-webcodecs" />
|
|
2
|
+
import type { ConvertMediaAudioCodec } from './codec-id';
|
|
3
|
+
export type WebCodecsAudioEncoder = {
|
|
4
|
+
encodeFrame: (audioData: AudioData) => Promise<void>;
|
|
5
|
+
waitForFinish: () => Promise<void>;
|
|
6
|
+
close: () => void;
|
|
7
|
+
getQueueSize: () => number;
|
|
8
|
+
flush: () => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
export declare const createAudioEncoder: ({ onChunk, onError, codec, signal, config: audioEncoderConfig, }: {
|
|
11
|
+
onChunk: (chunk: EncodedAudioChunk) => Promise<void>;
|
|
12
|
+
onError: (error: DOMException) => void;
|
|
13
|
+
codec: ConvertMediaAudioCodec;
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
config: AudioEncoderConfig;
|
|
16
|
+
}) => WebCodecsAudioEncoder;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAudioEncoder = void 0;
|
|
4
|
+
const createAudioEncoder = ({ onChunk, onError, codec, signal, config: audioEncoderConfig, }) => {
|
|
5
|
+
if (signal.aborted) {
|
|
6
|
+
throw new Error('Not creating audio encoder, already aborted');
|
|
7
|
+
}
|
|
8
|
+
let prom = Promise.resolve();
|
|
9
|
+
let outputQueue = 0;
|
|
10
|
+
let dequeueResolver = () => { };
|
|
11
|
+
const encoder = new AudioEncoder({
|
|
12
|
+
output: (chunk) => {
|
|
13
|
+
outputQueue++;
|
|
14
|
+
prom = prom
|
|
15
|
+
.then(() => onChunk(chunk))
|
|
16
|
+
.then(() => {
|
|
17
|
+
outputQueue--;
|
|
18
|
+
dequeueResolver();
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
error(error) {
|
|
23
|
+
onError(error);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
const close = () => {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
28
|
+
signal.removeEventListener('abort', onAbort);
|
|
29
|
+
if (encoder.state === 'closed') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
encoder.close();
|
|
33
|
+
};
|
|
34
|
+
const onAbort = () => {
|
|
35
|
+
close();
|
|
36
|
+
};
|
|
37
|
+
signal.addEventListener('abort', onAbort);
|
|
38
|
+
if (codec !== 'opus') {
|
|
39
|
+
throw new Error('Only `codec: "opus"` is supported currently');
|
|
40
|
+
}
|
|
41
|
+
const getQueueSize = () => {
|
|
42
|
+
return encoder.encodeQueueSize + outputQueue;
|
|
43
|
+
};
|
|
44
|
+
encoder.configure(audioEncoderConfig);
|
|
45
|
+
const waitForDequeue = async () => {
|
|
46
|
+
await new Promise((r) => {
|
|
47
|
+
dequeueResolver = r;
|
|
48
|
+
// @ts-expect-error exists
|
|
49
|
+
encoder.addEventListener('dequeue', () => r(), {
|
|
50
|
+
once: true,
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
const waitForFinish = async () => {
|
|
55
|
+
while (getQueueSize() > 0) {
|
|
56
|
+
await waitForDequeue();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const encodeFrame = async (audioData) => {
|
|
60
|
+
if (encoder.state === 'closed') {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
while (getQueueSize() > 10) {
|
|
64
|
+
await waitForDequeue();
|
|
65
|
+
}
|
|
66
|
+
// @ts-expect-error - can have changed in the meanwhile
|
|
67
|
+
if (encoder.state === 'closed') {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
encoder.encode(audioData);
|
|
71
|
+
};
|
|
72
|
+
let queue = Promise.resolve();
|
|
73
|
+
return {
|
|
74
|
+
encodeFrame: (audioData) => {
|
|
75
|
+
queue = queue.then(() => encodeFrame(audioData));
|
|
76
|
+
return queue;
|
|
77
|
+
},
|
|
78
|
+
waitForFinish: async () => {
|
|
79
|
+
await encoder.flush();
|
|
80
|
+
await waitForFinish();
|
|
81
|
+
await prom;
|
|
82
|
+
},
|
|
83
|
+
close,
|
|
84
|
+
getQueueSize,
|
|
85
|
+
flush: async () => {
|
|
86
|
+
await encoder.flush();
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
exports.createAudioEncoder = createAudioEncoder;
|
package/dist/codec-id.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { VideoTrack } from '@remotion/media-parser';
|
|
2
|
+
import type { ConvertMediaAudioCodec, ConvertMediaVideoCodec } from './codec-id';
|
|
3
|
+
import { type ResolveAudioActionFn } from './resolve-audio-action';
|
|
4
|
+
import { type ResolveVideoActionFn } from './resolve-video-action';
|
|
5
|
+
export type ConvertMediaState = {
|
|
6
|
+
decodedVideoFrames: number;
|
|
7
|
+
decodedAudioFrames: number;
|
|
8
|
+
encodedVideoFrames: number;
|
|
9
|
+
encodedAudioFrames: number;
|
|
10
|
+
};
|
|
11
|
+
export type ConvertMediaTo = 'webm';
|
|
12
|
+
export type ConvertMediaResult = {
|
|
13
|
+
save: () => Promise<File>;
|
|
14
|
+
remove: () => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
export declare const convertMedia: ({ src, onVideoFrame, onMediaStateUpdate, audioCodec, to, videoCodec, signal: userPassedAbortSignal, onAudioTrack: userAudioResolver, onVideoTrack: userVideoResolver, }: {
|
|
17
|
+
src: string | File;
|
|
18
|
+
to: ConvertMediaTo;
|
|
19
|
+
onVideoFrame?: ((inputFrame: VideoFrame, track: VideoTrack) => Promise<void>) | undefined;
|
|
20
|
+
onMediaStateUpdate?: ((state: ConvertMediaState) => void) | undefined;
|
|
21
|
+
videoCodec: ConvertMediaVideoCodec;
|
|
22
|
+
audioCodec: ConvertMediaAudioCodec;
|
|
23
|
+
signal?: AbortSignal | undefined;
|
|
24
|
+
onAudioTrack?: ResolveAudioActionFn | undefined;
|
|
25
|
+
onVideoTrack?: ResolveVideoActionFn | undefined;
|
|
26
|
+
}) => Promise<ConvertMediaResult>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.convertMedia = void 0;
|
|
7
|
+
const media_parser_1 = require("@remotion/media-parser");
|
|
8
|
+
const buffer_1 = require("@remotion/media-parser/buffer");
|
|
9
|
+
const web_fs_1 = require("@remotion/media-parser/web-fs");
|
|
10
|
+
const error_cause_1 = __importDefault(require("./error-cause"));
|
|
11
|
+
const on_audio_track_1 = require("./on-audio-track");
|
|
12
|
+
const on_video_track_1 = require("./on-video-track");
|
|
13
|
+
const resolve_audio_action_1 = require("./resolve-audio-action");
|
|
14
|
+
const resolve_video_action_1 = require("./resolve-video-action");
|
|
15
|
+
const with_resolvers_1 = require("./with-resolvers");
|
|
16
|
+
const convertMedia = async ({ src, onVideoFrame, onMediaStateUpdate, audioCodec, to, videoCodec, signal: userPassedAbortSignal, onAudioTrack: userAudioResolver, onVideoTrack: userVideoResolver, }) => {
|
|
17
|
+
if (to !== 'webm') {
|
|
18
|
+
return Promise.reject(new TypeError('Only `to: "webm"` is supported currently'));
|
|
19
|
+
}
|
|
20
|
+
if (audioCodec !== 'opus') {
|
|
21
|
+
return Promise.reject(new TypeError('Only `audioCodec: "opus"` is supported currently'));
|
|
22
|
+
}
|
|
23
|
+
if (videoCodec !== 'vp8') {
|
|
24
|
+
return Promise.reject(new TypeError('Only `videoCodec: "vp8"` is supported currently'));
|
|
25
|
+
}
|
|
26
|
+
const { promise, resolve, reject } = (0, with_resolvers_1.withResolvers)();
|
|
27
|
+
const controller = new AbortController();
|
|
28
|
+
const abortConversion = (errCause) => {
|
|
29
|
+
reject(errCause);
|
|
30
|
+
if (!controller.signal.aborted) {
|
|
31
|
+
controller.abort();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const onUserAbort = () => {
|
|
35
|
+
abortConversion(new error_cause_1.default('Conversion aborted by user'));
|
|
36
|
+
};
|
|
37
|
+
userPassedAbortSignal === null || userPassedAbortSignal === void 0 ? void 0 : userPassedAbortSignal.addEventListener('abort', onUserAbort);
|
|
38
|
+
const convertMediaState = {
|
|
39
|
+
decodedAudioFrames: 0,
|
|
40
|
+
decodedVideoFrames: 0,
|
|
41
|
+
encodedVideoFrames: 0,
|
|
42
|
+
encodedAudioFrames: 0,
|
|
43
|
+
};
|
|
44
|
+
const canUseWebFs = await (0, web_fs_1.canUseWebFsWriter)();
|
|
45
|
+
const state = await media_parser_1.MediaParserInternals.createMedia(canUseWebFs ? web_fs_1.webFsWriter : buffer_1.bufferWriter);
|
|
46
|
+
const onVideoTrack = (0, on_video_track_1.makeVideoTrackHandler)({
|
|
47
|
+
state,
|
|
48
|
+
onVideoFrame: onVideoFrame !== null && onVideoFrame !== void 0 ? onVideoFrame : null,
|
|
49
|
+
onMediaStateUpdate: onMediaStateUpdate !== null && onMediaStateUpdate !== void 0 ? onMediaStateUpdate : null,
|
|
50
|
+
abortConversion,
|
|
51
|
+
convertMediaState,
|
|
52
|
+
controller,
|
|
53
|
+
videoCodec,
|
|
54
|
+
onVideoTrack: userVideoResolver !== null && userVideoResolver !== void 0 ? userVideoResolver : resolve_video_action_1.defaultResolveVideoAction,
|
|
55
|
+
});
|
|
56
|
+
const onAudioTrack = (0, on_audio_track_1.makeAudioTrackHandler)({
|
|
57
|
+
abortConversion,
|
|
58
|
+
audioCodec,
|
|
59
|
+
controller,
|
|
60
|
+
convertMediaState,
|
|
61
|
+
onMediaStateUpdate: onMediaStateUpdate !== null && onMediaStateUpdate !== void 0 ? onMediaStateUpdate : null,
|
|
62
|
+
state,
|
|
63
|
+
onAudioTrack: userAudioResolver !== null && userAudioResolver !== void 0 ? userAudioResolver : resolve_audio_action_1.defaultResolveAudioAction,
|
|
64
|
+
bitrate: 128000,
|
|
65
|
+
});
|
|
66
|
+
(0, media_parser_1.parseMedia)({
|
|
67
|
+
src,
|
|
68
|
+
onVideoTrack,
|
|
69
|
+
onAudioTrack,
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
})
|
|
72
|
+
.then(() => {
|
|
73
|
+
return state.waitForFinish();
|
|
74
|
+
})
|
|
75
|
+
.then(() => {
|
|
76
|
+
resolve({ save: state.save, remove: state.remove });
|
|
77
|
+
})
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
reject(err);
|
|
80
|
+
})
|
|
81
|
+
.finally(() => {
|
|
82
|
+
userPassedAbortSignal === null || userPassedAbortSignal === void 0 ? void 0 : userPassedAbortSignal.removeEventListener('abort', onUserAbort);
|
|
83
|
+
});
|
|
84
|
+
return promise;
|
|
85
|
+
};
|
|
86
|
+
exports.convertMedia = convertMedia;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAudioDecoder = void 0;
|
|
4
|
+
const createAudioDecoder = ({ onProgress, onFrame, }) => {
|
|
5
|
+
let decodedFrames = 0;
|
|
6
|
+
const decoder = new AudioDecoder({
|
|
7
|
+
output(inputFrame) {
|
|
8
|
+
onFrame(inputFrame);
|
|
9
|
+
inputFrame.close();
|
|
10
|
+
onProgress(++decodedFrames);
|
|
11
|
+
},
|
|
12
|
+
error(error) {
|
|
13
|
+
console.error(error);
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
return { decoder };
|
|
17
|
+
};
|
|
18
|
+
exports.createAudioDecoder = createAudioDecoder;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDecoder = void 0;
|
|
4
|
+
const createDecoder = ({ onProgress, onFrame, }) => {
|
|
5
|
+
let decodedFrames = 0;
|
|
6
|
+
let nextKeyFrameTimestamp = 0;
|
|
7
|
+
const decoder = new VideoDecoder({
|
|
8
|
+
async output(inputFrame) {
|
|
9
|
+
const bitmap = await createImageBitmap(inputFrame);
|
|
10
|
+
const keyFrameEveryHowManySeconds = 2;
|
|
11
|
+
let keyFrame = false;
|
|
12
|
+
if (inputFrame.timestamp >= nextKeyFrameTimestamp) {
|
|
13
|
+
keyFrame = true;
|
|
14
|
+
nextKeyFrameTimestamp =
|
|
15
|
+
inputFrame.timestamp + keyFrameEveryHowManySeconds * 1e6;
|
|
16
|
+
}
|
|
17
|
+
const outputFrame = new VideoFrame(bitmap, {
|
|
18
|
+
timestamp: inputFrame.timestamp,
|
|
19
|
+
duration: inputFrame.duration,
|
|
20
|
+
});
|
|
21
|
+
onFrame(outputFrame, keyFrame);
|
|
22
|
+
inputFrame.close();
|
|
23
|
+
decodedFrames++;
|
|
24
|
+
onProgress(decodedFrames);
|
|
25
|
+
},
|
|
26
|
+
error(error) {
|
|
27
|
+
console.error(error);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
return { decoder };
|
|
31
|
+
};
|
|
32
|
+
exports.createDecoder = createDecoder;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference path="../src/types/mp4box.d.ts" />
|
|
2
|
+
export declare const createEncoder: ({ width, height, onProgress, }: {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
onProgress: (encoded: number) => void;
|
|
6
|
+
}) => {
|
|
7
|
+
encoder: VideoEncoder;
|
|
8
|
+
outputMp4: import("mp4box").MP4File;
|
|
9
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEncoder = void 0;
|
|
4
|
+
const mp4box_1 = require("mp4box");
|
|
5
|
+
const timescale = 90000;
|
|
6
|
+
const createEncoder = ({ width, height, onProgress, }) => {
|
|
7
|
+
let trackID = null;
|
|
8
|
+
let encodedFrames = 0;
|
|
9
|
+
const outputMp4 = (0, mp4box_1.createFile)();
|
|
10
|
+
const encoder = new VideoEncoder({
|
|
11
|
+
output(chunk, metadata) {
|
|
12
|
+
const uint8 = new Uint8Array(chunk.byteLength);
|
|
13
|
+
chunk.copyTo(uint8);
|
|
14
|
+
if (trackID === null) {
|
|
15
|
+
trackID = outputMp4.addTrack({
|
|
16
|
+
width,
|
|
17
|
+
height,
|
|
18
|
+
timescale,
|
|
19
|
+
avcDecoderConfigRecord: metadata.decoderConfig.description,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (chunk.duration === null) {
|
|
23
|
+
throw new Error('No duration in the chunk');
|
|
24
|
+
}
|
|
25
|
+
const sampleDuration = chunk.duration / (1000000 / timescale);
|
|
26
|
+
outputMp4.addSample(trackID, uint8, {
|
|
27
|
+
duration: sampleDuration,
|
|
28
|
+
is_sync: chunk.type === 'key',
|
|
29
|
+
});
|
|
30
|
+
encodedFrames++;
|
|
31
|
+
onProgress(encodedFrames);
|
|
32
|
+
},
|
|
33
|
+
error(error) {
|
|
34
|
+
console.error(error);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
encoder.configure({
|
|
38
|
+
codec: 'avc1.4d0034',
|
|
39
|
+
width,
|
|
40
|
+
height,
|
|
41
|
+
hardwareAcceleration: 'prefer-hardware',
|
|
42
|
+
bitrate: 20000000,
|
|
43
|
+
});
|
|
44
|
+
return { encoder, outputMp4 };
|
|
45
|
+
};
|
|
46
|
+
exports.createEncoder = createEncoder;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createVideoDecoder = void 0;
|
|
4
|
+
const createVideoDecoder = ({ onProgress, onFrame, }) => {
|
|
5
|
+
let decodedFrames = 0;
|
|
6
|
+
const decoder = new VideoDecoder({
|
|
7
|
+
output(inputFrame) {
|
|
8
|
+
onFrame(inputFrame);
|
|
9
|
+
inputFrame.close();
|
|
10
|
+
onProgress(++decodedFrames);
|
|
11
|
+
},
|
|
12
|
+
error(error) {
|
|
13
|
+
console.error(error);
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
return { decoder };
|
|
17
|
+
};
|
|
18
|
+
exports.createVideoDecoder = createVideoDecoder;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VideoSample, VideoTrack } from '@remotion/media-parser';
|
|
2
|
+
export declare const createDecoder: ({ track, onFrame, }: {
|
|
3
|
+
track: VideoTrack;
|
|
4
|
+
onFrame: (frame: VideoFrame) => Promise<void>;
|
|
5
|
+
}) => Promise<{
|
|
6
|
+
processSample: (sample: VideoSample) => Promise<void>;
|
|
7
|
+
} | null>;
|
package/dist/decoder.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDecoder = void 0;
|
|
4
|
+
const wait_for_dequeue_1 = require("./wait-for-dequeue");
|
|
5
|
+
const createDecoder = async ({ track, onFrame, }) => {
|
|
6
|
+
if (typeof VideoDecoder === 'undefined') {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const actualConfig = {
|
|
10
|
+
...track,
|
|
11
|
+
hardwareAcceleration: 'prefer-hardware',
|
|
12
|
+
};
|
|
13
|
+
const { supported } = await VideoDecoder.isConfigSupported(actualConfig);
|
|
14
|
+
if (!supported) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const videoDecoder = new VideoDecoder({
|
|
18
|
+
async output(inputFrame) {
|
|
19
|
+
await onFrame(inputFrame);
|
|
20
|
+
},
|
|
21
|
+
error(error) {
|
|
22
|
+
console.error(error);
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
videoDecoder.configure(actualConfig);
|
|
26
|
+
const processSample = async (sample) => {
|
|
27
|
+
if (videoDecoder.state === 'closed') {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
await (0, wait_for_dequeue_1.decoderWaitForDequeue)(videoDecoder);
|
|
31
|
+
if (sample.type === 'key') {
|
|
32
|
+
await videoDecoder.flush();
|
|
33
|
+
}
|
|
34
|
+
videoDecoder.decode(new EncodedVideoChunk(sample));
|
|
35
|
+
};
|
|
36
|
+
let queue = Promise.resolve();
|
|
37
|
+
return {
|
|
38
|
+
processSample: (sample) => {
|
|
39
|
+
queue = queue.then(() => processSample(sample));
|
|
40
|
+
return queue;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
exports.createDecoder = createDecoder;
|