0

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
        {

        }
    })
}
sCha
  • 1,291
  • 1
  • 10
  • 22
pankti patel
  • 21
  • 1
  • 7

1 Answers1

1

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)
        }
    } 
King of Masses
  • 16,881
  • 4
  • 57
  • 75
  • is this helped ? – King of Masses Sep 20 '17 at 04:06
  • Can its usage be shown?, I tried it the following way: var outputURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("compressed.mp4") compressVideo(inputURL: vidData, outputURL: outputURL) { (exportSession) in outputURL = exportSession!.outputURL! print(outputURL, "put url and also the old val : ", exportSession!.outputURL!) } –  Jun 07 '19 at 17:40
  • The issue I found is that it produces a video of length 0 –  Jun 07 '19 at 17:40