Skip to content

🎥 Make videos programmatically with React

make samples more correct

Jonny Burger7c5c4b407234
diff --git a/packages/media-parser/src/containers/avc/create-sps-pps-data.ts b/packages/media-parser/src/containers/avc/create-sps-pps-data.ts
index 4b5eddc7c5..9e6428cd92 100644
--- a/packages/media-parser/src/containers/avc/create-sps-pps-data.ts
+++ b/packages/media-parser/src/containers/avc/create-sps-pps-data.ts
@@ -1,5 +1,6 @@
import {combineUint8Arrays} from '../../combine-uint8-arrays';
import type {SpsAndPps} from '../../state/parser-state';
+import {truthy} from '../../truthy';
function serializeUint16(value: number): Uint8Array {
const buffer = new ArrayBuffer(2);
@@ -12,25 +13,40 @@ function serializeUint16(value: number): Uint8Array {
}
export const createSpsPpsData = (avc1Profile: SpsAndPps) => {
- return combineUint8Arrays([
- new Uint8Array([
- // https://gist.github.com/uupaa/8493378ec15f644a3d2b
- 1, // version
- avc1Profile.sps.spsData.profile,
- avc1Profile.sps.spsData.compatibility,
- avc1Profile.sps.spsData.level,
- 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,
- ]);
+ return combineUint8Arrays(
+ [
+ new Uint8Array([
+ // https://gist.github.com/uupaa/8493378ec15f644a3d2b
+ 1, // version
+ avc1Profile.sps.spsData.profile,
+ avc1Profile.sps.spsData.compatibility,
+ avc1Profile.sps.spsData.level,
+ 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,
+ // if AVCProfileIndication != 66 && AVCProfileIndication != 77 && AVCProfileIndication != 88
+ [66, 77, 88].some((b) => avc1Profile.sps.spsData.profile === b)
+ ? null
+ : /**
+ * reserved 63 (0x3F)
+ chroma_format 1, '4:2:0'
+ reserved 31 (0x1F)
+ bit_depth_luma_minus8 0
+ reserved 31 (0x1F)
+ bit_depth_chroma_minus8 0
+ numOfSequenceParameterSetExt 0
+ */
+ new Uint8Array([0xfd, 0xf8, 0xf8, 0]),
+ ].filter(truthy),
+ );
};
diff --git a/packages/media-parser/src/containers/transport-stream/find-separator.ts b/packages/media-parser/src/containers/transport-stream/find-separator.ts
index 46a0c4d30d..d2b50bfcec 100644
--- a/packages/media-parser/src/containers/transport-stream/find-separator.ts
+++ b/packages/media-parser/src/containers/transport-stream/find-separator.ts
@@ -1,6 +1,4 @@
-import type {TransportStreamEntry} from './parse-pmt';
-
-function findNthSubarrayIndex(
+export function findNthSubarrayIndex(
array: Uint8Array,
subarray: Uint8Array,
n: number,
@@ -28,17 +26,3 @@ function findNthSubarrayIndex(
return -1; // Return -1 if nth subarray is not found
}
-
-export const findNextSeparator = ({
- restOfPacket,
- transportStreamEntry,
-}: {
- restOfPacket: Uint8Array;
- transportStreamEntry: TransportStreamEntry;
-}) => {
- if (transportStreamEntry.streamType === 27) {
- return findNthSubarrayIndex(restOfPacket, new Uint8Array([0, 0, 1, 9]), 2);
- }
-
- throw new Error(`Unsupported stream ID ${transportStreamEntry.streamType}`);
-};
diff --git a/packages/media-parser/src/containers/transport-stream/parse-stream-packet.ts b/packages/media-parser/src/containers/transport-stream/parse-stream-packet.ts
index c40ca0bdac..26f8a81910 100644
--- a/packages/media-parser/src/containers/transport-stream/parse-stream-packet.ts
+++ b/packages/media-parser/src/containers/transport-stream/parse-stream-packet.ts
@@ -3,7 +3,7 @@ import type {TransportStreamStructure} from '../../parse-result';
import type {ParserState} from '../../state/parser-state';
import {readAdtsHeader} from './adts-header';
import {getRestOfPacket} from './discard-rest-of-packet';
-import {findNextSeparator} from './find-separator';
+import {findNthSubarrayIndex} from './find-separator';
import type {TransportStreamEntry} from './parse-pmt';
import type {TransportStreamPacketBuffer} from './process-stream-buffers';
import {processStreamBuffer} from './process-stream-buffers';
@@ -60,22 +60,21 @@ const parseAdtsStream = async ({
};
const parseAvcStream = async ({
- transportStreamEntry,
programId,
state,
structure,
streamBuffer,
}: {
- transportStreamEntry: TransportStreamEntry;
programId: number;
state: ParserState;
structure: TransportStreamStructure;
streamBuffer: TransportStreamPacketBuffer;
}): Promise<Uint8Array | null> => {
- const indexOfSeparator = findNextSeparator({
- restOfPacket: streamBuffer.buffer,
- transportStreamEntry,
- });
+ const indexOfSeparator = findNthSubarrayIndex(
+ streamBuffer.buffer,
+ new Uint8Array([0, 0, 1, 9]),
+ 2,
+ );
if (indexOfSeparator === -1 || indexOfSeparator === 0) {
return null;
}
@@ -88,6 +87,7 @@ const parseAvcStream = async ({
streamBuffer: {
offset: streamBuffer.offset,
pesHeader: streamBuffer.pesHeader,
+ // Replace the regular 0x00000001 with 0x00000002 to avoid confusion with other 0x00000001 (?)
buffer: packet,
},
programId,
@@ -131,7 +131,6 @@ export const parseStream = async ({
]);
const rest = await parseAvcStream({
- transportStreamEntry,
state,
programId,
structure,
diff --git a/packages/media-parser/src/test/mux-video.test.ts b/packages/media-parser/src/test/mux-video.test.ts
index 0a5b3196c4..ab4fec6ac9 100644
--- a/packages/media-parser/src/test/mux-video.test.ts
+++ b/packages/media-parser/src/test/mux-video.test.ts
@@ -22,6 +22,7 @@ test('should take a direct stream', async () => {
acknowledgeRemotionLicense: true,
fields: {
dimensions: true,
+ tracks: true,
},
});
diff --git a/packages/webcodecs/src/can-copy-video-track.ts b/packages/webcodecs/src/can-copy-video-track.ts
index 51a53120da..7683b84ec1 100644
--- a/packages/webcodecs/src/can-copy-video-track.ts
+++ b/packages/webcodecs/src/can-copy-video-track.ts
@@ -49,9 +49,7 @@ export const canCopyVideoTrack = ({
return (
(inputTrack.codecWithoutConfig === 'h264' ||
inputTrack.codecWithoutConfig === 'h265') &&
- (inputContainer === 'mp4' ||
- inputContainer === 'avi' ||
- inputContainer === 'm3u8')
+ (inputContainer === 'mp4' || inputContainer === 'avi')
);
}
diff --git a/packages/webcodecs/src/create/iso-base-media/mp4-header.ts b/packages/webcodecs/src/create/iso-base-media/mp4-header.ts
index af08629bbe..d0402119c5 100644
--- a/packages/webcodecs/src/create/iso-base-media/mp4-header.ts
+++ b/packages/webcodecs/src/create/iso-base-media/mp4-header.ts
@@ -12,7 +12,7 @@ import {createMeta} from './udta/create-meta';
import {createHdlr} from './udta/meta/create-hdlr';
// TODO: Creates a header that is way too large
-const HEADER_LENGTH = 2048_000;
+const HEADER_LENGTH = 1024_000;
export const createPaddedMoovAtom = ({
durationInUnits,
diff --git a/packages/webcodecs/src/create/iso-base-media/trak/mdia/minf/stbl/create-stts.ts b/packages/webcodecs/src/create/iso-base-media/trak/mdia/minf/stbl/create-stts.ts
index 04234b0aa0..7d03606fa9 100644
--- a/packages/webcodecs/src/create/iso-base-media/trak/mdia/minf/stbl/create-stts.ts
+++ b/packages/webcodecs/src/create/iso-base-media/trak/mdia/minf/stbl/create-stts.ts
@@ -28,7 +28,11 @@ export const createSttsAtom = (samplePositions: SamplePosition[]) => {
const durations = samplePositions.map((_, i, a) => {
// TODO: Why does 0 appear here?
if (a[i].duration === undefined || a[i].duration === 0) {
- return (a[i + 1]?.dts ?? a[i].dts) - a[i].dts;
+ if (a[i + 1] === undefined) {
+ return a[i].dts - (a[i - 1]?.dts ?? a[i].dts);
+ }
+
+ return a[i + 1].dts - a[i].dts;
}
return a[i].duration;