iOS上传相簿视频模糊
在上传相簿视频文件时,选择系统相簿里面的视频,系统会自动跳入下一页进行视频“压缩”。
实际上这个过程是将其他类型的视频,比如.MP4转码成了.MOV视频文件,顺便压缩了文件。
这时候会发现转后的视频没原视频清晰呢?如果没设置videoQuality和videoExportPreset属性的话。
- videoQuality
@property(nonatomic) UIImagePickerControllerQualityType videoQuality NS_AVAILABLE_IOS(3_1); // default value is UIImagePickerControllerQualityTypeMedium. If the cameraDevice does not support the videoQuality, it will use the default value.
- videoExportPreset
@property(nonatomic, copy) NSString *videoExportPreset;
Discussion The value of this key is one of the export presets supported by the AVAssetExportSession class. For a list of possible values, see the export preset constants in AVAssetExportSession.
AVAssetExportSession
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset960x540];
在这个类里搜索ExportPreset,可通过这些设置videoExportPreset属性。也可直接设置videoQuality来达到压缩时高分辨率的效果,使得上传的视频不模糊。
关于视频转码
下面试摘抄代码,出处见AVAssetExportSession 视频转码
// 视频转码
- (void) convertVideoWithModel:(NSString *) localVideoPath {
TYWeakify(self, weakSelf);
//转码配置
NSURL *inputURL = [NSURL fileURLWithPath:localVideoPath];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset960x540];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@.mp4",localVideoPath]]; //转码后的视频保存目录
self.liveShareView.localVideoURL = [NSString stringWithFormat:@"%@.mp4",localVideoPath];
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exportSession.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
NSError *exportError = exportSession.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog(@"视频转码成功");
[weakSelf uploadVideo];
}
}
}];
}
如果要研究视频转码,需要研究研究ffmpeg。