Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(loom-importer): stop at the first unskippable missing segment and keep audio-only length

Richie McIlroyf168c45bb047
diff --git a/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts b/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
index 90938ad0c..eb0ed2900 100644
--- a/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
+++ b/apps/media-server/src/__tests__/loom-importer/import.integration.test.ts
@@ -233,6 +233,8 @@ describe("runLoomImport", () => {
"video.m3u8",
].join("\n"),
);
+ await cp(audioOnly, join(root, "hls-audio-gap"), { recursive: true });
+ await rm(join(root, "hls-audio-gap", "audio-001.ts"));
ffmpeg(
[
"-f",
@@ -513,6 +515,29 @@ describe("runLoomImport", () => {
);
}, 60_000);
+ test("keeps the full length of an audio-only recording with a missing segment", async () => {
+ const job = await run(
+ request(
+ "hls-audio-gap",
+ `${base}/media/hls-audio-gap/master.m3u8?${SIGNATURE}`,
+ ),
+ );
+ expect(job?.phase).toBe("complete");
+ const imported = join(root, "audio-gap.mp4");
+ await writeFile(
+ imported,
+ uploads.get("hls-audio-gap/result.mp4") ?? new Uint8Array(),
+ );
+ const output = probe(imported);
+ expect(Number(output.format.duration)).toBeCloseTo(7, 0);
+ expect(
+ Number(
+ output.streams.find((stream) => stream.codec_type === "video")
+ ?.start_time,
+ ),
+ ).toBe(0);
+ }, 60_000);
+
test("imports an Opus-only file as an MP4 with AAC audio", async () => {
const job = await run(
request("opus-only", `${base}/media/opus-only.webm?${SIGNATURE}`),
diff --git a/apps/media-server/src/loom-importer/job.ts b/apps/media-server/src/loom-importer/job.ts
index 7f3685fd0..c12854ca5 100644
--- a/apps/media-server/src/loom-importer/job.ts
+++ b/apps/media-server/src/loom-importer/job.ts
@@ -176,6 +176,7 @@ function progressUpdate(
interface PreparedSource {
input: string;
inputArgs: string[];
+ skippedSeconds: number;
}
async function prepareSource(
@@ -203,7 +204,7 @@ async function prepareSource(
);
if (result.bytes > maxBytes)
throw new Error("Loom imports are limited to 10 GB");
- return { input: sourcePath, inputArgs: [] };
+ return { input: sourcePath, inputArgs: [], skippedSeconds: 0 };
}
const manifestDir = join(workDir, "manifest");
const segmentDir = join(workDir, "segments");
@@ -243,6 +244,7 @@ async function prepareSource(
return {
input: prefetched.masterPath,
inputArgs: PLAYLIST_INPUT_ARGS(await getFfmpegHlsCapabilities()),
+ skippedSeconds: prefetched.skippedSeconds,
};
}
@@ -370,7 +372,9 @@ export async function runLoomImport(
source.input,
outputPath,
codecs.audio,
- codecs.duration,
+ codecs.duration === null
+ ? null
+ : codecs.duration + source.skippedSeconds,
)
: buildRemuxArgs(
source.inputArgs,
diff --git a/apps/media-server/src/loom-importer/streaming.ts b/apps/media-server/src/loom-importer/streaming.ts
index f82e4812a..8a8ec5dff 100644
--- a/apps/media-server/src/loom-importer/streaming.ts
+++ b/apps/media-server/src/loom-importer/streaming.ts
@@ -186,6 +186,7 @@ export interface PrefetchResult {
masterPath: string;
bytes: number;
segments: number;
+ skippedSeconds: number;
}
const MISSING_SEGMENT_FRACTION = 0.01;
@@ -251,11 +252,14 @@ export async function prefetchMaterializedHls(
tracks.map(async (track) => {
const content = await readFile(track.path, "utf8");
const resources = listMediaPlaylistResources(content);
+ const lines = content.split(/\r?\n/);
return {
...track,
- lines: content.split(/\r?\n/),
+ lines,
resources,
- segments: resources.filter((resource) => !resource.attribute).length,
+ durations: resources
+ .filter((resource) => !resource.attribute)
+ .map((resource) => segmentDuration(lines, resource.lineIndex)),
};
}),
);
@@ -263,6 +267,7 @@ export async function prefetchMaterializedHls(
playlist.resources.map((resource, index) => ({
playlist,
resource,
+ duration: segmentDuration(playlist.lines, resource.lineIndex),
path: join(
workDir,
`${playlist.name}-${String(index).padStart(6, "0")}${segmentExtension(resource.url)}`,
@@ -300,12 +305,12 @@ export async function prefetchMaterializedHls(
throw error;
missing.add(download);
progress.completedSegments++;
- const lost = [...missing].filter(
- (entry) => entry.playlist === download.playlist,
- ).length;
- if (lost > missingSegmentLimit(download.playlist.segments)) {
+ const lost = [...missing]
+ .filter((entry) => entry.playlist === download.playlist)
+ .map((entry) => entry.duration);
+ if (!canSkipMissingSegments(lost, download.playlist.durations)) {
const tooMany = new Error(
- `Loom is missing ${lost} of ${download.playlist.segments} ${download.playlist.name} segments`,
+ `Loom is missing ${lost.length} of ${download.playlist.durations.length} ${download.playlist.name} segments`,
);
failure.abort(tooMany);
throw tooMany;
@@ -340,19 +345,6 @@ export async function prefetchMaterializedHls(
options.signal.throwIfAborted();
throw failure.signal.reason ?? rejected.reason;
}
- for (const playlist of playlists) {
- const media = downloads.filter(
- (download) =>
- download.playlist === playlist && !download.resource.attribute,
- );
- const durationOf = (download: (typeof downloads)[number]) =>
- segmentDuration(playlist.lines, download.resource.lineIndex);
- const lost = media.filter((download) => missing.has(download));
- if (!canSkipMissingSegments(lost.map(durationOf), media.map(durationOf)))
- throw new Error(
- `Loom is missing ${lost.length} of ${media.length} ${playlist.name} segments`,
- );
- }
for (const download of downloads) {
if (missing.has(download)) {
removeSegmentLines(download.playlist.lines, download.resource.lineIndex);
@@ -374,11 +366,16 @@ export async function prefetchMaterializedHls(
const audioPlaylist = selection.audio
? localPlaylists.get(selection.audio)
: undefined;
- if (
- audioPlaylist &&
+ const audioOnly =
+ audioPlaylist !== undefined &&
(playlistBytes.get("video") ?? 0) === 0 &&
- (playlistBytes.get("audio") ?? 0) > 0
- ) {
+ (playlistBytes.get("audio") ?? 0) > 0;
+ const skippedSeconds = [...missing]
+ .filter(
+ (download) => download.playlist.name === (audioOnly ? "audio" : "video"),
+ )
+ .reduce((sum, download) => sum + download.duration, 0);
+ if (audioOnly && audioPlaylist) {
await writeFile(localMasterPath, await readFile(audioPlaylist, "utf8"));
} else if (selection.masterLines.length > 0) {
await writeFile(
@@ -404,5 +401,6 @@ export async function prefetchMaterializedHls(
masterPath: localMasterPath,
bytes: progress.bytes,
segments: downloads.length,
+ skippedSeconds,
};
}