'React native - Which is the best way to compress video before upload to server?

We are using ffmpeg library (https://github.com/tanersener/react-native-ffmpeg) but with large videos this task is taking too long time, up to 4 minutes or more in some cases.

The ffmpeg command we are using is: ffmpeg -y -i {inputVideo.mp4} -c:v libx264 -crf 24 -preset ultrafast -vf scale=-2:720,format=yuv420p -movflags +faststart {outputVideo.mp4}

We want to have a quickly (and perfomant) compression so the user experience will not be affected



Solution 1:[1]

Try this library and see if it works better for you.

note: this library does not accept as much option as ffmpeg. but also has way less size.

Solution 2:[2]

This Library

This works in the background of the app too. So when you close the app, it should continue compression. Here's an example on how I use it:

Import the Methods

import {
  Video as VideoCompress,
  Image as ImageCompress,
} from "react-native-compressor";
Compress either video or image

if (media.type === "image") {
  await ImageCompress.compress(
    media.uri,
    {
      compressionMethod: "auto",
    },
    (progress) => {
      console.log({ compression: progress });
    }
  ).then(async (compressedFileUrl) => {
  // do something with compressed image file
  });
}
      
if (media.type === "video") {
  await VideoCompress.compress(
    media.uri,
    {
      compressionMethod: "auto",
    },
    (progress) => {
      console.log({ compression: progress });
    }
  ).then(async (compressedFileUrl) => {
  // do something with compressed video file
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alireza Hadjar
Solution 2 H. Khan