如何在iOS上传之前压缩视频数据

时间:2014-03-03 04:08:22

标签: ios objective-c cocoa-touch video alassetslibrary

我在压缩视频数据方面遇到问题(视频数据来自ALASSET)。我想在上传到服务器之前压缩视频数据。我发现以下功能转换为低质量,但输出是NSURL而不是NSDATA。如何在上传之前压缩视频的NSData。

这是我的上传功能:

ALAsset *alasset = [allVideos objectAtIndex:i];
ALAssetRepresentation *rep = [alasset defaultRepresentation];
NSString * videoName = [rep filename];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

这是转换功能:

    - (void)convertVideoToLowQuailtyWithInputURL:(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:^(void)
     {
         handler(exportSession);
         [exportSession release];
     }];
}

1 个答案:

答案 0 :(得分:-3)

下面的代码非常适合我:

asset = [[AssetItem alloc] initWithURL:movieURL];
            NSLog(@"Start");
            asset.exportSession.shouldOptimizeForNetworkUse = TRUE;
            asset.preset = AVAssetExportPresetMediumQuality;
            asset.exportSession.outputFileType = @"public.mpeg-4";
            [asset exportAssetWithCompletionHandler:^(NSError *error){
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (error != nil)
                    {
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
                                                                            message:[error localizedFailureReason] delegate:self
                                                                  cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                        [alertView show];
                    }
                });
            }];