Skip to content

🎥 Make videos programmatically with React

nice progress

Jonny Burgerc573cfbe8314
diff --git a/packages/example/src/Encoder/SrcEncoder.tsx b/packages/example/src/Encoder/SrcEncoder.tsx
index 42cb883847..78d95aaa26 100644
--- a/packages/example/src/Encoder/SrcEncoder.tsx
+++ b/packages/example/src/Encoder/SrcEncoder.tsx
@@ -35,10 +35,9 @@ const SampleLabel: React.FC<{
const SampleCount: React.FC<{
count: number;
label: string;
- errored: boolean;
-}> = ({count, label, errored}) => {
+}> = ({count, label}) => {
return (
- <div style={{display: 'inline-block', color: errored ? 'red' : 'white'}}>
+ <div style={{display: 'inline-block', color: 'white'}}>
<SampleLabel>{label}</SampleLabel>
{count}
</div>
@@ -54,11 +53,10 @@ export const SrcEncoder: React.FC<{
decodedVideoFrames: 0,
encodedVideoFrames: 0,
encodedAudioFrames: 0,
- audioError: null,
- videoError: null,
});
const [downloadFn, setDownloadFn] = useState<null | (() => void)>(null);
+ const [abortfn, setAbortFn] = useState<null | (() => void)>(null);
const ref = useRef<HTMLCanvasElement>(null);
@@ -127,6 +125,8 @@ export const SrcEncoder: React.FC<{
const onClick = useCallback(async () => {
try {
+ const abortController = new AbortController();
+ setAbortFn(() => () => abortController.abort());
const fn = await convertMedia({
src,
onVideoFrame,
@@ -138,19 +138,14 @@ export const SrcEncoder: React.FC<{
videoCodec: 'vp8',
audioCodec: 'opus',
to: 'webm',
+ signal: abortController.signal,
});
setDownloadFn(() => fn.save);
- } catch (err) {}
+ } catch (err) {
+ console.log(err);
+ }
}, [onVideoFrame, src]);
- if (state.audioError) {
- console.log(state.audioError);
- }
-
- if (state.videoError) {
- console.log(state.videoError);
- }
-
return (
<div
style={{
@@ -191,9 +186,25 @@ export const SrcEncoder: React.FC<{
>
{label}{' '}
</div>
- <button type="button" onClick={onClick}>
- Decode
- </button>
+
+ {downloadFn ? (
+ <button type="button" onClick={downloadFn}>
+ Download
+ </button>
+ ) : abortfn ? (
+ <button
+ type="button"
+ onClick={() => {
+ abortfn();
+ }}
+ >
+ Abort
+ </button>
+ ) : (
+ <button type="button" onClick={onClick}>
+ Decode
+ </button>
+ )}
<div
style={{
display: 'flex',
@@ -204,33 +215,11 @@ export const SrcEncoder: React.FC<{
height: 38,
}}
>
- <SampleCount
- errored={state.videoError !== null}
- count={state.decodedVideoFrames}
- label="VD"
- />
- <SampleCount
- errored={state.videoError !== null}
- count={state.encodedVideoFrames}
- label="VE"
- />
- <SampleCount
- errored={state.audioError !== null}
- count={state.decodedAudioFrames}
- label="AD"
- />
- <SampleCount
- errored={state.audioError !== null}
- count={state.encodedAudioFrames}
- label="AE"
- />
+ <SampleCount count={state.decodedVideoFrames} label="VD" />
+ <SampleCount count={state.encodedVideoFrames} label="VE" />
+ <SampleCount count={state.decodedAudioFrames} label="AD" />
+ <SampleCount count={state.encodedAudioFrames} label="AE" />
<br />
-
- {downloadFn ? (
- <button type="button" onClick={downloadFn}>
- DL
- </button>
- ) : null}
</div>
</AbsoluteFill>
</div>
diff --git a/packages/webcodecs/src/convert-media.ts b/packages/webcodecs/src/convert-media.ts
index 768a29ff0f..acb5163b17 100644
--- a/packages/webcodecs/src/convert-media.ts
+++ b/packages/webcodecs/src/convert-media.ts
@@ -38,6 +38,7 @@ export const convertMedia = async ({
audioCodec,
to,
videoCodec,
+ signal: userPassedAbortSignal,
}: {
src: string | File;
onVideoFrame?: (inputFrame: VideoFrame, track: VideoTrack) => Promise<void>;
@@ -45,6 +46,7 @@ export const convertMedia = async ({
videoCodec: ConvertMediaVideoCodec;
audioCodec: ConvertMediaAudioCodec;
to: ConvertMediaTo;
+ signal?: AbortSignal;
}): Promise<ConvertMediaResult> => {
if (to !== 'webm') {
return Promise.reject(
@@ -65,7 +67,7 @@ export const convertMedia = async ({
}
const {promise, resolve, reject} = withResolvers<ConvertMediaResult>();
- const {signal, abort} = new AbortController();
+ const controller = new AbortController();
const abortConversion = (errCause: Error | null) => {
if (errCause === null) {
@@ -78,9 +80,17 @@ export const convertMedia = async ({
);
}
- abort();
+ if (!controller.signal.aborted) {
+ controller.abort();
+ }
+ };
+
+ const onUserAbort = () => {
+ abortConversion(new Error('Conversion aborted by user'));
};
+ userPassedAbortSignal?.addEventListener('abort', onUserAbort);
+
const convertMediaState: ConvertMediaState = {
decodedAudioFrames: 0,
decodedVideoFrames: 0,
@@ -125,10 +135,14 @@ export const convertMedia = async ({
),
);
},
- signal,
+ signal: controller.signal,
});
if (videoEncoder === null) {
- onMediaStateUpdate?.({...convertMediaState});
+ abortConversion(
+ new Error(
+ `Could not configure video encoder of track ${track.trackId}`,
+ ),
+ );
return null;
}
@@ -151,10 +165,14 @@ export const convertMedia = async ({
),
);
},
- signal,
+ signal: controller.signal,
});
if (videoDecoder === null) {
- onMediaStateUpdate?.({...convertMediaState});
+ abortConversion(
+ new Error(
+ `Could not configure video decoder of track ${track.trackId}`,
+ ),
+ );
return null;
}
@@ -197,11 +215,15 @@ export const convertMedia = async ({
);
},
codec: audioCodec,
- signal,
+ signal: controller.signal,
});
if (!audioEncoder) {
- onMediaStateUpdate?.({...convertMediaState});
+ abortConversion(
+ new Error(
+ `Could not configure audio encoder of track ${track.trackId}`,
+ ),
+ );
return null;
}
@@ -224,11 +246,15 @@ export const convertMedia = async ({
),
);
},
- signal,
+ signal: controller.signal,
});
if (!audioDecoder) {
- onMediaStateUpdate?.(convertMediaState);
+ abortConversion(
+ new Error(
+ `Could not configure audio decoder of track ${track.trackId}`,
+ ),
+ );
return null;
}
@@ -257,6 +283,9 @@ export const convertMedia = async ({
})
.catch((err) => {
reject(err);
+ })
+ .finally(() => {
+ userPassedAbortSignal?.removeEventListener('abort', onUserAbort);
});
return promise;