Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Fix HLS by using PathedSource for .m3u8 URLs

Jonny Burger7b775a7905a4
diff --git a/packages/media/src/create-input.ts b/packages/media/src/create-input.ts
index 602131165c..f3db0882aa 100644
--- a/packages/media/src/create-input.ts
+++ b/packages/media/src/create-input.ts
@@ -1,5 +1,20 @@
import type {CreateInputFromOptions} from 'mediabunny';
-import {ALL_FORMATS, createInputFrom} from 'mediabunny';
+import {
+ ALL_FORMATS,
+ HLS_FORMATS,
+ PathedSource,
+ UrlSource,
+ createInputFrom,
+} from 'mediabunny';
+
+const isHlsSource = (src: string): boolean => {
+ try {
+ const url = new URL(src, 'http://placeholder');
+ return url.pathname.toLowerCase().endsWith('.m3u8');
+ } catch {
+ return src.toLowerCase().endsWith('.m3u8');
+ }
+};
export const createInput = ({
src,
@@ -10,8 +25,18 @@ export const createInput = ({
credentials: RequestCredentials | undefined;
urlSourceOptions?: Partial<CreateInputFromOptions>;
}) => {
- return createInputFrom(src, ALL_FORMATS, {
+ const options: CreateInputFromOptions = {
...urlSourceOptions,
...(credentials ? {requestInit: {credentials}} : undefined),
- });
+ };
+
+ if (isHlsSource(src)) {
+ const source = new PathedSource(
+ src,
+ ({path}) => new UrlSource(path, options),
+ );
+ return createInputFrom(source, HLS_FORMATS);
+ }
+
+ return createInputFrom(src, ALL_FORMATS, options);
};