Batch processing
compressBatch(inputs: readonly BatchInput[], options?: BatchOptions): Promise<string[]> validates all input strings and JavaScript options first, then processes videos sequentially in an isolated session. Each input is { inputUri: string, options?: CompressOptions }. Native readability, duration, permissions, codec support, and filename collisions are checked as each item is reached.
Options are an optional signal: AbortSignal and onProgress: (event: BatchProgress) => void callback. Progress contains the zero-based index, total, fractional itemProgress, and overall progress. Aggregate progress is (index + itemProgress) / total, weighted equally per item, not by file size or duration. Returned paths retain input order.
import { compressBatch } from "react-native-media-transformer";
export async function compressVideos(
inputUris: readonly string[],
signal: AbortSignal,
) {
try {
const paths = await compressBatch(
inputUris.map((inputUri) => ({
inputUri,
options: { quality: "medium" as const },
})),
{
signal,
onProgress: ({ index, total, progress }) =>
console.log(
`Video ${index + 1}/${total}: ${Math.round(progress * 100)}% overall`,
),
},
);
return { status: "complete" as const, paths };
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
return { status: "cancelled" as const };
}
throw error;
}
}Create an AbortController in your app, pass controller.signal to this helper, and call controller.abort() from a cancel action while it is running. Handle the helper's non-abort rejections too. cancelCompress() does not cancel a batch.
A failure stops the batch and rejects its promise, without a partial results array. Earlier completed files remain in cache; failed/cancelled temporary files are removed. Use unique output filenames per item, or a repeated fixed name can cause a collision. Manage individual session calls in the app if you need per-file recovery.
An empty batch resolves to []. A pre-aborted signal rejects before processing, including for an empty batch. When abortion is observed, the active export is cancelled, subsequent items do not start, and the batch rejects with name: 'AbortError'. A file completed immediately before abortion can remain on disk.