压缩视频,像苹果一样分享



在默认的照片应用程序中,Apple允许您将视频分享到youtube, facebook,vimeo等。我想复制这个功能,但是我用1080p录制视频,所以它们是非常大的文件。苹果通过在上传之前压缩视频来解决这个问题。我试着做同样的事,但失败得很惨。有人能给我指个方向吗?我发现这个问题很有用,但我不明白为什么它对我不起作用:iPhone:通过编程压缩录制的视频来共享?

这是我正在尝试的:

- (void)convertVideoToLowQualityWithInputURL:(NSURL *)inputURL
                                   outputURL:(NSURL *)outputURL
                                     handler:(void (^)(AVAssetExportSession *))handler {
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch ([exportSession status]) {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");
                break;
            default:
                break;
        }
    }];
}
-someMethod{
    NSURL *videoURL = [NSURL fileURLWithPath:self.currentVideoURL];
    NSArray *videoSplit = [[NSString stringWithFormat:@"%@",videoURL] componentsSeparatedByString: @"."];
    NSString *first = [videoSplit firstObject];
    NSString *output = [NSString stringWithFormat:@"%@_Low_Qual.%@",first,[videoSplit objectAtIndex:1]];
    NSLog(@"VIDEO URL IS: %@",videoURL);
    NSLog(@"OUTPUT URL IS: %@",output);
    NSURL *outputURL = [NSURL fileURLWithPath:output];
    [self convertVideoToLowQualityWithInputURL:videoURL outputURL:outputURL handler: ^(AVAssetExportSession *exportSession)
     {
         if (exportSession.status == AVAssetExportSessionStatusCompleted) {
             printf("completedn");
         }
         else {
             printf("errorn");
         }
     }];
}

但是,它每次都给我'Export failed: The operation could not be complete '。视频的URL是有效的,所以我不知道为什么它不会工作。什么好主意吗?

我不知道这是否是正确的答案,但当我把我的presetName改为AVAssetExportPreset640X480时,每次我运行代码时压缩都在工作

最新更新