Skip to content

🎥 Make videos programmatically with React

yayyy

Jonny Burger63f1aaf7a09a
diff --git a/packages/media-parser/src/make-hvc1-codec-strings.ts b/packages/media-parser/src/make-hvc1-codec-strings.ts
index 24e35a241f..2acd453c4f 100644
--- a/packages/media-parser/src/make-hvc1-codec-strings.ts
+++ b/packages/media-parser/src/make-hvc1-codec-strings.ts
@@ -20,12 +20,12 @@ export const getHvc1CodecString = (data: BufferIterator) => {
const generalConstraintIndicator = data.getSlice(6);
const generalLevelIdc = data.getUint8();
- let reversedGeneralProfileSpace = 0;
+ let profileId = 0;
for (let i = 0; i < 32; i++) {
- reversedGeneralProfileSpace |= generalProfileCompatibility & 1;
+ profileId |= generalProfileCompatibility & 1;
if (i === 31) break;
- reversedGeneralProfileSpace <<= 1;
+ profileId <<= 1;
generalProfileCompatibility >>= 1;
}
@@ -51,5 +51,5 @@ export const getHvc1CodecString = (data: BufferIterator) => {
}
}
- return `${profileSpaceChar}${generalProfileIdc.toString(16)}.${reversedGeneralProfileSpace.toString(16)}.${generalTierChar}${generalLevelIdc}.${generalConstraintString}`;
+ return `${profileSpaceChar}${generalProfileIdc.toString(16)}.${profileId.toString(16)}.${generalTierChar}${generalLevelIdc}.${generalConstraintString}`;
};
diff --git a/packages/webcodecs/src/choose-correct-hevc-profile.ts b/packages/webcodecs/src/choose-correct-hevc-profile.ts
new file mode 100644
index 0000000000..2934103f94
--- /dev/null
+++ b/packages/webcodecs/src/choose-correct-hevc-profile.ts
@@ -0,0 +1,58 @@
+import {hevcLevels} from './hevc-levels';
+
+export const chooseCorrectHevcProfile = ({
+ width,
+ height,
+ fps,
+}: {
+ width: number;
+ height: number;
+ fps: number | null;
+}) => {
+ const profile = hevcLevels.find((p) => {
+ return p.maxResolutionsAndFrameRates.some((max) => {
+ if (width > max.width) {
+ return false;
+ }
+
+ if (height > max.height) {
+ return false;
+ }
+
+ // if has no fps, use 60 as a conservative fallback
+ const fallbackFps = fps ?? 60;
+ return fallbackFps <= max.fps;
+ });
+ });
+
+ if (!profile) {
+ throw new Error(
+ `No suitable HEVC profile found for ${width}x${height}@${fps}fps`,
+ );
+ }
+
+ // HEVC codec string format: hev1.2.${level_hex} or hvc1.2.${level_hex}
+ // We'll use hvc1 as it's more widely supported
+ return `hvc1.${
+ // Profile
+ // 1 = Main
+ // 2 = Main 10
+ // Chrome seems to support only Main
+ 1
+ }.${
+ // Profile space
+ // Unclear which value to set, but 0 works
+ 0
+ }.${
+ // L = Main tier
+ // H = High tier
+ // TODO: Select high tier if resolution is big
+ 'L'
+ }${
+ // Level
+ Number(profile.level) * 30
+ }.${
+ // Bit depth
+ 'b0'
+ }`;
+};
diff --git a/packages/webcodecs/src/get-codec-string.ts b/packages/webcodecs/src/get-codec-string.ts
new file mode 100644
index 0000000000..45a9fb958c
--- /dev/null
+++ b/packages/webcodecs/src/get-codec-string.ts
@@ -0,0 +1,33 @@
+import {chooseCorrectAvc1Profile} from './choose-correct-avc1-profile';
+import {chooseCorrectHevcProfile} from './choose-correct-hevc-profile';
+import type {ConvertMediaVideoCodec} from './get-available-video-codecs';
+
+export const getCodecStringForEncoder = ({
+ codec,
+ fps,
+ height,
+ width,
+}: {
+ codec: ConvertMediaVideoCodec;
+ fps: number | null;
+ height: number;
+ width: number;
+}) => {
+ if (codec === 'h264') {
+ return chooseCorrectAvc1Profile({fps, height, width});
+ }
+
+ if (codec === 'h265') {
+ return chooseCorrectHevcProfile({fps, height, width});
+ }
+
+ if (codec === 'vp8') {
+ return 'vp8';
+ }
+
+ if (codec === 'vp9') {
+ return 'vp09.00.10.08';
+ }
+
+ throw new Error(`Unknown codec: ${codec satisfies never}`);
+};
diff --git a/packages/webcodecs/src/hevc-levels.ts b/packages/webcodecs/src/hevc-levels.ts
new file mode 100644
index 0000000000..122e531f13
--- /dev/null
+++ b/packages/webcodecs/src/hevc-levels.ts
@@ -0,0 +1,243 @@
+type MaxRes = {
+ width: number;
+ height: number;
+ fps: number;
+};
+
+interface HEVCLevel {
+ level: string;
+ maxBitrateMainTier: number;
+ maxBitrateHighTier: number | null;
+ maxResolutionsAndFrameRates: MaxRes[];
+}
+
+export const hevcLevels: HEVCLevel[] = [
+ {
+ level: '3.1',
+ maxBitrateMainTier: 10000,
+ maxBitrateHighTier: null,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 720,
+ height: 480,
+ fps: 84.3,
+ },
+ {
+ width: 720,
+ height: 576,
+ fps: 75,
+ },
+ {
+ width: 960,
+ height: 540,
+ fps: 60,
+ },
+ {
+ width: 1280,
+ height: 720,
+ fps: 33.7,
+ },
+ ],
+ },
+ {
+ level: '4',
+ maxBitrateMainTier: 12000,
+ maxBitrateHighTier: 30000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 1280,
+ height: 720,
+ fps: 68,
+ },
+ {
+ width: 1920,
+ height: 1080,
+ fps: 32,
+ },
+ {
+ width: 2048,
+ height: 1080,
+ fps: 30,
+ },
+ ],
+ },
+ {
+ level: '4.1',
+ maxBitrateMainTier: 20000,
+ maxBitrateHighTier: 50000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 1280,
+ height: 720,
+ fps: 136,
+ },
+ {
+ width: 1920,
+ height: 1080,
+ fps: 64,
+ },
+ {
+ width: 2048,
+ height: 1080,
+ fps: 60,
+ },
+ ],
+ },
+ {
+ level: '5',
+ maxBitrateMainTier: 25000,
+ maxBitrateHighTier: 100000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 1920,
+ height: 1080,
+ fps: 128,
+ },
+ {
+ width: 2048,
+ height: 1080,
+ fps: 120,
+ },
+ {
+ width: 3840,
+ height: 2160,
+ fps: 32,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 30,
+ },
+ ],
+ },
+ {
+ level: '5.1',
+ maxBitrateMainTier: 40000,
+ maxBitrateHighTier: 160000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 1920,
+ height: 1080,
+ fps: 256,
+ },
+ {
+ width: 2048,
+ height: 1080,
+ fps: 240,
+ },
+ {
+ width: 3840,
+ height: 2160,
+ fps: 64,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 60,
+ },
+ ],
+ },
+ {
+ level: '5.2',
+ maxBitrateMainTier: 60000,
+ maxBitrateHighTier: 240000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 2048,
+ height: 1080,
+ fps: 300,
+ },
+ {
+ width: 3840,
+ height: 2160,
+ fps: 128,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 120,
+ },
+ ],
+ },
+ {
+ level: '6',
+ maxBitrateMainTier: 60000,
+ maxBitrateHighTier: 240000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 3840,
+ height: 2160,
+ fps: 128,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 120,
+ },
+ {
+ width: 7680,
+ height: 4320,
+ fps: 32,
+ },
+ {
+ width: 8192,
+ height: 4320,
+ fps: 30,
+ },
+ ],
+ },
+ {
+ level: '6.1',
+ maxBitrateMainTier: 120000,
+ maxBitrateHighTier: 480000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 3840,
+ height: 2160,
+ fps: 256,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 240,
+ },
+ {
+ width: 7680,
+ height: 4320,
+ fps: 64,
+ },
+ {
+ width: 8192,
+ height: 4320,
+ fps: 60,
+ },
+ ],
+ },
+ {
+ level: '6.2',
+ maxBitrateMainTier: 240000,
+ maxBitrateHighTier: 800000,
+ maxResolutionsAndFrameRates: [
+ {
+ width: 3840,
+ height: 2160,
+ fps: 512,
+ },
+ {
+ width: 4096,
+ height: 2160,
+ fps: 480,
+ },
+ {
+ width: 7680,
+ height: 4320,
+ fps: 128,
+ },
+ {
+ width: 8192,
+ height: 4320,
+ fps: 120,
+ },
+ ],
+ },
+];
diff --git a/packages/webcodecs/src/video-encoder-config.ts b/packages/webcodecs/src/video-encoder-config.ts
index d5132b173b..53891552e7 100644
--- a/packages/webcodecs/src/video-encoder-config.ts
+++ b/packages/webcodecs/src/video-encoder-config.ts
@@ -1,6 +1,6 @@
import {isSafari} from './browser-quirks';
-import {chooseCorrectAvc1Profile} from './choose-correct-avc1-profile';
import type {ConvertMediaVideoCodec} from './get-available-video-codecs';
+import {getCodecStringForEncoder} from './get-codec-string';
export const getVideoEncoderConfig = async ({
codec,
@@ -18,17 +18,13 @@ export const getVideoEncoderConfig = async ({
}
const config: VideoEncoderConfig = {
- codec:
- codec === 'h264'
- ? chooseCorrectAvc1Profile({fps, height, width})
- : codec === 'vp9'
- ? 'vp09.00.10.08'
- : codec,
+ codec: getCodecStringForEncoder({codec, fps, height, width}),
height,
width,
bitrate: isSafari() ? 3_000_000 : undefined,
- bitrateMode: codec === 'vp9' ? 'quantizer' : undefined,
+ bitrateMode: codec === 'vp9' && !isSafari() ? 'quantizer' : undefined,
};
+ console.log(config);
const hardware: VideoEncoderConfig = {
...config,