Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add desktopSegments recording mode and segments playlist

Richie McIlroya216d3237254
diff --git a/apps/web/app/api/desktop/[...route]/video.ts b/apps/web/app/api/desktop/[...route]/video.ts
index 861b3d940..5a159aa09 100644
--- a/apps/web/app/api/desktop/[...route]/video.ts
+++ b/apps/web/app/api/desktop/[...route]/video.ts
@@ -33,7 +33,11 @@ app.get(
"query",
z.object({
recordingMode: z
- .union([z.literal("hls"), z.literal("desktopMP4")])
+ .union([
+ z.literal("hls"),
+ z.literal("desktopMP4"),
+ z.literal("desktopSegments"),
+ ])
.optional(),
isScreenshot: z.coerce.boolean().default(false),
videoId: z.string().optional(),
@@ -178,7 +182,9 @@ app.get(
? { type: "local" as const }
: recordingMode === "desktopMP4"
? { type: "desktopMP4" as const }
- : undefined,
+ : recordingMode === "desktopSegments"
+ ? { type: "desktopSegments" as const }
+ : undefined,
isScreenshot,
bucket: customBucket?.id,
public: serverEnv().CAP_VIDEOS_DEFAULT_PUBLIC,
diff --git a/apps/web/app/api/playlist/route.ts b/apps/web/app/api/playlist/route.ts
index 8bf3b3b90..2808bc894 100644
--- a/apps/web/app/api/playlist/route.ts
+++ b/apps/web/app/api/playlist/route.ts
@@ -28,7 +28,16 @@ export const dynamic = "force-dynamic";
const GetPlaylistParams = Schema.Struct({
videoId: Video.VideoId,
- videoType: Schema.Literal("video", "audio", "master", "mp4", "raw-preview"),
+ videoType: Schema.Literal(
+ "video",
+ "audio",
+ "master",
+ "mp4",
+ "raw-preview",
+ "segments-master",
+ "segments-video",
+ "segments-audio",
+ ),
thumbnail: Schema.OptionFromUndefinedOr(Schema.String),
fileType: Schema.OptionFromUndefinedOr(Schema.String),
});
@@ -140,6 +149,112 @@ const getPlaylistResponse = (
.pipe(Effect.map(HttpServerResponse.redirect));
}
+ if (
+ urlParams.videoType === "segments-master" ||
+ urlParams.videoType === "segments-video" ||
+ urlParams.videoType === "segments-audio"
+ ) {
+ const segSource = new Video.SegmentsSource({
+ videoId: video.id,
+ ownerId: video.ownerId,
+ });
+
+ const manifestKey = segSource.getManifestKey();
+ const manifestContent = yield* s3.getObject(manifestKey).pipe(
+ Effect.andThen(
+ Option.match({
+ onNone: () => Effect.fail(new HttpApiError.NotFound()),
+ onSome: (c) => Effect.succeed(c),
+ }),
+ ),
+ );
+
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(manifestContent);
+ } catch {
+ return yield* Effect.fail(new HttpApiError.InternalServerError());
+ }
+
+ const manifest = yield* Schema.decodeUnknown(Video.SegmentManifest)(
+ parsed,
+ ).pipe(Effect.mapError(() => new HttpApiError.InternalServerError()));
+
+ if (urlParams.videoType === "segments-master") {
+ const videoPlaylistUrl = `${serverEnv().WEB_URL}/api/playlist?videoId=${video.id}&videoType=segments-video`;
+ const audioPlaylistUrl = manifest.audio_init_uploaded
+ ? `${serverEnv().WEB_URL}/api/playlist?videoId=${video.id}&videoType=segments-audio`
+ : null;
+
+ let playlist = "#EXTM3U\n#EXT-X-VERSION:7\n";
+ if (audioPlaylistUrl) {
+ playlist += `#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="default",DEFAULT=YES,AUTOSELECT=YES,URI="${audioPlaylistUrl}"\n`;
+ playlist += `#EXT-X-STREAM-INF:BANDWIDTH=2000000,AUDIO="audio"\n`;
+ } else {
+ playlist += "#EXT-X-STREAM-INF:BANDWIDTH=2000000\n";
+ }
+ playlist += videoPlaylistUrl + "\n";
+
+ return HttpServerResponse.text(playlist, {
+ headers: {
+ ...CACHE_CONTROL_HEADERS,
+ "Content-Type": "application/vnd.apple.mpegurl",
+ },
+ });
+ }
+
+ const isVideo = urlParams.videoType === "segments-video";
+ const initKey = isVideo
+ ? segSource.getVideoInitKey()
+ : segSource.getAudioInitKey();
+ const rawSegments = isVideo
+ ? manifest.video_segments
+ : manifest.audio_segments;
+ const segments = rawSegments.map(Video.normalizeSegmentEntry);
+ const initUploaded = isVideo
+ ? manifest.video_init_uploaded
+ : manifest.audio_init_uploaded;
+
+ if (!initUploaded || segments.length === 0) {
+ return yield* Effect.fail(new HttpApiError.NotFound());
+ }
+
+ const initUrl = yield* s3.getSignedObjectUrl(initKey);
+ const segmentUrls = yield* Effect.all(
+ segments.map((seg) => {
+ const key = isVideo
+ ? segSource.getVideoSegmentKey(seg.index)
+ : segSource.getAudioSegmentKey(seg.index);
+ return s3.getSignedObjectUrl(key);
+ }),
+ { concurrency: "unbounded" },
+ );
+
+ const targetDuration = Math.ceil(
+ segments.reduce((max, seg) => Math.max(max, seg.duration), 0),
+ );
+
+ let playlist = `#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-TARGETDURATION:${Math.max(targetDuration, 1)}\n#EXT-X-MEDIA-SEQUENCE:1\n`;
+ playlist += `#EXT-X-MAP:URI="${initUrl}"\n`;
+
+ for (let i = 0; i < segmentUrls.length; i++) {
+ const dur = segments[i]?.duration ?? 3.0;
+ playlist += `#EXTINF:${dur.toFixed(3)},\n`;
+ playlist += segmentUrls[i] + "\n";
+ }
+
+ if (manifest.is_complete) {
+ playlist += "#EXT-X-ENDLIST\n";
+ }
+
+ return HttpServerResponse.text(playlist, {
+ headers: {
+ ...CACHE_CONTROL_HEADERS,
+ "Content-Type": "application/vnd.apple.mpegurl",
+ },
+ });
+ }
+
if (Option.isNone(customBucket)) {
let redirect = `${video.ownerId}/${video.id}/combined-source/stream.m3u8`;
@@ -290,6 +405,7 @@ const getPlaylistResponse = (
};
}),
),
+ { concurrency: "unbounded" },
);
const generatedPlaylist = generateM3U8Playlist(chunksUrls);