1

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

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

0 回答 0