Skip to content

🎥 Make videos programmatically with React

error state

Jonny Burger75611c20768e
diff --git a/packages/convert/app/components/ErrorState.tsx b/packages/convert/app/components/ErrorState.tsx
index 0d228f094a..7f8ed59bef 100644
--- a/packages/convert/app/components/ErrorState.tsx
+++ b/packages/convert/app/components/ErrorState.tsx
@@ -5,7 +5,7 @@ const ErrorBox: React.FC<{
readonly cause?: boolean;
}> = ({error, cause}) => {
const {message} = error;
- const splitted = error.stack?.split('\n') ?? [];
+ const splitted = error.stack ? error.stack.split('\n') : [error.message];
const deduplicated = splitted[0].includes(message)
? splitted.slice(1)
: splitted;
diff --git a/packages/media-parser/src/add-avc-profile-to-track.ts b/packages/media-parser/src/add-avc-profile-to-track.ts
index bd336eada1..33b8c8471c 100644
--- a/packages/media-parser/src/add-avc-profile-to-track.ts
+++ b/packages/media-parser/src/add-avc-profile-to-track.ts
@@ -1,9 +1,10 @@
-import type {AvcProfileInfo} from './boxes/avc/parse-avc';
+import {combineUint8Arrays, serializeUint16} from './boxes/webm/make-header';
import type {VideoTrack} from './get-tracks';
+import type {SpsAndPps} from './parser-state';
export const addAvcProfileToTrack = (
track: VideoTrack,
- avc1Profile: AvcProfileInfo | null,
+ avc1Profile: SpsAndPps | null,
): VideoTrack => {
if (avc1Profile === null) {
return track;
@@ -11,6 +12,27 @@ export const addAvcProfileToTrack = (
return {
...track,
- codec: `avc1.${avc1Profile.profile.toString(16)}${avc1Profile.compatibility.toString(16).padStart(2, '0')}${avc1Profile.level.toString(16)}`,
+ codec: `avc1.${avc1Profile.sps.profile.toString(16).padStart(2, '0')}${avc1Profile.sps.compatibility.toString(16).padStart(2, '0')}${avc1Profile.sps.level.toString(16).padStart(2, '0')}`,
+ codecPrivate: combineUint8Arrays([
+ new Uint8Array([
+ // https://gist.github.com/uupaa/8493378ec15f644a3d2b
+ 1,
+ avc1Profile.sps.level,
+ avc1Profile.sps.compatibility,
+ avc1Profile.sps.profile,
+ 0xff,
+ 0xe1,
+ ]),
+ // sequence parameter set length
+ serializeUint16(avc1Profile.sps.sps.length),
+ // sequence parameter set
+ avc1Profile.sps.sps,
+ // num of PPS
+ new Uint8Array([0x01]),
+ // picture parameter set length
+ serializeUint16(avc1Profile.pps.pps.length),
+ // PPS
+ avc1Profile.pps.pps,
+ ]),
};
};
diff --git a/packages/media-parser/src/boxes/avc/parse-avc.ts b/packages/media-parser/src/boxes/avc/parse-avc.ts
index 81ef82f71f..fa65aae54e 100644
--- a/packages/media-parser/src/boxes/avc/parse-avc.ts
+++ b/packages/media-parser/src/boxes/avc/parse-avc.ts
@@ -16,11 +16,18 @@ export type AvcProfileInfo = {
profile: number;
level: number;
compatibility: number;
+ sps: Uint8Array;
type: 'avc-profile';
};
+export type AvcPPs = {
+ type: 'avc-pps';
+ pps: Uint8Array;
+};
+
type AvcInfo =
| AvcProfileInfo
+ | AvcPPs
| {
type: 'keyframe';
}
@@ -28,6 +35,26 @@ type AvcInfo =
type: 'delta-frame';
};
+const findEnd = (buffer: Uint8Array) => {
+ let zeroesInARow = 0;
+ for (let i = 0; i < buffer.length; i++) {
+ const val = buffer[i];
+
+ if (val === 0) {
+ zeroesInARow++;
+ continue;
+ }
+
+ if (zeroesInARow >= 2 && val === 1) {
+ return i - zeroesInARow;
+ }
+
+ zeroesInARow = 0;
+ }
+
+ return null;
+};
+
const inspect = (buffer: Uint8Array): AvcInfo | null => {
const iterator = getArrayBufferIterator(buffer, buffer.byteLength);
iterator.startReadingBits();
@@ -36,11 +63,15 @@ const inspect = (buffer: Uint8Array): AvcInfo | null => {
const type = iterator.getBits(5);
iterator.stopReadingBits();
if (type === 7) {
- const sps = readSps(iterator);
+ const end = findEnd(buffer);
+ const data = readSps(iterator);
+ const sps = buffer.slice(1, end === null ? Infinity : end);
+
return {
- level: sps.level,
- profile: sps.profile,
- compatibility: sps.compatibility,
+ level: data.level,
+ profile: data.profile,
+ compatibility: data.compatibility,
+ sps,
type: 'avc-profile',
};
}
@@ -51,6 +82,16 @@ const inspect = (buffer: Uint8Array): AvcInfo | null => {
};
}
+ if (type === 8) {
+ const end = findEnd(buffer);
+ const pps = buffer.slice(0, end === null ? Infinity : end);
+
+ return {
+ type: 'avc-pps',
+ pps,
+ };
+ }
+
if (type === 1) {
return {
type: 'delta-frame',
@@ -65,6 +106,7 @@ const inspect = (buffer: Uint8Array): AvcInfo | null => {
export const parseAvc = (buffer: Uint8Array): AvcInfo[] => {
let zeroesInARow = 0;
const infos: AvcInfo[] = [];
+
for (let i = 0; i < buffer.length; i++) {
const val = buffer[i];
@@ -74,6 +116,7 @@ export const parseAvc = (buffer: Uint8Array): AvcInfo[] => {
}
if (zeroesInARow >= 2 && val === 1) {
+ zeroesInARow = 0;
const info = inspect(buffer.slice(i + 1, i + 100));
if (info) {
infos.push(info);
@@ -83,7 +126,9 @@ export const parseAvc = (buffer: Uint8Array): AvcInfo[] => {
}
}
- zeroesInARow = 0;
+ if (val !== 1) {
+ zeroesInARow = 0;
+ }
}
return infos;
diff --git a/packages/media-parser/src/boxes/riff/get-tracks-from-avi.ts b/packages/media-parser/src/boxes/riff/get-tracks-from-avi.ts
index 1adce4664a..704201c0d4 100644
--- a/packages/media-parser/src/boxes/riff/get-tracks-from-avi.ts
+++ b/packages/media-parser/src/boxes/riff/get-tracks-from-avi.ts
@@ -42,9 +42,9 @@ export const makeAviAudioTrack = ({
return {
type: 'audio',
codec: 'mp4a.40.2', // According to Claude 3.5 Sonnet
- codecPrivate: null,
+ codecPrivate: new Uint8Array([18, 16]),
codecWithoutConfig: 'aac',
- description: undefined,
+ description: new Uint8Array([18, 16]),
numberOfChannels: strf.numberOfChannels,
sampleRate: strf.sampleRate,
timescale: MEDIA_PARSER_RIFF_TIMESCALE,
@@ -68,8 +68,7 @@ export const makeAviVideoTrack = ({
return {
codecPrivate: null,
- // TODO: Which avc1?
- codec: 'avc1',
+ codec: 'to-be-overriden-later',
codecWithoutConfig: 'h264',
codedHeight: strf.height,
codedWidth: strf.width,
diff --git a/packages/media-parser/src/boxes/riff/parse-movi.ts b/packages/media-parser/src/boxes/riff/parse-movi.ts
index 2c8e4ae118..859db7950d 100644
--- a/packages/media-parser/src/boxes/riff/parse-movi.ts
+++ b/packages/media-parser/src/boxes/riff/parse-movi.ts
@@ -58,8 +58,9 @@ export const handleChunk = async ({
}
const avcProfile = infos.find((i) => i.type === 'avc-profile');
- if (avcProfile) {
- await options.parserState.onProfile(avcProfile);
+ const ppsProfile = infos.find((i) => i.type === 'avc-pps');
+ if (avcProfile && ppsProfile) {
+ await options.parserState.onProfile({pps: ppsProfile, sps: avcProfile});
}
await options.parserState.onVideoSample(trackId, {
diff --git a/packages/media-parser/src/parser-state.ts b/packages/media-parser/src/parser-state.ts
index 183af9533a..5012d9810a 100644
--- a/packages/media-parser/src/parser-state.ts
+++ b/packages/media-parser/src/parser-state.ts
@@ -1,4 +1,4 @@
-import type {AvcProfileInfo} from './boxes/avc/parse-avc';
+import type {AvcPPs, AvcProfileInfo} from './boxes/avc/parse-avc';
import type {OnTrackEntrySegment} from './boxes/webm/segments';
import type {TrackInfo} from './boxes/webm/segments/track-entry';
import {
@@ -14,7 +14,12 @@ import type {
export type InternalStats = {};
-type AvcProfileInfoCallback = (profile: AvcProfileInfo) => Promise<void>;
+export type SpsAndPps = {
+ sps: AvcProfileInfo;
+ pps: AvcPPs;
+};
+
+type AvcProfileInfoCallback = (profile: SpsAndPps) => Promise<void>;
export const makeParserState = ({
hasAudioCallbacks,
@@ -106,9 +111,9 @@ export const makeParserState = ({
profileCallbacks.push(callback);
};
- let avcProfile: AvcProfileInfo | null = null;
+ let avcProfile: SpsAndPps | null = null;
- const onProfile = async (profile: AvcProfileInfo) => {
+ const onProfile = async (profile: SpsAndPps) => {
avcProfile = profile;
for (const callback of profileCallbacks) {
await callback(profile);
@@ -164,7 +169,7 @@ export const makeParserState = ({
throw new Error('Aborted');
}
- if (samplesForTrack[trackId] === undefined) {
+ if (typeof samplesForTrack[trackId] === 'undefined') {
samplesForTrack[trackId] = 0;
}
@@ -188,10 +193,12 @@ export const makeParserState = ({
throw new Error('Aborted');
}
- if (samplesForTrack[trackId] === undefined) {
- samplesForTrack[trackId]++;
+ if (typeof samplesForTrack[trackId] === 'undefined') {
+ samplesForTrack[trackId] = 0;
}
+ samplesForTrack[trackId]++;
+
const callback = videoSampleCallbacks[trackId];
if (callback) {
await callback(videoSample);
diff --git a/packages/media-parser/src/test/parse-avi-file.test.ts b/packages/media-parser/src/test/parse-avi-file.test.ts
index f511e53ea3..d0be7beca1 100644
--- a/packages/media-parser/src/test/parse-avi-file.test.ts
+++ b/packages/media-parser/src/test/parse-avi-file.test.ts
@@ -73,9 +73,9 @@ test('AVI file', async () => {
expect(audioTracks).toEqual([
{
codec: 'mp4a.40.2',
- codecPrivate: null,
+ codecPrivate: new Uint8Array([18, 16]),
codecWithoutConfig: 'aac',
- description: undefined,
+ description: new Uint8Array([18, 16]),
numberOfChannels: 2,
sampleRate: 48000,
timescale: 1_000_000,
@@ -87,7 +87,11 @@ test('AVI file', async () => {
expect(videoTracks).toEqual([
{
codec: 'avc1.640015',
- codecPrivate: null,
+ codecPrivate: new Uint8Array([
+ 1, 21, 0, 100, 255, 225, 0, 25, 100, 0, 21, 172, 217, 65, 224, 143, 235,
+ 1, 16, 0, 0, 3, 0, 16, 0, 0, 3, 3, 192, 241, 98, 217, 96, 1, 0, 4, 104,
+ 239, 139, 203,
+ ]),
codecWithoutConfig: 'h264',
codedHeight: 270,
codedWidth: 480,
diff --git a/packages/webcodecs/src/audio-decoder.ts b/packages/webcodecs/src/audio-decoder.ts
index 52f4893466..af2a634ed0 100644
--- a/packages/webcodecs/src/audio-decoder.ts
+++ b/packages/webcodecs/src/audio-decoder.ts
@@ -97,6 +97,12 @@ export const createAudioDecoder = ({
return {
processSample: (sample: AudioOrVideoSample) => {
+ // In example.avi, we have samples with 0 data
+ // Chrome fails on these
+ if (sample.data.length === 0) {
+ return queue;
+ }
+
queue = queue.then(() => processSample(sample));
return queue;
},