NRVideoCompression: This class is used for compressing the video taken from iPhone camera or from camera roll. There is various circumstances where you want to send videos to the network but because of the high resolution of iPhone camera videos are of very large size which needs to be compressed before we are sending them to network to save the memory. This library helps you to the compress the size of videos according to your quality choices which have options AVAssetExportPresetLowQuality, AVAssetExportPresetMediumQuality, AVAssetExportPresetHighQuality.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Xcode 7+ , Swift 2 , iOS 9 and above
NRVideoCompression is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "NRVideoCompression"
func openImagePicker() {
let controller = UIImagePickerController()
//controller.allowsEditing = false
controller.sourceType = .Camera
controller.mediaTypes = [kUTTypeMovie as String]
controller.delegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
//MARK: UIImagePickerController Delegates
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)
self.originalVideoUrl = info[UIImagePickerControllerMediaURL] as? NSURL
if let _ = originalVideoUrl {
if NSFileManager.defaultManager().fileExistsAtPath(self.originalVideoUrl!.absoluteString) {
try! NSFileManager.defaultManager().removeItemAtURL(self.originalVideoUrl!)
}
let originalSize = NSData(contentsOfURL: originalVideoUrl!)
let sizeinMB = NSByteCountFormatter.stringFromByteCount(Int64((originalSize?.length)!), countStyle: .File)
self.originalVideoSize.text = "original video size\(sizeinMB)"
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func compressButtonClicked(sender: UIButton) {
if let _ = self.originalVideoUrl {
NRVideoCompressor.compressVideoWithQuality(AVAssetExportPresetLowQuality, inputURL: originalVideoUrl!, completionHandler: { (outputUrl) in
let compressSize = NSData(contentsOfURL: outputUrl)
let sizeinMB = NSByteCountFormatter.stringFromByteCount(Int64((compressSize?.length)!), countStyle: .File)
self.newVideoSize.text = sizeinMB
})
}
}
}
Contributions are always welcome! (:
- Fork it ( http://github.com/naveenrana1309/NRVideoCompression/fork )
- Create your feature branch ('git checkout -b my-new-feature')
- Commit your changes ('git commit -am 'Add some feature')
- Push to the branch ('git push origin my-new-feature')
- Create new Pull Request
Xcode 8+ , Swift 3 , iOS 9 and above
Naveen Rana. See Profile
Email: naveenrana1309@gmail.com.
Check out Facebook Page for detail.
NRVideoCompression is available under the MIT license. See the LICENSE file for more info.