Skip to content

🎥 Make videos programmatically with React

convert media

Jonny Burger07f297eccad2
diff --git a/packages/webcodecs/src/convert-media.ts b/packages/webcodecs/src/convert-media.ts
index 793f8a79f7..d27005f19e 100644
--- a/packages/webcodecs/src/convert-media.ts
+++ b/packages/webcodecs/src/convert-media.ts
@@ -17,6 +17,7 @@ import {
import Error from './error-cause';
import {createVideoDecoder} from './video-decoder';
import {createVideoEncoder} from './video-encoder';
+import {getVideoEncoderConfig} from './video-encoder-config';
import {withResolvers} from './with-resolvers';
export type ConvertMediaState = {
@@ -116,9 +117,22 @@ export const convertMedia = async ({
codecId: codecNameToMatroskaCodecId(videoCodec),
});
- const videoEncoder = await createVideoEncoder({
- width: track.displayAspectWidth,
+ const videoEncoderConfig = await getVideoEncoderConfig({
+ codec: 'vp8',
height: track.displayAspectHeight,
+ width: track.displayAspectWidth,
+ });
+
+ if (videoEncoderConfig === null) {
+ abortConversion(
+ new Error(
+ `Could not configure video encoder of track ${track.trackId}`,
+ ),
+ );
+ return null;
+ }
+
+ const videoEncoder = createVideoEncoder({
onChunk: async (chunk) => {
await state.addSample(chunk, trackNumber);
const newDuration = Math.round(
@@ -139,15 +153,8 @@ export const convertMedia = async ({
);
},
signal: controller.signal,
+ config: videoEncoderConfig,
});
- if (videoEncoder === null) {
- abortConversion(
- new Error(
- `Could not configure video encoder of track ${track.trackId}`,
- ),
- );
- return null;
- }
const videoDecoder = await createVideoDecoder({
track,
@@ -170,6 +177,7 @@ export const convertMedia = async ({
},
signal: controller.signal,
});
+
if (videoDecoder === null) {
abortConversion(
new Error(
diff --git a/packages/webcodecs/src/get-config.ts b/packages/webcodecs/src/get-config.ts
index 8ed21577e1..e6f8486256 100644
--- a/packages/webcodecs/src/get-config.ts
+++ b/packages/webcodecs/src/get-config.ts
@@ -21,27 +21,3 @@ export const getVideoDecoderConfigWithHardwareAcceleration = async (
return null;
};
-
-export const getVideoEncoderConfigWithHardwareAcceleration = async (
- config: VideoEncoderConfig,
-): Promise<VideoEncoderConfig | null> => {
- const hardware: VideoEncoderConfig = {
- ...config,
- hardwareAcceleration: 'prefer-hardware',
- };
-
- if ((await VideoEncoder.isConfigSupported(hardware)).supported) {
- return hardware;
- }
-
- const software: VideoEncoderConfig = {
- ...config,
- hardwareAcceleration: 'prefer-software',
- };
-
- if ((await VideoEncoder.isConfigSupported(software)).supported) {
- return software;
- }
-
- return null;
-};
diff --git a/packages/webcodecs/src/video-encoder-config.ts b/packages/webcodecs/src/video-encoder-config.ts
new file mode 100644
index 0000000000..534c0b635e
--- /dev/null
+++ b/packages/webcodecs/src/video-encoder-config.ts
@@ -0,0 +1,27 @@
+export const getVideoEncoderConfig = async (
+ config: VideoEncoderConfig,
+): Promise<VideoEncoderConfig | null> => {
+ if (typeof VideoEncoder === 'undefined') {
+ return null;
+ }
+
+ const hardware: VideoEncoderConfig = {
+ ...config,
+ hardwareAcceleration: 'prefer-hardware',
+ };
+
+ if ((await VideoEncoder.isConfigSupported(hardware)).supported) {
+ return hardware;
+ }
+
+ const software: VideoEncoderConfig = {
+ ...config,
+ hardwareAcceleration: 'prefer-software',
+ };
+
+ if ((await VideoEncoder.isConfigSupported(software)).supported) {
+ return software;
+ }
+
+ return null;
+};
diff --git a/packages/webcodecs/src/video-encoder.ts b/packages/webcodecs/src/video-encoder.ts
index 56d92f1023..0e4b44dfb7 100644
--- a/packages/webcodecs/src/video-encoder.ts
+++ b/packages/webcodecs/src/video-encoder.ts
@@ -1,5 +1,3 @@
-import {getVideoEncoderConfigWithHardwareAcceleration} from './get-config';
-
export type WebCodecsVideoEncoder = {
encodeFrame: (videoFrame: VideoFrame) => Promise<void>;
waitForFinish: () => Promise<void>;
@@ -8,25 +6,19 @@ export type WebCodecsVideoEncoder = {
flush: () => Promise<void>;
};
-export const createVideoEncoder = async ({
- width,
- height,
+export const createVideoEncoder = ({
onChunk,
onError,
signal,
+ config,
}: {
- width: number;
- height: number;
onChunk: (chunk: EncodedVideoChunk) => Promise<void>;
onError: (error: DOMException) => void;
signal: AbortSignal;
-}): Promise<WebCodecsVideoEncoder | null> => {
- if (typeof VideoEncoder === 'undefined') {
- return Promise.resolve(null);
- }
-
+ config: VideoEncoderConfig;
+}): WebCodecsVideoEncoder => {
if (signal.aborted) {
- return Promise.resolve(null);
+ throw new Error('Not creating video encoder, already aborted');
}
let outputQueue = Promise.resolve();
@@ -61,16 +53,6 @@ export const createVideoEncoder = async ({
signal.addEventListener('abort', onAbort);
- const config = await getVideoEncoderConfigWithHardwareAcceleration({
- codec: 'vp8',
- height,
- width,
- });
-
- if (!config) {
- return null;
- }
-
const getQueueSize = () => {
return encoder.encodeQueueSize + outputQueueSize;
};