简体   繁体   中英

PHAsset video compression iOS swift

I am trying to upload images and videos to server. it working fine. the problem I am facing is the picture captured or video recorded from the iphone having too much size and it takes time to upload to server. how can i reduce the size of video data ?

if(asset.mediaType == .video)
{           
    var dataMOV : Data?

    manager.requestAVAsset(forVideo: asset, options: option2, resultHandler:  {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) in
        let avURLAsset = asset as? AVURLAsset
        do
        {
            let data = try Data(contentsOf: (avURLAsset?.url)!)
            dataMOV = data
            print("asset data :%@ ", data)
        }
        catch
        {

        }
    })
}

From AssetInfo, pull the asset URL and pass it to below method

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 = AVFileTypeQuickTimeMovie
        exportSession.shouldOptimizeForNetworkUse = true
        exportSession.exportAsynchronously { () -> Void in
            handler(exportSession)
        }
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM