expo-trim-video
v1.0.1
Published
A video trimmer package for Expo apps
Maintainers
Readme
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-videoUsage
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
- File URIs:
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 URIINVALID_START: Start time is negativeINVALID_END: End time exceeds video durationINVALID_RANGE: Start time is greater than or equal to end timeINVALID_URI: Invalid URI format (iOS)FILE_NOT_FOUND: Video file not found or invalidTRIM_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:
- Enter a video URI (file path or content URI)
- Set start and end times
- Tap "Trim Video" to process
To run the example:
cd example
npm install
# For Android
npx expo run:android
# For iOS
npx expo run:iosTechnical Details
Android Implementation
The Android implementation uses:
MediaExtractorto read video dataMediaMuxerto write the trimmed outputMediaMetadataRetrieverto 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:
AVAssetto load and analyze video filesAVAssetExportSessionfor efficient video trimmingCMTimefor 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
AVAssetExportSessionfor hardware-accelerated processing - Setting precise time ranges with
CMTimeRange - Preserving original video quality and metadata
Troubleshooting
Common Issues
"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
"End time exceeds video duration":
- Check the actual video duration
- Ensure end time is less than video length
"Invalid range" error:
- Ensure start < end
- Both times must be positive
"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-pickerfor user-selected filesexpo-media-libraryfor accessing device mediaexpo-file-systemfor app-specific filesexpo-image-pickerfor camera/gallery videos
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
