2016-11-07 82 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時。 –