【问题标题】:How To compress a video to accurate size in objective c?如何在目标 c 中将视频压缩到准确的大小?
【发布时间】:2015-06-18 22:01:57
【问题描述】:

可能你们中的大多数人都使用 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);
     }];

}

这段代码效果很好,它把视频压缩成非常小的尺寸。

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

我想要做的实际上是将任何视频压缩到有限的大小 比如说像 WhatsApp 那样的 16Mb。 我该怎么做?

【问题讨论】:

  • @Ravi 在您添加的链接中只有一个设置压缩后视频的外观,这实际上是解决方案,但缺少很多代码,我仍然不知道如何使用这段小代码。你能帮忙吗?
  • 意思是 4k 和 1080p 视频最终会有相同的质量吗? whatsapp 上的适应性压缩这么好?

标签: ios objective-c video compression size


【解决方案1】:

这似乎不是一个简单的方法,但 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)

【讨论】:

  • 你的解决方案真的很好,但实际上它和我的完全一样。最高质量通常大于 16mb,而低质量对我来说太低了。如果您了解如何使用它,请参阅此link,因为我在里面有点困惑。
  • AVAssetExportSession 不是实现这一目标的唯一方法,如果您需要更多控制,可以使用 AVAssetReader/AVAssetWriter 的组合,但我从未尝试过。我发现这篇文章我认为它可能对你有帮助finalize.com/2014/10/21/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多