在iOS中压缩视频 [英] compressing a video in iOS

查看:581
本文介绍了在iOS中压缩视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个问题这个问题,两者都没能帮助。

I've looked at this question and this question, neither have been able to help.

我尝试过以下方法:

- (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.movi​​eUrl 不是 nil 。但是当我打印与视频相关联的 NSData 的大小(以字节为单位)时,它们之前和之后都是相同的,都是30,000,000字节。

I know that self.movieUrl is not nil. But when I printed the size (in bytes) of the NSData associated with the video, they were the same before and after, both 30,000,000 bytes.

但根据这个问题,上述代码应该有效。

But according to this question, the above code should work.

我到底做错了什么?

推荐答案

我想过它出来了,感谢这个问题(Swift版本): IOS Video压缩Swift iOS 8损坏的视频文件

I have figured it out, thanks to this question (Swift version): IOS Video Compression Swift iOS 8 corrupt video file

我有目标C版本。以下是方法:

I have the objective C version. Here is the method:

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

这会将我的视频文件大小从32 MB减少到1.5 MB。

This reduced the file size of my videos from 32 MB to 1.5 MB.

这篇关于在iOS中压缩视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆