AbedElazizShe / LightCompressor_iOS

A powerful and easy-to-use video compression swift package for iOS.
MIT License
97 stars 18 forks source link

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.

Usage

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

In order to use the compressor, just call [compressVideo()] and pass the following:

Configuration

Example

import LightCompressor
...

let videoCompressor = LightCompressor()

compression = videoCompressor.compressVideo(videos: [
    .init(
        source: videoToCompress, 
        destination: destinationPath, 
        configuration: .init(
            quality: VideoQuality.very_high, 
            videoBitrateInMbps: 5, 
            disableAudio: false, 
            keepOriginalResolution: 
            false, videoSize: 
            CGSize(width: 360, height: 480) 
            )
        )
    ],
    progressQueue: .main,
    progressHandler: { progress in
                    DispatchQueue.main.async { [unowned self] in
                       // Handle progress- "\(String(format: "%.0f", progress.fractionCompleted * 100))%"                            
                    }},

    completion: {[weak self] result in
                    guard let `self` = self else { return }

                    switch result {

                    case .onSuccess(let index, let path):
                        // Handle onSuccess

                    case .onStart:
                        // Handle onStart                              

                    case .onFailure(let index, let error):
                        // Handle onFailure                

                    case .onCancelled:
                        // Handle onCancelled                          
                    }
})

// to cancel call
compression.cancel = true

Compatibility

The minimum iOS version supported is 14.

Getting help

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