react-native-compressor

repository·main·Indexed 23 days ago

https://github.com/numandev1/react-native-compressor

A lightweight React Native package for compressing images, videos, and audio files with quality similar to WhatsApp. It provides automatic and manual compression methods, background upload support (PUT/POST/MULTIPART), file downloading, and video thumbnail generation. Version 2.0.3 adds minimal overhead (~50KB to APK size) compared to FFmpeg and supports both React Native CLI and Managed Expo projects.

Tokens
7.6K
Snippets
17
Records
63
Agent score
78%

What's inside react-native-compressor

  1. Manual Installation for Android

    main

    If automatic linking fails, follow these steps for Android:

    1. Modify MainActivity.java:

      • Add import com.reactnativecompressor.CompressorPackage; to the imports.
      • Add new CompressorPackage() to the list returned by the getPackages() method.
    2. Modify android/settings.gradle: Append these lines:

      include ':react-native-compressor'
      project(':react-native-compressor').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-compressor/android')
    3. Modify android/app/build.gradle: Insert this line inside the dependencies block:

      compile project(':react-native-compressor')
  2. Run the react-native-compressor Expo example

    main

    To run the provided Expo example project, follow these steps:

    1. Install the project dependencies:

      npm install
    2. Start the Expo development server:

      npx expo start

    Once the server is running, you can open the app in a development build, an Android emulator, an iOS simulator, or Expo Go.

    npm install
    npx expo start
  3. Build and run the iOS app

    main

    For iOS, you must first install CocoaPods dependencies. If this is your first time or you have updated native dependencies, run bundle exec pod install. Then, run the following command to launch the app on an iOS Simulator or connected device:

    # Install CocoaPods dependencies
    bundle exec pod install
    
    # Using npm
    npm run ios
    
    # OR using Yarn
    yarn ios
  4. Reload the application

    main

    If you need to forcefully reload the app to reset its state, use the following platform-specific shortcuts:

    • Android: Press the <kbd>R</kbd> key twice or select "Reload" from the Dev Menu (accessed via <kbd>Ctrl</kbd> + <kbd>M</kbd> on Windows/Linux or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> on macOS).
    • iOS: Press <kbd>R</kbd> in the iOS Simulator.
  5. Install in Managed Expo

    main

    To use react-native-compressor in a Managed Expo project, follow these steps:

    1. Install the package:
    expo install react-native-compressor
    1. Add the Compressor plugin to your Expo config (app.json, app.config.json, or app.config.js):
    {
      "name": "my app",
      "plugins": ["react-native-compressor"]
    }
    1. Run prebuild to compile the mods:
    expo prebuild
    1. Build a new binary with EAS:
    eas build
  6. Compress Audio

    main

    Use the Audio module to compress audio files. You can use preset quality levels or manual bitrate/samplerate settings.

    Using Quality Presets

    import { Audio } from 'react-native-compressor';
    
    const result = await Audio.compress(
      'file://path_of_file/file_example_MP3_2MG.wav',
      { quality: 'medium' }
    );

    Manual Settings

    import { Audio } from 'react-native-compressor';
    
    const result = await Audio.compress(
      'file://path_of_file/file_example_MP3_2MG.wav',
      {
        bitrate: 64000,
        samplerate: 44100,
        channels: 1,
      }
    );
  7. Generate a temporary file path

    main

    Use generateFilePath to create a random file path within the cache directory for saving files.

    import { generateFilePath } from 'react-native-compressor';
    
    const randomFilePath = await generateFilePath('mp4'); // file://file_path.mp4
    import { generateFilePath } from 'react-native-compressor';
    
    const randomFilePathForSaveFile = await generateFilePath('mp4'); // file://file_path.mp4