Unable to compress video using "react-native-compressor"! any other solution?

1.3k Views Asked by At

The Process

  1. Select a video from the gallery using react-native-image-crop-picker
  2. Compress the video using react-native-compressor
  3. Upload video on S3

The problem

❌ Video compression does not work on Android

ERROR: android open failed: ENOENT (No such file or directory)

These are the Permissions I have in my android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA"/>

✅ Images and video compression work correctly in IOS.

✅ Only Image compression worked correctly in Android.

The code for video compression

  const chooseVideoFromLibrary = async () => {
    const options: Options = {
      mediaType: 'video',
    };

    try {
      setIsUploading(true);
      const response = await ImagePicker.openPicker(options);
      //Compress video -----
      const compressedVideoPath = await Video.compress(
        response.path,
        {
          compressionMethod: 'manual',
          minimumFileSizeForCompress: 1,
        })
      console.log('****compressing/SUCCESS-- ', compressedVideoPath)
      const dataToSend = { ...response, path: compressedVideoPath }

      // This is another function to handle S3 Upload
      await handleS3Upload(dataToSend);
    } catch (error) {
      console.log('Error: ', error);
    } finally {
      setIsUploading(false);
    }
  };

Note

It seems that the error appears when trying to upload the compressed video to the S3, I think that this package react-native-compressor returns an incorrect path for the compressed video on Android.

Finally, I hope this is clear to you so that you can help. If you have any idea that could help, or if you have a better solution for video compression with react native please share it with me, I'll appreciate it.

2

There are 2 best solutions below

0
ronak dholariya On

I would like to suggest library FFmpegKit for React Native.

Please follow this command to compress video or any other files.

import { FFmpegKit } from 'ffmpeg-kit-react-native';
FFmpegKit.execute('-i YOUR FILE PATH -c:v libx265 YOUR OUTPUT PATH').then(async (session) => {
  const returnCode = await session.getReturnCode();

  if (ReturnCode.isSuccess(returnCode)) {

    // SUCCESS

  } else if (ReturnCode.isCancel(returnCode)) {

    // CANCEL

  } else {

    // ERROR

  }
});

For more understanding open this link

0
Yash Gada On

Not a complete answer In such cases, I suggest the user tries to insert console.log at multiple positions to find where code execution stops. Also, you can reduce sources of error by storing the file only locally when you just want to check the issue.