2016-11-07 58 views
3

所以,此刻我使用这个压缩视频:斯威夫特 - 压缩视频文件

func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void) 
    { 
     let urlAsset = AVURLAsset(URL: inputURL, options: nil) 

     let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) 

     exportSession!.outputURL = outputURL 

     exportSession!.outputFileType = AVFileTypeQuickTimeMovie 

     exportSession!.shouldOptimizeForNetworkUse = true 

     exportSession!.exportAsynchronouslyWithCompletionHandler {() -> Void in 

      handler(session: exportSession!) 
     } 

    } 

当我记录在2秒的视频,大小为4,3 MB,当我记录6秒钟内的视频文件大小为9,3 MB

任何提示以减小尺寸?

回答

7

虽然这些扩展都使用中等设置进行压缩,但如果要关注质量或大小,可以将其更改为低或高。

我使用基于雨燕版这些扩展:

对于OP(雨燕2.2):

extension PreviewVideoViewController: AVCaptureFileOutputRecordingDelegate { 
    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { 
     let data = NSData(contentsOfURL: outputFileURL) 
     print("File size before compression: \(Double(data!.length/1048576)) mb") 
     let compressedURL = NSURL.fileURLWithPath(NSTemporaryDirectory() + NSUUID().UUIDString + ".m4v") 
     compressVideo(outputFileURL, outputURL: compressedURL) { (session) in 
      switch session.status { 
      case .Unknown: 
       break 
      case .Waiting: 
       break 
      case .Exporting: 
       break 
      case .Completed: 
       let data = NSData(contentsOfURL: compressedURL) 
       print("File size after compression: \(Double(data!.length/1048576)) mb") 
      case .Failed: 
       break 
      case .Cancelled: 
       break 
      } 
     } 
    } 

    private func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void) { 
     let urlAsset = AVURLAsset(URL: inputURL, options: nil) 
     if let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) { 
      exportSession.outputURL = outputURL 
      exportSession.outputFileType = AVFileTypeQuickTimeMovie 
      exportSession.shouldOptimizeForNetworkUse = true 
      exportSession.exportAsynchronouslyWithCompletionHandler {() -> Void in 
       handler(session: exportSession) 
      } 
     } 
    } 
} 

有人谁需要它雨燕3.0

extension PreviewVideoViewController: AVCaptureFileOutputRecordingDelegate { 
    func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) { 
     guard let data = NSData(contentsOf: outputFileURL as URL) else { 
      return 
     } 

     print("File size before compression: \(Double(data.length/1048576)) mb") 
     let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".m4v") 
     compressVideo(inputURL: outputFileURL 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 
       } 

       print("File size after compression: \(Double(compressedData.length/1048576)) mb") 
      case .failed: 
       break 
      case .cancelled: 
       break 
      } 
     } 
    } 

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

我猜是因为我的类名是'class PreviewVideoViewController:UIViewController {'我应该替换你的'MyViewContr oliver'与'PreviewVideoViewController'?但我确实收到3条错误消息:https://s22.postimg.org/p8ps48lm9/Screen_Shot_2016_11_07_at_18_24_39.png –

+0

@ RoduckNickes Swift 2.2是你的应用程序吗? – CodeBender

+0

它说'Apple Swift 3.0.1版本(swiftlang-800.0.58.6 clang-800.0.42.1) 目标:x86_64-apple-macosx10.9'当我在终端中运行xcrun swift -version时。 –