Skip to content

🎥 Make videos programmatically with React

allow to drop audio / video if not supported

Jonny Burger94695fffb44c
diff --git a/packages/webcodecs/src/convert-media.ts b/packages/webcodecs/src/convert-media.ts
index 448b23fc31..f61ceea8a8 100644
--- a/packages/webcodecs/src/convert-media.ts
+++ b/packages/webcodecs/src/convert-media.ts
@@ -17,6 +17,8 @@ import {
type ConvertMediaVideoCodec,
} from './codec-id';
import Error from './error-cause';
+import {resolveAudioAction} from './resolve-audio-action';
+import {resolveVideoAction} from './resolve-video-action';
import {createVideoDecoder} from './video-decoder';
import {getVideoDecoderConfigWithHardwareAcceleration} from './video-decoder-config';
import {createVideoEncoder} from './video-encoder';
@@ -37,12 +39,6 @@ export type ConvertMediaResult = {
remove: () => Promise<void>;
};
-type AudioMode = 'reencode' | 'copy' | 'drop';
-type VideoMode = 'reencode' | 'copy' | 'drop';
-// TODO: Unhardcode
-const AUDIO_MODE: AudioMode = 'reencode' as AudioMode;
-const VIDEO_MODE: VideoMode = 'reencode' as VideoMode;
-
export const convertMedia = async ({
src,
onVideoFrame,
@@ -116,12 +112,16 @@ export const convertMedia = async ({
});
const videoDecoderConfig =
await getVideoDecoderConfigWithHardwareAcceleration(track);
+ const videoOperation = await resolveVideoAction({
+ videoDecoderConfig,
+ videoEncoderConfig,
+ });
- if (VIDEO_MODE === 'drop') {
+ if (videoOperation === 'drop') {
return null;
}
- if (VIDEO_MODE === 'copy') {
+ if (videoOperation === 'copy') {
// TODO: Copy over video track
const videoTrack = await state.addTrack({
@@ -235,7 +235,7 @@ export const convertMedia = async ({
const onAudioTrack: OnAudioTrack = async (track) => {
const audioEncoderConfig = await getAudioEncoderConfig({
- codec: 'opus',
+ codec: audioCodec,
numberOfChannels: track.numberOfChannels,
sampleRate: track.sampleRate,
bitrate: 128000,
@@ -247,11 +247,16 @@ export const convertMedia = async ({
description: track.description,
});
- if (AUDIO_MODE === 'drop') {
+ const audioOperation = await resolveAudioAction({
+ audioDecoderConfig,
+ audioEncoderConfig,
+ });
+
+ if (audioOperation === 'drop') {
return null;
}
- if (AUDIO_MODE === 'copy') {
+ if (audioOperation === 'copy') {
const addedTrack = await state.addTrack({
type: 'audio',
codecId: codecNameToMatroskaAudioCodecId(audioCodec),
diff --git a/packages/webcodecs/src/resolve-audio-action.ts b/packages/webcodecs/src/resolve-audio-action.ts
new file mode 100644
index 0000000000..1c39f438d4
--- /dev/null
+++ b/packages/webcodecs/src/resolve-audio-action.ts
@@ -0,0 +1,16 @@
+export type AudioOperation = 'reencode' | 'copy' | 'drop';
+
+export const resolveAudioAction = ({
+ audioDecoderConfig,
+ audioEncoderConfig,
+}: {
+ audioDecoderConfig: AudioDecoderConfig | null;
+ audioEncoderConfig: AudioEncoderConfig | null;
+}): Promise<AudioOperation> => {
+ // TODO: Unhardcode and allow user to config
+ if (audioDecoderConfig === null || audioEncoderConfig === null) {
+ return Promise.resolve('drop');
+ }
+
+ return Promise.resolve('reencode');
+};
diff --git a/packages/webcodecs/src/resolve-video-action.ts b/packages/webcodecs/src/resolve-video-action.ts
new file mode 100644
index 0000000000..df5cc9f033
--- /dev/null
+++ b/packages/webcodecs/src/resolve-video-action.ts
@@ -0,0 +1,16 @@
+export type VideoOperation = 'reencode' | 'copy' | 'drop';
+
+export const resolveVideoAction = ({
+ videoDecoderConfig,
+ videoEncoderConfig,
+}: {
+ videoDecoderConfig: VideoDecoderConfig | null;
+ videoEncoderConfig: VideoEncoderConfig | null;
+}): Promise<VideoOperation> => {
+ // TODO: Unhardcode and allow user to config
+ if (videoDecoderConfig === null || videoEncoderConfig === null) {
+ return Promise.resolve('drop');
+ }
+
+ return Promise.resolve('reencode');
+};