Swift 3gp视频压缩问题

时间:2019-05-08 04:22:35

标签: ios swift video-compression

我正在使用我的应用程序中的视频上传模块。在上载到服务器之前,我正在压缩视频,这可以正常工作,并且在压缩mp4和.mov类型的文件后,视频大小会减小,但是当我上载.3gp格式的视频时,它没有压缩。假设我在压缩后上传了20 Mb的视频,它被压缩为20 Mb或有时为27Mb。这是我尝试的:-

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)
}
}

0 个答案:

没有答案