Skip to content

🎥 Make videos programmatically with React

remotion.dev/convert: Load video from x.com

Jonny Burger4a3e35459099
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 1f6e4ea144..45afb21f72 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -8,7 +8,11 @@ import type {
MediaParserVideoCodec,
TracksField,
} from '@remotion/media-parser';
-import {hasBeenAborted, MediaParserInternals} from '@remotion/media-parser';
+import {
+ defaultSelectM3uAssociatedPlaylists,
+ hasBeenAborted,
+ MediaParserInternals,
+} from '@remotion/media-parser';
import {fetchReader} from '@remotion/media-parser/fetch';
import {webFileReader} from '@remotion/media-parser/web-file';
import type {
@@ -272,6 +276,10 @@ const ConvertUI = ({
return selectedM3uId ?? streams[0].id;
},
selectM3uAssociatedPlaylists: ({associatedPlaylists}) => {
+ if (selectAssociatedPlaylistId === null) {
+ return defaultSelectM3uAssociatedPlaylists({associatedPlaylists});
+ }
+
return associatedPlaylists.filter(
(playlist) => selectAssociatedPlaylistId === playlist.id,
);
diff --git a/packages/convert/app/components/LoadFromX.tsx b/packages/convert/app/components/LoadFromX.tsx
index 31a5a0e304..2b9cf62f13 100644
--- a/packages/convert/app/components/LoadFromX.tsx
+++ b/packages/convert/app/components/LoadFromX.tsx
@@ -1,4 +1,5 @@
import React, {useCallback, useMemo} from 'react';
+import {extractTweetId} from '~/lib/extract-tweet-id';
import {Button} from './ui/button';
import {
Dialog,
@@ -12,16 +13,17 @@ import {Input} from './ui/input';
export const LoadFromX: React.FC = () => {
const [open, setOpen] = React.useState(false);
const [value, setValue] = React.useState('');
+ const [loading, setLoading] = React.useState(false);
+ const [error, setError] = React.useState<string | null>(null);
const onOpenChange = React.useCallback(() => {
setOpen((prev) => !prev);
+ setError(null);
}, []);
const isValid = useMemo(() => {
try {
- // eslint-disable-next-line no-new
- new URL(value);
- return true;
+ return extractTweetId(value) !== null;
} catch {
return false;
}
@@ -32,31 +34,40 @@ export const LoadFromX: React.FC = () => {
}, []);
const set = useCallback(async () => {
- const res = await fetch(
- 'https://lzpqleuj7axkrgoxsa3h7ecynq0vveim.lambda-url.eu-central-1.on.aws/?id=1859650265021817143',
- );
- const json = await res.json();
- if (json.type !== 'success') {
- console.log(json.error); // TODO: Show error
- return;
- }
+ setLoading(true);
+ setError(null);
+ try {
+ const res = await fetch(
+ 'https://lzpqleuj7axkrgoxsa3h7ecynq0vveim.lambda-url.eu-central-1.on.aws/?id=' +
+ extractTweetId(value),
+ );
+ const json = await res.json();
+ if (json.type !== 'success') {
+ setError(json.error || 'Failed to load from X');
+ return;
+ }
- const urls = json.urls as string[];
- if (!urls.length) {
- console.log('No URLs found'); // TODO: Show error
- return;
- }
+ const urls = json.urls as string[];
+ if (!urls.length) {
+ setError('No videos found in this post');
+ return;
+ }
- const [firstUrl, ...moreUrls] = urls;
- const currentUrl = new URL(window.location.href);
- currentUrl.searchParams.set('url', firstUrl);
+ const [firstUrl] = urls;
+ const currentUrl = new URL(window.location.href);
+ currentUrl.searchParams.set('url', firstUrl);
- window.location.href = currentUrl.toString();
- }, []);
+ window.location.href = currentUrl.toString();
+ } catch {
+ setError('An unexpected error occurred');
+ } finally {
+ setLoading(false);
+ }
+ }, [value]);
const onKeydown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
- if (!isValid) {
+ if (!isValid || loading) {
return;
}
@@ -64,7 +75,7 @@ export const LoadFromX: React.FC = () => {
set();
}
},
- [isValid, set],
+ [isValid, loading, set],
);
return (
@@ -90,9 +101,14 @@ export const LoadFromX: React.FC = () => {
onChange={(e) => setValue(e.target.value)}
placeholder="For example: https://x.com/JNYBGR/status/1859650265021817143"
/>
+ {error && <p className="text-red-500 text-sm mt-2">{error}</p>}
<div className="h-2" />
- <Button disabled={!isValid} variant="brand" onClick={set}>
- Load
+ <Button
+ disabled={!isValid || loading}
+ variant="brand"
+ onClick={set}
+ >
+ {loading ? 'Loading...' : 'Load'}
</Button>
</DialogHeader>
</DialogContent>
diff --git a/packages/convert/app/components/M3uAudioStreamSelector.tsx b/packages/convert/app/components/M3uAudioStreamSelector.tsx
index 674138ebce..4b69e0416a 100644
--- a/packages/convert/app/components/M3uAudioStreamSelector.tsx
+++ b/packages/convert/app/components/M3uAudioStreamSelector.tsx
@@ -1,4 +1,8 @@
-import type {M3uStream} from '@remotion/media-parser';
+import type {M3uAssociatedPlaylist} from '@remotion/media-parser';
+import {
+ defaultSelectM3uAssociatedPlaylists,
+ type M3uStream,
+} from '@remotion/media-parser';
import React from 'react';
import {Label} from './ui/label';
import {
@@ -25,7 +29,11 @@ export const M3uAudioStreamSelector: React.FC<{
}) => {
const selectedAssociatedPlaylist =
selectedAssociatedPlaylistId ??
- selectedStream?.associatedPlaylists?.find((stream) => stream.default)?.id ??
+ (
+ defaultSelectM3uAssociatedPlaylists({
+ associatedPlaylists: selectedStream.associatedPlaylists,
+ }) as M3uAssociatedPlaylist[]
+ )?.[0]?.id ??
null;
return (
<>
diff --git a/packages/convert/app/components/PickFile.tsx b/packages/convert/app/components/PickFile.tsx
index 84001bb1cd..8e843b5239 100644
--- a/packages/convert/app/components/PickFile.tsx
+++ b/packages/convert/app/components/PickFile.tsx
@@ -1,6 +1,7 @@
import React, {useCallback} from 'react';
import {SAMPLE_FILE} from '~/lib/config';
import type {Source} from '~/lib/convert-state';
+import {AlternativePickFileOptions} from './AlternativePickFileOptions';
import {DropFileBox} from './DropFileBox';
import {TextMarkLogo} from './TextMarkLogo';
import {WhyRemotionConvert} from './WhyRemotionConvert';
@@ -50,12 +51,7 @@ export const PickFile: React.FC<{
<div className="h-4" />
<div className="font-brand">or </div>
<div className="h-4" />
- <a
- className="font-brand text-brand cursor cursor-pointer hover:underline"
- onClick={onSampleFile}
- >
- Use a sample file
- </a>
+ <AlternativePickFileOptions onSampleFile={onSampleFile} />
<div className="h-10" />
</div>
<div className="w-full bg-white border-t-2 border-black">
diff --git a/packages/convert/app/lib/extract-tweet-id.ts b/packages/convert/app/lib/extract-tweet-id.ts
new file mode 100644
index 0000000000..2d748ffc42
--- /dev/null
+++ b/packages/convert/app/lib/extract-tweet-id.ts
@@ -0,0 +1,47 @@
+/**
+ * Extracts the tweet ID from various Twitter/X URL formats
+ * @param url The Twitter/X URL to extract the tweet ID from
+ * @returns The tweet ID as a string, or null if no valid ID could be extracted
+ */
+export function extractTweetId(url: string): string | null {
+ if (!url) return null;
+
+ // Normalize the URL by trimming whitespace and converting to lowercase
+ const normalizedUrl = url.trim().toLowerCase();
+
+ // Define regex patterns for different Twitter URL formats
+ const patterns = [
+ // twitter.com/username/status/id
+ /twitter\.com\/[^/]+\/status(?:es)?\/(\d+)/i,
+ // x.com/username/status/id
+ /x\.com\/[^/]+\/status(?:es)?\/(\d+)/i,
+ // twitter.com/i/web/status/id
+ /twitter\.com\/i\/web\/status(?:es)?\/(\d+)/i,
+ // x.com/i/web/status/id
+ /x\.com\/i\/web\/status(?:es)?\/(\d+)/i,
+ // mobile.twitter.com/username/status/id
+ /mobile\.twitter\.com\/[^/]+\/status(?:es)?\/(\d+)/i,
+ // mobile.x.com/username/status/id
+ /mobile\.x\.com\/[^/]+\/status(?:es)?\/(\d+)/i,
+ // t.co/shortcode (this will only work if the URL has the ID directly)
+ /t\.co\/[a-zA-Z0-9]+\/(\d+)/i,
+ // For URLs with query parameters: ?id=123456
+ /[?&]id=(\d+)/i,
+ ];
+
+ // Try each pattern until we find a match
+ for (const pattern of patterns) {
+ const match = normalizedUrl.match(pattern);
+ if (match && match[1]) {
+ return match[1];
+ }
+ }
+
+ // Handle URLs that end with the ID
+ const idAtEndMatch = normalizedUrl.match(/\/(\d{16,20})(?:[?#].*)?$/);
+ if (idAtEndMatch && idAtEndMatch[1]) {
+ return idAtEndMatch[1];
+ }
+
+ return null;
+}
diff --git a/packages/media-parser/src/containers/m3u/select-stream.ts b/packages/media-parser/src/containers/m3u/select-stream.ts
index e03622fab1..2547746206 100644
--- a/packages/media-parser/src/containers/m3u/select-stream.ts
+++ b/packages/media-parser/src/containers/m3u/select-stream.ts
@@ -42,12 +42,10 @@ export const selectAssociatedPlaylists = async ({
export const defaultSelectM3uAssociatedPlaylists: SelectM3uAssociatedPlaylistsFn =
({associatedPlaylists}) => {
if (associatedPlaylists.length === 1) {
- return Promise.resolve(associatedPlaylists);
+ return associatedPlaylists;
}
- return Promise.resolve(
- associatedPlaylists.filter((playlist) => playlist.default),
- );
+ return associatedPlaylists.filter((playlist) => playlist.default);
};
export const selectStream = async ({
diff --git a/packages/media-parser/src/index.ts b/packages/media-parser/src/index.ts
index ac37b3f5c5..024a5c3ac1 100644
--- a/packages/media-parser/src/index.ts
+++ b/packages/media-parser/src/index.ts
@@ -94,7 +94,7 @@ export type {
} from './containers/webm/segments/all-segments';
export type {LogLevel};
-export {M3uStream} from './containers/m3u/get-streams';
+export {M3uAssociatedPlaylist, M3uStream} from './containers/m3u/get-streams';
export {
defaultSelectM3uAssociatedPlaylists,
defaultSelectM3uStreamFn,