【问题标题】:Compress video for sharing like Apple?压缩视频以像 Apple 一样共享?
【发布时间】:2014-03-27 19:22:22
【问题描述】:

在默认的照片应用程序中,Apple 允许您将视频分享到 youtube、facebook、vimeo 等。我想重现此功能,但我以 1080p 录制我的视频,所以它们是非常大的文件。苹果通过在上传前压缩视频来解决这个问题。我尝试做同样的事情,但惨遭失败。谁能指出我正确的方向?我发现这个问题很有用,但我不明白为什么它对我不起作用:iPhone:Programmatically compressing recorded video to share?

这是我正在尝试的:

- (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("completed\n");
         }
         else {
             printf("error\n");
         }
     }];

}

但是,它每次都给我“导出失败:操作无法完成”。视频网址是有效的,所以我不知道为什么它不起作用。有什么想法吗?

【问题讨论】:

  • 使用 AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:theAsset presetName:AVAssetExportPresetLowQuality];
  • 刚试了没用:(
  • 你还在导出失败吗?
  • 打印 NSError。使用 AVAssetExportSession 属性状态和错误。

标签: ios objective-c cocoa-touch cocoa avfoundation


【解决方案1】:

我不知道这是否是正确的答案,但是当我将我的 presetName 更改为 AVAssetExportPreset640X480 时,每次运行代码时压缩都会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多