【问题标题】:How to compress video data before uploading in iOS如何在 iOS 上传前压缩视频数据
【发布时间】:2014-04-04 00:20:45
【问题描述】:

我在压缩视频数据中遇到问题(视频数据来自 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];
     }];
}

【问题讨论】:

  • 为什么不直接将file-url 作为outputURL: 值传递,然后将文件加载到您的NSData 实例中?
  • @aroth:你能举例代码吗?谢谢
  • @user3214941 你找到一个好的解决方案了吗?我也面临同样的问题

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


【解决方案1】:

下面的代码非常适合我:

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];
                    }
                });
            }];

【讨论】:

  • AssetItem是什么类?我只知道AVAsset。我尝试了与线程启动器完全相同的代码,但总是出现“无法完成”错误。不幸的是,这个错误非常不具体。
  • -1 -- 这是不完整的信息,如果从评论中提供的链接下载Apple示例代码,会注意到 -- “AssetItem”实际上是“NSObject”的子类。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 2021-05-30
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多