Skip to content

Open source CapCut alternative.

TypeScript

feat(site): import_url pulls tweet photos, direct image links, and the post text [rebuild]

David Le73ace583bcd0
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index cc478836..fe400bba 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -127,12 +127,14 @@ export function LibraryView() {
setImporting(true);
setUrlError(null);
try {
- const asset = await importUrlToLibrary(value);
+ const imported = await importUrlToLibrary(value);
if (openFolder) {
- await moveLibraryAsset(asset.id, openFolder).catch(() => {});
- asset.folderId = openFolder;
+ for (const asset of imported) {
+ await moveLibraryAsset(asset.id, openFolder).catch(() => {});
+ asset.folderId = openFolder;
+ }
}
- setAssets((prev) => [asset, ...(prev ?? [])]);
+ setAssets((prev) => [...imported, ...(prev ?? [])]);
setUrl("");
setAddOpen(false);
} catch (e) {
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 9faff134..bd0b07db 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -949,13 +949,16 @@ export async function runAiTool(
// Captured before the download: the media files under the chat that
// asked, even if the user switches threads while it downloads.
const chatId = chatOwner();
- const asset = await importUrlMedia(projectId, url);
- tagChatAsset(asset.id, chatId);
+ const { assets, text } = await importUrlMedia(projectId, url);
+ for (const asset of assets) tagChatAsset(asset.id, chatId);
return {
- assetId: asset.id,
- name: asset.name,
- kind: asset.type,
- duration: round2(asset.duration),
+ assets: assets.map((asset) => ({
+ assetId: asset.id,
+ name: asset.name,
+ kind: asset.type,
+ duration: round2(asset.type === "image" ? IMAGE_CLIP_SECONDS : asset.duration),
+ })),
+ ...(text ? { postText: text } : {}),
};
}
diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.ts
index 013704ff..5cba7608 100644
--- a/site/src/cut/lib/library.ts
+++ b/site/src/cut/lib/library.ts
@@ -47,13 +47,13 @@ export async function fetchLibrary(): Promise<LibraryData> {
return (await res.json()) as LibraryData;
}
-export async function importUrlToLibrary(url: string): Promise<LibraryAsset> {
+export async function importUrlToLibrary(url: string): Promise<LibraryAsset[]> {
const res = await apiFetch("/api/cut/library/import-url", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url }),
});
- const body = await apiJson<LibraryAsset>(res);
+ const body = await apiJson<LibraryAsset[]>(res);
if (!res.ok) throw new Error(body.error ?? "Could not import that URL.");
return body;
}
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index 013b1bd3..534397ba 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -208,40 +208,58 @@ export async function importStockVideo(
return asset;
}
-/** Download a media URL (TikTok, YouTube, …) into the project through the
- * engine's bundled downloader and register it, without placing it on the
- * timeline. Callers choose where it lands. */
-export async function importUrlMedia(projectId: string, url: string): Promise<MediaAsset> {
+/** Download a media URL (TikTok, YouTube, a tweet, …) into the project
+ * through the engine's bundled downloader and register what came back — one
+ * asset for a video, one per photo for a photo tweet — without placing
+ * anything on the timeline. Callers choose where assets land. `text` is the
+ * post text when the URL was a tweet. */
+export async function importUrlMedia(
+ projectId: string,
+ url: string
+): Promise<{ assets: MediaAsset[]; text?: string }> {
const res = await apiFetch(`/api/cut/projects/${projectId}/import-url`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url }),
});
- const body = await apiJson<{ fileName?: string; title?: string }>(res);
- if (!res.ok || !body.fileName) throw new Error(body.error ?? "Could not import that URL.");
- const asset = await assetFromProjectFile(projectId, body.fileName, body.title || "Imported clip");
- useEditor.getState().addAsset(asset);
- void enrichAsset(asset);
- return asset;
+ const body = await apiJson<{ files?: { fileName: string; title: string }[]; text?: string }>(res);
+ if (!res.ok || !body.files?.length) throw new Error(body.error ?? "Could not import that URL.");
+ const assets: MediaAsset[] = [];
+ for (const f of body.files) {
+ const asset = await assetFromProjectFile(projectId, f.fileName, f.title || "Imported clip");
+ useEditor.getState().addAsset(asset);
+ void enrichAsset(asset);
+ assets.push(asset);
+ }
+ return { assets, text: body.text };
}
/** Build a runtime asset for a media file the engine already wrote into the
- * project folder (freeze frames, AI generations) — probe metadata, no upload. */
+ * project folder (freeze frames, AI generations, URL imports) — probe
+ * metadata, no upload. */
export async function assetFromProjectFile(
projectId: string,
fileName: string,
name: string
): Promise<MediaAsset> {
const url = mediaUrl(projectId, fileName);
- const v = await loadVideoMeta(url);
const asset: MediaAsset = {
id: uid(),
fileName,
name,
type: "video",
- duration: v.duration,
+ duration: 0,
url,
};
+ if (/\.(png|jpe?g|webp|gif|avif|bmp)$/i.test(fileName)) {
+ asset.type = "image";
+ const dims = await loadImageMeta(url);
+ asset.width = dims.width;
+ asset.height = dims.height;
+ return asset;
+ }
+ const v = await loadVideoMeta(url);
+ asset.duration = v.duration;
if (v.videoWidth === 0) {
asset.type = "audio";
} else {
diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.ts
index 7f5c52e5..a0270b4c 100644
--- a/site/src/cut/server/ai/catalog.ts
+++ b/site/src/cut/server/ai/catalog.ts
@@ -388,7 +388,7 @@ export const AI_TOOLS: AiToolDef[] = [
{
name: "import_url",
description:
- "Download a media URL — TikTok, YouTube, Instagram Reels, or a direct video/audio link — with the bundled downloader and import it into the project. Free and local. It lands on a card in this chat, and the user drags it from there to the timeline, Media, or the Library; place it yourself (add_clip) only when they asked for it in the cut. A short clip downloads in seconds; a long video can take a couple of minutes.",
+ "Download a media URL — TikTok, YouTube, Instagram Reels, an X/Twitter post, or a direct video/audio/image link — with the bundled downloader and import it into the project. An X post yields its video, or every photo plus the post text (postText). Free and local. Each asset lands on a card in this chat, and the user drags it from there to the timeline, Media, or the Library; place it yourself (add_clip) only when they asked for it in the cut. A short clip downloads in seconds; a long video can take a couple of minutes.",
inputSchema: obj({ url: str("The page or media URL to download") }, ["url"]),
},
{
diff --git a/site/src/cut/server/urlImport.ts b/site/src/cut/server/urlImport.ts
index 1890be8a..4974f63f 100644
--- a/site/src/cut/server/urlImport.ts
+++ b/site/src/cut/server/urlImport.ts
@@ -1,5 +1,5 @@
import { spawn } from "node:child_process";
-import { copyFile, mkdir, mkdtemp, readdir, rm, stat } from "node:fs/promises";
+import { copyFile, mkdir, mkdtemp, readdir, rm, stat, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { addDownloaded, type LibraryAsset, type LibrarySource } from "./library";
@@ -9,6 +9,8 @@ import { uniqueName } from "./util";
// Download a media URL (TikTok, YouTube, Instagram, …) with the bundled
// yt-dlp, resolved from the widened PATH (tool-path.ts) exactly like ffmpeg.
+// yt-dlp only extracts video/audio, so a photo tweet falls back to fetching
+// the image through X's embed endpoint.
const MEDIA_EXT = /\.(mp4|mov|m4v|webm|mkv|mp3|m4a|aac|wav|ogg|flac)$/i;
@@ -23,15 +25,17 @@ interface YtMeta {
webpage_url?: string;
}
-/** What a finished download hands its consumer: the merged media file (inside
- * a temp dir the wrapper deletes afterwards) plus what yt-dlp knew about it. */
+/** What a finished download hands its consumer: the media files (inside a
+ * temp dir the wrapper deletes afterwards — one for yt-dlp's merged output,
+ * one per photo for a photo tweet) plus what the extractor knew about them.
+ * `text` is the full post text when the URL was a tweet. */
interface Downloaded {
- file: string;
- title: string;
+ files: { file: string; title: string }[];
source: LibrarySource;
+ text?: string;
}
-/** Run one guarded download and hand the file to `consume` before the temp
+/** Run one guarded download and hand the files to `consume` before the temp
* dir is deleted. The active slot spans the whole operation; mkdtemp runs
* inside the try so a failure there still releases the slot (else a rejection
* would leak it and, after MAX_ACTIVE failures, brick URL import until
@@ -46,24 +50,7 @@ async function withDownload<T>(url: string, consume: (dl: Downloaded) => Promise
try {
const tmp = await mkdtemp(path.join(os.tmpdir(), "cut-dl-"));
try {
- const meta = await runYtDlp(url.trim(), tmp);
- // Pick the largest media file left behind (the merged output).
- const names = (await readdir(tmp)).filter((f) => MEDIA_EXT.test(f));
- if (names.length === 0) throw new Error("Nothing downloadable was found at that URL.");
- const sized = await Promise.all(
- names.map(async (n) => ({ n, size: (await stat(path.join(tmp, n))).size }))
- );
- const file = sized.sort((a, b) => b.size - a.size)[0].n;
- return await consume({
- file: path.join(tmp, file),
- title: (meta.title || "Imported clip").slice(0, 120),
- source: {
- url: meta.webpage_url || url.trim(),
- title: meta.title,
- uploader: meta.uploader,
- uploadDate: meta.upload_date,
- },
- });
+ return await consume(await download(url.trim(), tmp));
} finally {
void rm(tmp, { recursive: true, force: true });
}
@@ -72,27 +59,139 @@ async function withDownload<T>(url: string, consume: (dl: Downloaded) => Promise
}
}
-/** Download a URL into the shared Library (the Library panel's import box). */
-export async function importFromUrl(url: string): Promise<LibraryAsset> {
- return withDownload(url, (dl) => addDownloaded(dl.file, dl.title, dl.source));
+/** Download a URL into the shared Library (the Library panel's import box).
+ * A multi-photo tweet lands as one asset per photo. */
+export async function importFromUrl(url: string): Promise<LibraryAsset[]> {
+ return withDownload(url, async (dl) => {
+ const assets: LibraryAsset[] = [];
+ for (const f of dl.files) assets.push(await addDownloaded(f.file, f.title, dl.source));
+ return assets;
+ });
}
/** Download a URL straight into a project's media folder (the chat's
- * import_url tool). Returns the project file name; the client builds and
- * registers the asset. */
+ * import_url tool). Returns the project file names; the client builds and
+ * registers the assets. */
export async function importUrlToProject(
projectId: string,
url: string
-): Promise<{ fileName: string; title: string; source: LibrarySource }> {
+): Promise<{ files: { fileName: string; title: string }[]; source: LibrarySource; text?: string }> {
if (!(await readProject(projectId))) throw new Error("Project not found.");
return withDownload(url, async (dl) => {
await mkdir(mediaDir(projectId), { recursive: true });
- const dest = await uniqueName(path.basename(dl.file), (n) => projectMediaPath(projectId, n));
- await copyFile(dl.file, projectMediaPath(projectId, dest));
- return { fileName: dest, title: dl.title, source: dl.source };
+ const files: { fileName: string; title: string }[] = [];
+ for (const f of dl.files) {
+ const dest = await uniqueName(path.basename(f.file), (n) => projectMediaPath(projectId, n));
+ await copyFile(f.file, projectMediaPath(projectId, dest));
+ files.push({ fileName: dest, title: f.title });
+ }
+ return { files, source: dl.source, text: dl.text };
});
}
+/** yt-dlp handles anything with a video/audio stream. A direct image link
+ * skips it (yt-dlp rejects plain images), and a tweet it can't pull anything
+ * from may still carry photos, so those fall through to the photo fetch; its
+ * failure rethrows the yt-dlp error, which names the real cause. */
+async function download(url: string, tmp: string): Promise<Downloaded> {
+ if (IMAGE_URL_RE.test(url)) return downloadDirectImage(url, tmp);
+ try {
+ return await downloadMedia(url, tmp);
+ } catch (e) {
+ const tweetId = TWEET_RE.exec(url)?.[1];
+ if (!tweetId) throw e;
+ try {
+ return await downloadTweetPhotos(tweetId, url, tmp);
+ } catch {
+ throw e;
+ }
+ }
+}
+
+async function downloadMedia(url: string, tmp: string): Promise<Downloaded> {
+ const meta = await runYtDlp(url, tmp);
+ // Pick the largest media file left behind (the merged output).
+ const names = (await readdir(tmp)).filter((f) => MEDIA_EXT.test(f));
+ if (names.length === 0) throw new Error("Nothing downloadable was found at that URL.");
+ const sized = await Promise.all(
+ names.map(async (n) => ({ n, size: (await stat(path.join(tmp, n))).size }))
+ );
+ const file = sized.sort((a, b) => b.size - a.size)[0].n;
+ return {
+ files: [{ file: path.join(tmp, file), title: (meta.title || "Imported clip").slice(0, 120) }],
+ source: {
+ url: meta.webpage_url || url,
+ title: meta.title,
+ uploader: meta.uploader,
+ uploadDate: meta.upload_date,
+ },
+ };
+}
+
+const IMAGE_URL_RE = /\.(png|jpe?g|webp|gif|avif|bmp)(?:$|\?)/i;
+
+async function downloadDirectImage(url: string, dir: string): Promise<Downloaded> {
+ const res = await fetch(url);
+ if (!res.ok) throw new Error("Could not download that image.");
+ const name = path.basename(new URL(url).pathname) || "image.jpg";
+ const file = path.join(dir, name);
+ await writeFile(file, Buffer.from(await res.arrayBuffer()));
+ return { files: [{ file, title: name.slice(0, 120) }], source: { url } };
+}
+
+const TWEET_RE = /^https?:\/\/(?:www\.)?(?:x|twitter)\.com\/[^/]+\/status\/(\d+)/i;
+
+/** Fetch all of a tweet's photos through X's embed endpoint, plus the post
+ * text. The endpoint is auth-free but requires the token its embed script
+ * derives from the id — same float math here, precision loss included. */
+async function downloadTweetPhotos(id: string, url: string, dir: string): Promise<Downloaded> {
+ const token = ((Number(id) / 1e15) * Math.PI).toString(36).replace(/(0+|\.)/g, "");
+ const res = await fetch(`https://cdn.syndication.twimg.com/tweet-result?id=${id}&token=${token}`);
+ if (!res.ok) throw new Error("Could not read the tweet.");
+ const tweet = (await res.json()) as {
+ text?: string;
+ user?: { name?: string; screen_name?: string };
+ photos?: { url?: string }[];
+ };
+ const photoUrls = (tweet.photos ?? []).map((p) => p.url).filter((u): u is string => !!u);
+ if (photoUrls.length === 0) throw new Error("No photo was found in this tweet.");
+ const handle = tweet.user?.screen_name;
+ // The endpoint HTML-escapes the post text; it becomes titles and chat text.
+ const text = (tweet.text || "")
+ .replace(/&(amp|lt|gt|quot|#39);/g, (_, e) =>
+ ({ amp: "&", lt: "<", gt: ">", quot: '"', "#39": "'" })[e as string] ?? ""
+ )
+ .replace(/\s+/g, " ")
+ .trim();
+ const baseTitle = (text || `Photo from ${handle ? `@${handle}` : "X"}`).slice(0, 120);
+ const files: Downloaded["files"] = [];
+ for (const [i, photoUrl] of photoUrls.entries()) {
+ // pbs.twimg.com serves the original resolution behind name=orig.
+ let img = await fetch(`${photoUrl}${photoUrl.includes("?") ? "&" : "?"}name=orig`);
+ if (!img.ok) img = await fetch(photoUrl);
+ if (!img.ok) throw new Error("Could not download the tweet's photos.");
+ const ext = (
+ /\.(png|jpe?g|webp|gif)(?:$|\?)/i.exec(photoUrl)?.[1] ||
+ (img.headers.get("content-type")?.includes("png") ? "png" : "jpg")
+ ).toLowerCase();
+ const file = path.join(dir, `${id}-${i + 1}.${ext}`);
+ await writeFile(file, Buffer.from(await img.arrayBuffer()));
+ files.push({
+ file,
+ title: photoUrls.length > 1 ? `${baseTitle} (${i + 1}/${photoUrls.length})` : baseTitle,
+ });
+ }
+ return {
+ files,
+ source: {
+ url,
+ title: text || undefined,
+ uploader: handle ? `@${handle}` : tweet.user?.name,
+ },
+ text: text || undefined,
+ };
+}
+
function runYtDlp(url: string, dir: string): Promise<YtMeta> {
return new Promise((resolve, reject) => {
const p = spawn("yt-dlp", [