I'm working on the video uploading module in my app. Before uploading to the server, I'm compressing the video which is working fine, and video size are reducing after compression for mp4 & .mov type file but when I upload .3gp format video, it is not compressing. Suppose I upload 20 Mb video after compression it is compressed in 20 Mb or sometime 27Mb. Here what I tried:-
import UIKit
import AVFoundation
class ViewController: UIViewController {
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
@IBAction func selectVideo(_ sender: UIButton) {
picker.sourceType = .photoLibrary
picker.delegate = self
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum)!
picker.mediaTypes = ["public.movie"]
present(picker, animated: true, completion: nil)
}
func compressVideo(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
let urlAsset = AVURLAsset(url: inputURL, options: nil)
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) else {
handler(nil)
return
}
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileType.mov
exportSession.shouldOptimizeForNetworkUse = true
exportSession.exportAsynchronously { () -> Void in
handler(exportSession)
}
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if info[UIImagePickerController.InfoKey.mediaType] as? String == "public.movie" {
// here your video capture code
let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL!
let data = NSData(contentsOf: videoURL! as URL)!
print("File size before compression: \(Double(data.length / 1048576)) mb")
let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v")
compressVideo(inputURL: videoURL! as URL, outputURL: compressedURL) { (exportSession) in
guard let session = exportSession else {
return
}
switch session.status {
case .unknown:
break
case .waiting:
break
case .exporting:
break
case .completed:
guard let compressedData = NSData(contentsOf: compressedURL) else {
return
}
let b = compressedData.length;
let k = compressedData.length / 1024;
let m = ((compressedData.length / 1024) / 1024);
let g = (((compressedData.length / 1024) / 1024) / 1024);
let t = ((((compressedData.length / 1024) / 1024) / 1024) / 1024);
if(t>1) {
let fileSizeStr = String("\(t) TB");
print(fileSizeStr)
} else if(g>1){
let fileSizeStr = String("\(g) GB");
print(fileSizeStr)
} else if(m>1){
let fileSizeStr = String(" \(m) MB");
print(fileSizeStr)
} else if(k>1){
let fileSizeStr = String(" \(k) KB");
print(fileSizeStr)
} else {
let fileSizeStr = String(" \(b) bytes");
print(fileSizeStr)
}
print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
case .failed:
break
case .cancelled:
break
}
}
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}