1

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)
}
}
Chetan Lodhi
  • 353
  • 1
  • 3
  • 21
  • exporting 3GP video to MOV won't reduce the size, as 3GP video is already compressed well. MOV is usually larger in file size. – Raptor May 08 '19 at 04:29
  • Okay, but I also tried with .mp4 export. I got the same result. What should I do in this scenario with the 3gp video? – Chetan Lodhi May 08 '19 at 04:32
  • As mentioned 3GP is already a compressed format. What are you trying to achieve? Chances of compressing to smaller file size are low. – Raptor May 08 '19 at 04:38
  • Actually, I'm restricting the user to upload only 16mb video after compression. So when the user uploads the video, After compression than either upload it or giving exceed video size message. – Chetan Lodhi May 08 '19 at 05:02
  • Then you should limit the duration of the video as well (i.e. trim the video length before upload). – Raptor May 08 '19 at 05:54
  • Okay. Any other approach if we don't trim video and directly upload it? – Chetan Lodhi May 08 '19 at 06:38

0 Answers0