Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Accept .ts files if the server does not send a content-length

Jonny Burgerd448f3376089
diff --git a/packages/media-parser/src/readers/fetch/get-body-and-reader.ts b/packages/media-parser/src/readers/fetch/get-body-and-reader.ts
index db7d4dfcdc..dda84346ca 100644
--- a/packages/media-parser/src/readers/fetch/get-body-and-reader.ts
+++ b/packages/media-parser/src/readers/fetch/get-body-and-reader.ts
@@ -6,15 +6,25 @@ type ReturnType = {
needsContentRange: boolean;
};
-export const getLengthAndReader = async (
- endsWithM3u8: boolean,
- res: Response,
- ownController: AbortController,
-): Promise<ReturnType> => {
- if (endsWithM3u8) {
- const text = await res.text();
-
- const encoded = new TextEncoder().encode(text);
+export const getLengthAndReader = async ({
+ canLiveWithoutContentLength,
+ res,
+ ownController,
+ requestedWithoutRange,
+}: {
+ canLiveWithoutContentLength: boolean;
+ res: Response;
+ ownController: AbortController;
+ requestedWithoutRange: boolean;
+}): Promise<ReturnType> => {
+ const length = res.headers.get('content-length');
+ const contentLength = length === null ? null : parseInt(length, 10);
+ if (
+ requestedWithoutRange ||
+ (canLiveWithoutContentLength && contentLength === null)
+ ) {
+ const buffer = await res.arrayBuffer();
+ const encoded = new Uint8Array(buffer);
const stream = new ReadableStream({
start(controller) {
@@ -35,9 +45,6 @@ export const getLengthAndReader = async (
};
}
- const length = res.headers.get('content-length');
- const contentLength = length === null ? null : parseInt(length, 10);
-
if (!res.body) {
throw new Error('No body');
}
diff --git a/packages/media-parser/src/readers/from-fetch.ts b/packages/media-parser/src/readers/from-fetch.ts
index 4023377de8..bc5fa94732 100644
--- a/packages/media-parser/src/readers/from-fetch.ts
+++ b/packages/media-parser/src/readers/from-fetch.ts
@@ -33,11 +33,15 @@ export function parseContentRange(input: string): ParsedContentRange | null {
return range;
}
-const validateContentRangeAndDetectIfSupported = (
- actualRange: number | [number, number],
- parsedContentRange: ParsedContentRange | null,
- statusCode: number,
-): {supportsContentRange: boolean} => {
+const validateContentRangeAndDetectIfSupported = ({
+ actualRange,
+ parsedContentRange,
+ statusCode,
+}: {
+ actualRange: number | [number, number];
+ parsedContentRange: ParsedContentRange | null;
+ statusCode: number;
+}): {supportsContentRange: boolean} => {
if (statusCode === 206) {
return {supportsContentRange: true};
}
@@ -102,14 +106,18 @@ export const fetchReader: ReaderInterface = {
const actualRange = range === null ? 0 : range;
- const endsWithM3u8 = (
- typeof resolvedUrl === 'string' ? resolvedUrl : resolvedUrl.pathname
- ).endsWith('.m3u8');
+ const asString =
+ typeof resolvedUrl === 'string' ? resolvedUrl : resolvedUrl.pathname;
+
+ const requestWithoutRange = asString.endsWith('.m3u8');
+
+ const canLiveWithoutContentLength =
+ asString.endsWith('.m3u8') || asString.endsWith('.ts');
const headers: {
Range?: string;
} =
- actualRange === 0 && endsWithM3u8
+ actualRange === 0 && requestWithoutRange
? {}
: typeof actualRange === 'number'
? {
@@ -130,11 +138,11 @@ export const fetchReader: ReaderInterface = {
? parseContentRange(contentRange)
: null;
- const {supportsContentRange} = validateContentRangeAndDetectIfSupported(
+ const {supportsContentRange} = validateContentRangeAndDetectIfSupported({
actualRange,
parsedContentRange,
- res.status,
- );
+ statusCode: res.status,
+ });
controller._internals.signal.addEventListener(
'abort',
@@ -158,9 +166,12 @@ export const fetchReader: ReaderInterface = {
const fallbackName = src.split('/').pop() as string;
const {contentLength, needsContentRange, reader} = await getLengthAndReader(
- endsWithM3u8,
- res,
- ownController,
+ {
+ canLiveWithoutContentLength,
+ res,
+ ownController,
+ requestedWithoutRange: requestWithoutRange,
+ },
);
if (controller) {