Code Monkey home page Code Monkey logo

lightcompressor_ios's Introduction

LightCompressor

A powerful and easy-to-use video compression swift package for iOS. It generates a compressed MP4 video with a modified width, height, and bitrate (the number of bits per seconds that determines the video and audio files’ size and quality). It is based on LightCompressor for Android.

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.

I would like to mention that the set attributes for size and quality worked just great in my projects and met the expectations. It may or may not meet yours. I’d appreciate your feedback so I can enhance the compression process.

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 5 video qualities; .very_high, .high, .medium, .low or .very_low and the package will handle generating the right bitrate and size values for the output video.

For a sample app, you can have a look at LightCompressor_iOS_Sample.

Usage

To import this swift package to your XCode project follow the following;

  • Go to File -> Swift Packages then choose to Add package dependency.
  • Add https://github.com/AbedElazizShe/LightCompressor_iOS.git, to the text field shown on the popup window and click next.
  • Specifiy the minimum release version and confirm. The project will be imported and you can start using it.
  • In case on a new release update, you can choose File -> Swift packages and then click on Update to latest package version.

In order to use the compressor, just call [compressVideo()] and pass both source and destination file paths. The method has a callback for 5 functions;

  1. onStart - called when compression started.
  2. onSuccess - called when compression completed with no errors/exceptions.
  3. onFailure - called when an exception occurred or video bitrate and size are below the minimum required for compression.
  4. onProgress - called with progress new value.
  5. onCancelled - called when the job is cancelled.

In addition, you can pass the video quality (default is medium) to enable checking for min bitrate (default is true), and if you wish to keep the original video width and height from being changed during compression, you can pass true or false for keepOriginalResolution where default is false.

Example

import LightCompressor
...

let videoCompressor = LightCompressor()
 
let compression: Compression = videoCompressor.compressVideo(
                                source: videoToCompress,
                                destination: destinationPath as URL,
                                quality: .medium,
                                isMinBitRateEnabled: true,
                                keepOriginalResolution: false,
                                progressQueue: .main,
                                progressHandler: { progress in
                                    // progress
                                },                                            
                                completion: {[weak self] result in
                                    guard let `self` = self else { return }
                                             
                                    switch result {                                                 
                                    case .onSuccess(let path):
                                        // success 
                                                 
                                    case .onStart:
                                        // when compression starts
                                                 
                                    case .onFailure(let error):
                                        // failure error 
                                                 
                                    case .onCancelled:
                                        // if cancelled
                                    }
                                }
 )

// to cancel call
compression.cancel = true

Compatibility

The minimum iOS version supported is 11.

Getting help

For questions, suggestions, or anything else, email elaziz.shehadeh(at)gmail.com.

lightcompressor_ios's People

Contributors

abed-binary avatar abedelazizshe 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.