light_compressor
A powerful and easy-to-use video compression plugin for Flutter built based on LightCompressor library for Android and LightCompressor_iOS library for iOS and macOS. This plugin generates a compressed MP4 video with a modified width, height, and bitrate.
The general idea of how the library works is that, extreme high bitrate is reduced while maintaining a good video quality resulting in a smaller size.
How it works
When the video file is called to be compressed, the library checks if the user wants to set a min bitrate to avoid compressing low resolution videos. This becomes handy if you don’t want the video to be compressed every time it is to be processed to avoid having very bad quality after multiple rounds of compression. The minimum bitrate set is 2mbps.
You can pass one of a 5 video qualities; very_high, high, medium, low OR very_low and the plugin will handle generating the right bitrate value for the output video.
Installation
First, add light_compressor as a dependency in your pubspec.yaml file.
Usage
yaml
dependencies:
light_compressor: ^2.2.0
light_compressor_ohos: 1.0.0
In order to start compression, just call LightCompressorOhos().startCompression() and pass the following parameters;
path: the path of the provided video file to be compressed - required.videoQuality: to allow choosing a video quality that can beVideoQuality.very_low,VideoQuality.low,VideoQuality.medium,VideoQuality.high, orVideoQuality.very_high- required.
final LightCompressorOhos _lightCompressor = LightCompressorOhos();
final Result response = await _lightCompressor.startCompression(
path: _filePath!,
videoQuality: VideoQuality.medium,
);
Result response can be one of the following;
- onSuccess: if the compression succeeded and it returns the output path if needed.
- onFailure: if the compression failed in which a failure message is returned.
if (response is OnSuccess) {
final String outputFile = response.destinationPath;
// use the file
} else if (response is OnFailure) {
// failure message
print(response.message);
} else if (response is OnCancelled) {
print(response.isCancelled);
}
In order to get the progress of compression while the video is being compressed the following to receive a stream;
_lightCompressor.onProgressUpdated
You can use a stream builder for example as follows;
StreamBuilder<double>(
stream: _lightCompressor.onProgressUpdated,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.data != null && snapshot.data > 0) {
// --> use snapshot.data
}
},
),
Options
| Name | Android | iOS | OpenHarmony | Default |
|---|---|---|---|---|
| startCompression | ✅ | ✅ | ✅ | OnFailure |
| cancelCompression | ✅ | ✅ | ❌ |
For more information on how to use the plugin, refer to the sample app
Reporting issues
To report an issue, please specify the following:
- Device name
- Android version
- If the bug/issue exists on the sample app of the library that could be downloaded at this link.
Compatibility
Minimum Android SDK: the plugin requires a minimum API level of 24.
The minimum iOS version supported is 11.
The minimum macOS version supported is 10.15.
Dart Versions
- Dart 3: >= 3.0.0