ios - 如何在 Objective C 中将视频压缩到精确的大小?

标签 ios objective-c video compression size

可能大多数人都使用 WhatsApp,并且您可能知道,当您想要向您的联系人之一发送视频时,WhatsApp 首先将视频压缩到最大大小 16mb,然后才将所选视频上传到您的联系人。

我想做的只是使用 AV Foundation 做的事情,或者更具体地说是 AVAssetExportSession。

这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {



        NSURL *videoURL = info[UIImagePickerControllerMediaURL];
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *sourcePath =[documentsDirectory stringByAppendingString:@"/output.mov"];
        NSURL *outputURL = [NSURL fileURLWithPath:sourcePath];

        [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
         {
             if (exportSession.status == AVAssetExportSessionStatusCompleted)
             {
                 NSLog(@"completed");
             }
             else
             {
                 NSLog(@"error: %@",exportSession.error);
             }
         }];

         [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (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:AVAssetExportPresetMediumQuality];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
         handler(exportSession);
     }];

}

这段代码效果很好,它获取视频并将其压缩得非常小。

问题是当用户尝试使用强大的相机上传相当长的视频时,尺寸对我来说不够小。

我想做的实际上是将任何视频压缩到有限的大小 比如说 16Mb,就像 WhatsApp 那样。 我怎样才能做到这一点?

最佳答案

这似乎不存在一种简单的方法,但 AVAssetExportSession 有一个预计的OutputFileLenght 可以提供帮助。
在我的代码中,我迭代不同的质量并检查文件大小是否符合我想要的大小:

NSURL * inputURL = [NSURL fileURLWithPath:path];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = nil;
    for (NSString * string in @[AVAssetExportPresetHighestQuality,AVAssetExportPresetMediumQuality,AVAssetExportPresetLowQuality]) {
        exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:string];
        exportSession.shouldOptimizeForNetworkUse = YES;
        exportSession.outputFileType = AVFileTypeMPEG4;
        exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
        unsigned long long espectedFileSize = exportSession.estimatedOutputFileLength;
        if (espectedFileSize < VIDE_LIMIT) {
            break;
        }

    }
    //Temp file

    NSString *fileName = [NSString stringWithFormat:@"%@_%@", [[NSProcessInfo processInfo] globallyUniqueString], @"video.mov"];
    NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    exportSession.outputURL = fileURL;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)

关于ios - 如何在 Objective C 中将视频压缩到精确的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29607943/

相关文章:

winapi - 如何在win32 C程序中读取摄像机

iphone - UITableView 上的核心数据关系和/或 NSPredicate 排序

ios - *** 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,

ios - 在 ChildViewController 中关闭 ChildViewController 并转到上一个 ViewController

iphone - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

android - 安卓播放时视频旋转90度怎么解决?

video - 重新创建/匹配 ffmpeg 设置

ios - 显示 UINavigationBar

objective-c - 在 Xcode 中启动动画

objective-c - C/Objective-C 读取并获取整数的最后一位?