在iOS中压缩video

我看过这个问题和这个问题 ,都没能帮上忙。

我已经尝试了以下内容:

- (void)compress:(NSURL *)videoPath completionBlock:(void(^)(id data, BOOL result))block{ self.outputFilePath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"]; NSURL *outputURL = [NSURL fileURLWithPath:self.outputFilePath]; [self compressVideoWithURL:self.movieURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) { }]; } - (void)compressVideoWithURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler { AVURLAsset *asset = [AVURLAsset assetWithURL:self.movieURL]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; exportSession.fileLengthLimit = 3000000; exportSession.outputURL = outputURL; exportSession.outputFileType = AVFileTypeQuickTimeMovie; exportSession.shouldOptimizeForNetworkUse = YES; [exportSession exportAsynchronouslyWithCompletionHandler:^{ NSData *newOutputData = [NSData dataWithContentsOfURL:outputURL]; NSLog(@"Size of New Video(bytes):%d",[newOutputData length]); }]; } 

我知道self.movieUrl不是nil 。 但是当我打印与video关联的NSData的大小(以字节为单位)时,前后都是相同的,都是30,000,000字节。

但根据这个问题 ,上面的代码应该工作。

我究竟做错了什么?

在Swift 3.0中

从相机胶卷中selectvideo

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if info[UIImagePickerControllerMediaType] as? String == (kUTTypeMovie as? String) { // here your video capture code let videoURL = info[UIImagePickerControllerMediaURL] 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 } print("File size after compression: \(Double(compressedData.length / 1048576)) mb") case .failed: break case .cancelled: break } } } self.dismiss(animated: true, completion: nil) } 

压缩types:

 AVAssetExportPresetLowQuality AVAssetExportPresetMediumQuality AVAssetExportPresetHighestQuality AVAssetExportPreset640x480 AVAssetExportPreset960x540 

压缩video方法

  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: AVAssetExportPresetLowQuality) else { handler(nil) return } exportSession.outputURL = outputURL exportSession.outputFileType = AVFileTypeQuickTimeMovie exportSession.shouldOptimizeForNetworkUse = true exportSession.exportAsynchronously { () -> Void in handler(exportSession) } } 

输出:

 File size before compression: 25.0 mb File size after compression: 7.0 mb 

我已经想通了,感谢这个问题(迅速版): IOSvideo压缩Swift iOS 8损坏的video文件

我有客观的C版本。 这里是方法:

  - (void)compressVideo:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))completion { AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:urlAsset presetName:AVAssetExportPresetMediumQuality]; exportSession.outputURL = outputURL; exportSession.outputFileType = AVFileTypeQuickTimeMovie; exportSession.shouldOptimizeForNetworkUse = YES; [exportSession exportAsynchronouslyWithCompletionHandler:^{ completion(exportSession); }]; } 

并调用该方法:

 NSURL* uploadURL = [NSURL fileURLWithPath: [NSTemporaryDirectory() stringByAppendingPathComponent:@"temporaryPreview.mov"]]; [self compressVideo:self.movieURL outputURL:uploadURL handler:^(AVAssetExportSession *completion) { if (completion.status == AVAssetExportSessionStatusCompleted) { NSData *newDataForUpload = [NSData dataWithContentsOfURL:uploadURL]; NSLog(@"Size of new Video after compression is (bytes):%d",[newDataForUpload length]); } }]; 

这将我的video的文件大小从32 MB减less到1.5 MB。