Coder Social home page Coder Social logo

masudshuvo / light_compressor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from abedelazizshe/light_compressor

0.0 1.0 0.0 1.41 MB

A powerful and easy-to-use video compression plugin for Flutter.

License: MIT License

Kotlin 20.39% Ruby 5.39% Swift 42.60% Objective-C 1.78% Dart 29.84%

light_compressor's Introduction

Pub

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. 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.

Demo

Android-demo

Installation

First, add light_compressor as a dependency in your pubspec.yaml file.

iOS

Add the following to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>${PRODUCT_NAME} library Usage</string>

Android

Add the following permissions in AndroidManifest.xml:

API < 29

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>

API >= 29

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Include this in your Project-level build.gradle file:

allprojects {
    repositories {
        .
        .
        .
        maven { url 'https://jitpack.io' }
    }
}

Include this in your Module-level build.gradle file:

implementation 'com.github.AbedElazizShe:LightCompressor:0.9.4'

And since the library depends on Kotlin version 1.5.21, please ensure that 1.5.21 is the minimum kotlin version in your project by changing ext.kotlin_version in your Project-level build.gradle file.

Usage

In order to start compression, just call [LightCompressor().compressVideo()] and pass the following parameters;

  1. path: the path of the provided video file to be compressed - required.
  2. destinationPath: the path where the output compressed video file should be saved - required.
  3. videoQuality: to allow choosing a video quality that can be VideoQuality.very_low, VideoQuality.low, VideoQuality.medium, VideoQuality.high, or VideoQuality.very_high - required.
  4. isMinBitRateEnabled: to determine if the checking for a minimum bitrate threshold before compression is enabled or not. The default value is true - optional.
  5. keepOriginalResolution: to keep the original video height and width when compressing. This default value is false - optional.
  6. iosSaveInGallery: to avoid saving the output file in gallery.
import 'package:light_compressor/compressor.dart';


final LightCompressor _lightCompressor = LightCompressor();
final dynamic response = await _lightCompressor.compressVideo(
  path: _sourcePath,
  destinationPath: _destinationPath,
  videoQuality: VideoQuality.medium,
  isMinBitRateEnabled: false,
  keepOriginalResolution: false);

The plugin allows cancelling the compression by calling;

_lightCompressor.cancelCompression();

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.
  • onCancelled: if cancelCompression() was called.
   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
       }
    },
),

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 21.

The minimum iOS version supported is 11.

What's next?

  • Allow passing a custom video width, height, and bitrate to avoid allowing the library to auto-generate the values.
  • Improve the speed of compression.
  • Simplify the plugin's APIs to make it easier to implement.

Dart Versions

  • Dart 2: >= 2.12.0
  • Flutter: >=2.0.0

Maintainers

light_compressor's People

Contributors

abedelazizshe avatar abed-binary avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.