Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add remuxOnly option for faststart MP4 remuxing

Richie McIlroy33c2f52b48fd
diff --git a/apps/media-server/src/__tests__/routes/audio.test.ts b/apps/media-server/src/__tests__/routes/audio.test.ts
index 08ffdc65f..cb4c24d38 100644
--- a/apps/media-server/src/__tests__/routes/audio.test.ts
+++ b/apps/media-server/src/__tests__/routes/audio.test.ts
@@ -146,7 +146,10 @@ describe("POST /audio/extract", () => {
new Request("http://localhost/audio/extract", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ videoUrl: "https://example.com/video.mp4" }),
+ body: JSON.stringify({
+ videoUrl: "https://example.com/video.mp4",
+ stream: false,
+ }),
}),
);
@@ -174,7 +177,10 @@ describe("POST /audio/extract", () => {
new Request("http://localhost/audio/extract", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ videoUrl: "https://example.com/video.mp4" }),
+ body: JSON.stringify({
+ videoUrl: "https://example.com/video.mp4",
+ stream: false,
+ }),
}),
);
diff --git a/apps/media-server/src/lib/ffmpeg-video.ts b/apps/media-server/src/lib/ffmpeg-video.ts
index f63feb892..f7e6c8e93 100644
--- a/apps/media-server/src/lib/ffmpeg-video.ts
+++ b/apps/media-server/src/lib/ffmpeg-video.ts
@@ -14,6 +14,7 @@ export interface VideoProcessingOptions {
audioBitrate?: string;
crf?: number;
preset?: "ultrafast" | "fast" | "medium" | "slow";
+ remuxOnly?: boolean;
}
const DEFAULT_OPTIONS: Required<VideoProcessingOptions> = {
@@ -23,6 +24,7 @@ const DEFAULT_OPTIONS: Required<VideoProcessingOptions> = {
audioBitrate: "128k",
crf: 23,
preset: "medium",
+ remuxOnly: false,
};
export interface ThumbnailOptions {
@@ -207,8 +209,11 @@ export async function processVideo(
const opts = { ...DEFAULT_OPTIONS, ...definedOptions };
const outputTempFile = await createTempFile(".mp4");
- const videoTranscode = needsVideoTranscode(metadata, opts);
- const audioTranscode = needsAudioTranscode(metadata);
+ const remuxOnly = opts.remuxOnly;
+ const videoTranscode = remuxOnly
+ ? false
+ : needsVideoTranscode(metadata, opts);
+ const audioTranscode = remuxOnly ? false : needsAudioTranscode(metadata);
const ffmpegArgs: string[] = ["ffmpeg", "-threads", "2", "-i", inputPath];
diff --git a/apps/media-server/src/routes/audio.ts b/apps/media-server/src/routes/audio.ts
index 1a253b098..28cb63f6d 100644
--- a/apps/media-server/src/routes/audio.ts
+++ b/apps/media-server/src/routes/audio.ts
@@ -16,7 +16,7 @@ const videoUrlSchema = z.object({
const extractSchema = z.object({
videoUrl: z.string().url(),
- stream: z.boolean().optional().default(false),
+ stream: z.boolean().optional().default(true),
});
function isBusyError(err: unknown): boolean {
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 02353f888..d751e0639 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -53,6 +53,7 @@ const processSchema = z.object({
maxHeight: z.number().max(4096).optional(),
crf: z.number().min(0).max(51).optional(),
preset: z.enum(["ultrafast", "fast", "medium", "slow"]).optional(),
+ remuxOnly: z.boolean().optional(),
});
function isBusyError(err: unknown): boolean {
@@ -323,6 +324,7 @@ async function processVideoAsync(
maxHeight: options.maxHeight,
crf: options.crf,
preset: options.preset,
+ remuxOnly: options.remuxOnly,
},
(progress, message) => {
const scaledProgress = 10 + progress * 0.7;
diff --git a/apps/web/__tests__/unit/media-client.test.ts b/apps/web/__tests__/unit/media-client.test.ts
index 7264fe4cb..b99fb3467 100644
--- a/apps/web/__tests__/unit/media-client.test.ts
+++ b/apps/web/__tests__/unit/media-client.test.ts
@@ -152,7 +152,10 @@ describe("media-client", () => {
{
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ videoUrl: "https://example.com/video.mp4" }),
+ body: JSON.stringify({
+ videoUrl: "https://example.com/video.mp4",
+ stream: true,
+ }),
},
);
});
diff --git a/apps/web/app/api/upload/[...route]/multipart.ts b/apps/web/app/api/upload/[...route]/multipart.ts
index 40bd6e5e9..0c9a38923 100644
--- a/apps/web/app/api/upload/[...route]/multipart.ts
+++ b/apps/web/app/api/upload/[...route]/multipart.ts
@@ -425,6 +425,55 @@ app.post(
),
);
+ const mediaServerUrl = serverEnv().MEDIA_SERVER_URL;
+ if (video.source.type === "webMP4" && mediaServerUrl) {
+ const inputUrl = yield* bucket.getInternalSignedObjectUrl(fileKey);
+ const outputPresignedUrl = yield* bucket.getInternalPresignedPutUrl(
+ fileKey,
+ {
+ ContentType: "video/mp4",
+ CacheControl: "max-age=31536000",
+ Metadata: {
+ userId: user.id,
+ source: "cap-multipart-upload",
+ },
+ },
+ );
+
+ yield* Effect.tryPromise({
+ try: async () => {
+ const response = await fetch(
+ `${mediaServerUrl}/video/process`,
+ {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ videoId,
+ userId: user.id,
+ videoUrl: inputUrl,
+ outputPresignedUrl,
+ remuxOnly: true,
+ }),
+ },
+ );
+
+ if (!response.ok) {
+ const errorText = await response.text().catch(() => "");
+ throw new Error(
+ `Media server remux failed: ${response.status} ${errorText}`,
+ );
+ }
+ },
+ catch: (cause) =>
+ cause instanceof Error ? cause : new Error(String(cause)),
+ }).pipe(
+ Effect.catchAll((error) => {
+ console.error("Failed to queue faststart remux:", error);
+ return Effect.succeed(null);
+ }),
+ );
+ }
+
if (Option.isNone(customBucket)) {
const distributionId = serverEnv().CAP_CLOUDFRONT_DISTRIBUTION_ID;
if (distributionId) {
diff --git a/apps/web/lib/media-client.ts b/apps/web/lib/media-client.ts
index fd2ce658d..eff5bfabe 100644
--- a/apps/web/lib/media-client.ts
+++ b/apps/web/lib/media-client.ts
@@ -115,7 +115,10 @@ export async function extractAudioViaMediaServer(
const response = await fetchWithRetry(`${mediaServerUrl}/audio/extract`, {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ videoUrl }),
+ body: JSON.stringify({
+ videoUrl,
+ stream: true,
+ }),
});
if (!response.ok) {