iOS - Fine tuned video compression in Swift 4
Clone or download
Latest commit d51a693 Aug 28, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
VideoCompressionTutorial.xcodeproj Added tutorial files Aug 27, 2018
VideoCompressionTutorial Added tutorial files Aug 27, 2018
.gitignore Initial commit Aug 27, 2018
LICENSE Initial commit Aug 27, 2018
README.md Improved README Aug 28, 2018

README.md

Video Compression Tutorial

iOS - Fine tuned video compression in Swift 4

Features

  • iOS 9+
  • No dependencies
  • Single file, single function
  • Compression in background thread
  • Cancelable
  • Configurable a/v bitrate, video resolution, audio sample rate and many other fine tuning operations
  • Proper orientation correction for back/front camera
  • Low performance compression during Background Execution, even when device is locked. (Application.beginBackgroundTask must be called explicitly)

Install

Copy this file to your project.

Usage

// Get source video
let videoToCompress = //any valid URL pointing device storage

// Declare destination path and remove anything exists in it
let destinationPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("compressed.mp4")
try? FileManager.default.removeItem(at: destinationPath)

// Compress
let cancelable = compressh264VideoInBackground(
    videoToCompress: videoToCompress,
    destinationPath: destinationPath,
    size: nil, // nil preserves original,
    //size: (width: 1280, height: 720) 
    compressionTransform: .keepSame,
    compressionConfig: .defaultConfig,
    completionHandler: { [weak self] path in
        // use path
    },
    errorHandler: { e in
        print("Error: ", e)
    },
    cancelHandler: {
        print("Canceled.")
    }
)

// To cancel compression, set cancel flag to true and wait for handler invoke
cancelable.cancel = true