react-native-create-thumbnail

repository·master·Indexed 43 days ago

https://github.com/souvik-ghosh/react-native-create-thumbnail

A React Native wrapper for generating video thumbnails on iOS and Android using native APIs (AVAssetImageGenerator and MediaMetadataRetriever). Version 2.2.0 supports both local and remote video sources and includes built-in storage and cache management.

Tokens
1.6K
Snippets
8
Records
12
Agent score
55%

What's inside react-native-create-thumbnail

  1. Publish the library as a Maven dependency

    master

    If you intend to publish this library as a Maven dependency, you must complete these steps before publishing a new version to npm:

    1. Ensure the Android SDK and NDK are installed on your system.
    2. Create or update a local.properties file in the android directory to point to your Android SDK and NDK locations.
    3. Delete the existing maven folder to ensure a clean build.
    4. Execute the Gradle task to install archives.
    5. Verify that the newly generated files are present in the maven folder and contain the correct version number.
    ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
    sdk.dir=/Users/{username}/Library/Android/sdk
    ./gradlew installArchives
  2. Build and run the iOS app

    master

    For iOS, you must first install CocoaPods dependencies.

    1. If this is your first time running the project, install CocoaPods using Ruby bundler:
      bundle install
    2. Every time you update native dependencies, run:
      bundle exec pod install

    Once dependencies are installed, run the following command to launch the app on an iOS Simulator or connected device:

    # Using npm
    npm run ios
    
    # OR using Yarn
    yarn ios
  3. Install react-native-create-thumbnail

    master

    To use this library in your React Native project, install it via npm or yarn and ensure native dependencies are linked.

    Installation

    npm i react-native-create-thumbnail

    or

    yarn add react-native-create-thumbnail

    Linking Native Code

    For React Native 0.60+ (Autolinking):

    cd ios && pod install

    For React Native < 0.60:

    react-native link react-native-create-thumbnail
  4. Android permissions and limitations for react-native-create-thumbnail

    master

    Required Permissions

    Ensure your Android application has the following permissions configured:

    • READ_EXTERNAL_STORAGE
    • WRITE_EXTERNAL_STORAGE

    Limitations

    • Remote Videos: Not supported on Android sdk_version < 14.
    • Expo Go: This is a Native Module, so it will not work in Expo Go.
    • Codec Support: Thumbnail generation depends on native APIs; it can only generate thumbnails from video formats/codecs supported by the device's OS.
  5. Use createThumbnail() to generate video thumbnails

    master

    Import createThumbnail to generate a thumbnail from a local or remote video file. The function returns a Promise that resolves with a response object containing the thumbnail's metadata or rejects with an error.

    import { createThumbnail } from "react-native-create-thumbnail";
    
    createThumbnail({
      url: '<path to video file>',
      timeStamp: 10000,
    })
      .then(response => console.log({ response }))
      .catch(err => console.log({ err }));
  6. Understand the createThumbnail response object

    master

    The Promise returned by createThumbnail resolves to an object containing the following properties:

    PropertyTypeDescription
    pathStringPath to generated thumbnail
    sizeNumberSize (in bytes) of thumbnail
    mimeStringMimetype of thumbnail
    widthNumberThumbnail width
    heightNumberThumbnail height
  7. Configure the createThumbnail request object

    master

    The createThumbnail function accepts a request object with the following properties:

    PropertyTypeDescription
    urlString (required)Path to video file (local or remote)
    timeStampNumber (default 0)Thumbnail timestamp (in milliseconds)
    formatString (default jpeg)Thumbnail format: jpeg or png
    maxWidthNumber (default 512)Max thumbnail width in px
    maxHeightNumber (default 512)Max thumbnail height in px
    dirSizeNumber (default 100)Max cache directory size (in MB). When full, old thumbnails are deleted to clear ~half the size.
    headersObjectHeaders to load the video with (e.g., { Authorization: 'token' })
    cacheNameString (optional)Cache name to avoid duplicate generation. If a thumbnail with this name exists, it is returned instead of re-generating.
    timeToleranceMsNumber (default 2000, iOS only)Time tolerance in ms for the system to pick the best matching video frame
    onlySyncedFramesBoolean (default true, Android only)true: retrieve a sync frame closest to timestamp. false: retrieve closest frame regardless of sync status.
  8. Use createThumbnail() to generate thumbnails

    master

    The createThumbnail function is the primary API for generating thumbnails from media files. It is exported as a named export from the package. It wraps the underlying CreateThumbnail native module.

    import createThumbnail from 'react-native-create-thumbnail';
    
    // Usage example (exact signature depends on native implementation):
    // createThumbnail(requestObject).then(responseObject => ...)