Skip to content

ali-kamranch/expo-trim-video

 
 

Repository files navigation

expo-trim-video

A native Expo module for trimming videos on Android and iOS devices.

Features

  • ✅ Trim videos by specifying start and end times
  • ✅ Support for various video formats (MP4, MOV, etc.)
  • ✅ Handle file:// and content:// URIs
  • ✅ Error handling for invalid inputs and file issues
  • ✅ Fast video processing using native platform APIs
  • ✅ Cross-platform support for Android and iOS

Installation

npm install expo-trim-video

Usage

import { trimVideo } from 'expo-trim-video';

// Trim a video from 5 seconds to 15 seconds
try {
  const result = await trimVideo({
    uri: 'file:///path/to/your/video.mp4', // or content:// URI
    start: 5,  // Start time in seconds
    end: 15    // End time in seconds
  });
  
  console.log('Trimmed video saved to:', result.uri);
} catch (error) {
  console.error('Error trimming video:', error.message);
}

API

trimVideo(options: TrimVideoOptions): Promise<TrimVideoResult>

Trims a video file according to the specified options.

Parameters

  • options.uri (string): The URI of the video file to trim. Supports:

    • File URIs: file:///path/to/video.mp4
    • Content URIs (Android): content://...
    • Photos URIs (iOS): ph://...
    • Absolute paths: /storage/emulated/0/Movies/video.mp4
  • options.start (number): Start time in seconds (must be >= 0)

  • options.end (number): End time in seconds (must be > start and <= video duration)

Returns

Promise that resolves to:

{
  uri: string; // File URI of the trimmed video
}

Error Codes

  • INVALID_ARGUMENTS: Missing or invalid URI
  • INVALID_START: Start time is negative
  • INVALID_END: End time exceeds video duration
  • INVALID_RANGE: Start time is greater than or equal to end time
  • INVALID_URI: Invalid URI format (iOS)
  • FILE_NOT_FOUND: Video file not found or invalid
  • TRIM_ERROR: General error during video processing

Platform Support

  • Android: Full support using MediaExtractor/MediaMuxer
  • iOS: Full support using AVFoundation
  • Web: Not supported (returns error)

Example App

The example app demonstrates how to use the module:

  1. Enter a video URI (file path or content URI)
  2. Set start and end times
  3. Tap "Trim Video" to process

To run the example:

cd example
npm install

# For Android
npx expo run:android

# For iOS
npx expo run:ios

Technical Details

Android Implementation

The Android implementation uses:

  • MediaExtractor to read video data
  • MediaMuxer to write the trimmed output
  • MediaMetadataRetriever to validate video duration and get metadata

The module preserves:

  • Video quality (no re-encoding when possible)
  • Audio tracks
  • Video rotation metadata
  • Original codecs and formats

iOS Implementation

The iOS implementation uses:

  • AVAsset to load and analyze video files
  • AVAssetExportSession for efficient video trimming
  • CMTime for precise time handling

The module preserves:

  • Video quality using AVAssetExportPresetHighestQuality
  • Audio tracks
  • Video orientation and metadata
  • Optimal file compression

Performance

Video trimming is performed efficiently by:

Android:

  • Seeking to the start position using SEEK_TO_CLOSEST_SYNC
  • Copying video/audio samples without re-encoding
  • Processing only the required time range

iOS:

  • Using AVAssetExportSession for hardware-accelerated processing
  • Setting precise time ranges with CMTimeRange
  • Preserving original video quality and metadata

Troubleshooting

Common Issues

  1. "File not found" error:

    • Ensure the video file exists and is accessible
    • Check file permissions
    • Try using a content:// URI (Android) or ph:// URI (iOS) for media files
  2. "End time exceeds video duration":

    • Check the actual video duration
    • Ensure end time is less than video length
  3. "Invalid range" error:

    • Ensure start < end
    • Both times must be positive
  4. "Invalid URI format" (iOS):

    • Ensure the URI is properly formatted
    • Use file:// prefix for local files
    • Use ph:// for Photos library assets

Getting Video URIs

You can get video URIs using:

  • expo-document-picker for user-selected files
  • expo-media-library for accessing device media
  • expo-file-system for app-specific files
  • expo-image-picker for camera/gallery videos

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

expo video trimmer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Kotlin 32.7%
  • TypeScript 31.9%
  • Swift 21.3%
  • Ruby 8.9%
  • JavaScript 4.9%
  • C 0.3%