我有一个应用程序,允许用户录制视频,UIImagePickerController然后将其上传到YouTube.问题是UIImagePickerController创建的视频文件是巨大的,即使视频只有5秒长.例如,5秒长的视频是16-20兆字节.我想保持540或720质量的视频,但我想减少文件大小.
我一直在尝试AVFoundation并AVAssetExportSession试图获得更小的文件大小.我试过以下代码:
AVAsset *video = [AVAsset assetWithURL:videoURL];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:@"vid1.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"done processing video!");
}];
Run Code Online (Sandbox Code Playgroud)
但这并没有减少文件大小.我知道我正在做的事情是可能的,因为在Apple的照片应用程序中,当您选择"在YouTube上分享"时,会自动处理视频文件,因此它的小到可以上传.我想在我的应用程序中做同样的事情.
我怎么能做到这一点?
我尝试过以下方法:
- (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]);
}];
}
Run Code Online (Sandbox Code Playgroud)
我知道self.movieUrl不是 …