react-native-video-processing

npm version npm package

Trim, crop, compress, merge, reverse and thumbnail videos on iOS and Android, using the platforms’ own media stacks: AVFoundation + Core Image on iOS, Media3 Transformer on Android. No ffmpeg binaries.

Upgrading from 1.x? v3 is a rewrite with a new API. See MIGRATION.md.

Requirements

  Minimum
React Native 0.81, New Architecture (tested on 0.88)
iOS 15.1
Android API 24

Installation

v3 is in beta, so install it from the beta tag (the default latest tag is still 1.x):

npm install react-native-video-processing@beta   # or yarn / pnpm / bun
cd ios && pod install

Nothing else to link. Expo apps need a development build (npx expo prebuild); Expo Go is not supported.

Usage

import {
  getVideoInfo,
  getThumbnails,
  trim,
  crop,
  compress,
  merge,
  reverse,
  boomerang,
} from 'react-native-video-processing';

const info = await getVideoInfo('file:///path/to/video.mp4');
// { duration, width, height, rotation, frameRate, bitrate, fileSize, hasAudio, videoCodec }

const { uri } = await trim(source, { startTime: 2, endTime: 7.5 });

await compress(source, {
  width: 1280, // fits inside 1280 wide, keeping the aspect ratio; never upscales
  bitrate: 2_500_000,
  removeAudio: true,
  onProgress: (progress) => console.log(progress), // 0…1
});

await crop(source, { x: 0, y: 0, width: 720, height: 720 }, { endTime: 10 });
await merge([first, second, third]);
await reverse(source, { startTime: 0, endTime: 3 });
await boomerang(source, { endTime: 2 });

const thumbnails = await getThumbnails(source, { count: 10 }, { maxHeight: 120 });

Every operation writes an H.264/AAC .mp4 to the app’s cache directory and resolves with its file:// URI. Move it somewhere permanent if you need to keep it.

Sources

A source can be a file:// URI or absolute path, an http(s):// URL (downloaded first), a content:// URI on Android, { uri }, or a require()d asset. ph:// Photos URIs aren’t supported; most pickers can return a file URI instead.

Cancelling

const controller = new AbortController();
const job = compress(source, { signal: controller.signal });
controller.abort(); // job rejects with code E_CANCELLED and partial output is deleted

Combining operations

trim, crop, merge, reverse and boomerang are shorthands for process, which plays a list of segments back to back in a single encode:

import { process } from 'react-native-video-processing';

await process(
  [
    { source: intro, endTime: 2 },
    { source: clip, startTime: 5, endTime: 9, crop: { x: 0, y: 280, width: 1080, height: 1080 } },
    { source: clip, startTime: 5, endTime: 9, reverse: true },
  ],
  { width: 720, bitrate: 2_000_000 }
);

The output takes the size of the first segment (after cropping), fitted inside width/height. Later segments are letterboxed to that size.

Crop rectangles are in display pixels with a top-left origin, so a portrait phone video is cropped the way it appears on screen.

Reversed segments are silent. boomerang output is silent.

Errors

Rejections carry a code:

Code Meaning
E_INVALID_OPTIONS Bad options, such as a crop outside the frame or a start past the end
E_SOURCE The source can’t be found, downloaded or read
E_CANCELLED Aborted through the signal
E_PROCESSING Decoding or encoding failed

Trimmer

import { Trimmer } from 'react-native-video-processing';

<Trimmer
  source={uri}
  minDuration={1}
  maxDuration={15}
  currentTime={playerTime} // optional playhead
  onChange={({ startTime, endTime }) => seek(startTime)}
  onChangeEnd={(range) => setRange(range)}
/>;

It fetches the duration and fills its width with thumbnails. Pass duration if you already have it. Styling props: height, handleWidth, themeColor, overlayColor, playheadColor, style.

For playback, use react-native-video or expo-video.

Development

mise install    # Node, pnpm, JDK, Ruby and Maestro, as pinned in mise.toml
pnpm install
pnpm test       # Jest unit tests
pnpm typecheck && pnpm lint
pnpm example ios       # or: pnpm example android

The example app runs a self-test of every API against the clips in example/assets/fixtures on launch and shows pass/fail for each. CI runs it on an iOS simulator and an Android emulator through Maestro (example/.maestro/selftest.yaml). Regenerate the clips with scripts/make-fixtures.sh.

## If this project was helpful to you, please <html> Buy Me A Coffee </html>

License

MIT