【发布时间】: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