iOS应用层的API无法对视频源数据进行直接操作,所以想要对视频进行修改,需要进行一次导出操作。可以使用AVAssetExportSession来实现。
#mark 这里可以设置质量,主要针对需要对视频进行压缩。这个方法只能粗略设置,想要更精细设置可以使用AVAssetReader和AVAssetWriterInput的API。
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetMediumQuality];
NSInteger degress = 0;
switch (orientation) {
case UIImageOrientationUp:
degress = 0;
break;
case UIImageOrientationRight:
degress = 90;
break;
case UIImageOrientationDown:
degress = 180;
break;
case UIImageOrientationLeft:
degress = 270;
break;
}
AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
//计算好角度和renderSize
if (videoAssetTrack && degress != 0 ) {
CGAffineTransform translateToCenter;
CGAffineTransform mixedTransform;
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = videoAssetTrack.minFrameDuration;
if(degress == 90){
translateToCenter = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.height,0.0);
mixedTransform = CGAffineTransformRotate(translateToCenter,M_PI_2);
videoComposition.renderSize = CGSizeMake(videoAssetTrack.naturalSize.height,videoAssetTrack.naturalSize.width);
}else if(degress == 180){
translateToCenter = CGAffineTransformMakeTranslation(videoAssetTrack.naturalSize.width, videoAssetTrack.naturalSize.height);
mixedTransform = CGAffineTransformRotate(translateToCenter,M_PI);
videoComposition.renderSize = CGSizeMake(videoAssetTrack.naturalSize.width,videoAssetTrack.naturalSize.height);
}else if(degress == 270){
translateToCenter = CGAffineTransformMakeTranslation(0.0, videoAssetTrack.naturalSize.width);
mixedTransform = CGAffineTransformRotate(translateToCenter,M_PI_2*3.0);
videoComposition.renderSize = CGSizeMake(videoAssetTrack.naturalSize.height,videoAssetTrack.naturalSize.width);
} else {
mixedTransform = videoAssetTrack.preferredTransform;
CGSize renderSize = CGSizeMake(videoAssetTrack.naturalSize.width, videoAssetTrack.naturalSize.height);
videoComposition.renderSize = renderSize;
}
AVMutableVideoCompositionInstruction *videoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
#mark设置需要的裁剪时间
videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
AVMutableVideoCompositionLayerInstruction *videoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoAssetTrack];
#mark设置旋转角度
[videoCompositionLayerInstruction setTransform:mixedTransform atTime:kCMTimeZero];
videoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:videoCompositionLayerInstruction];
videoComposition.instructions = [NSArray arrayWithObject: videoCompositionInstruction];
exportSession.videoComposition = videoComposition;
}
执行导出:
[exportSession exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus status = [exportSession status];
switch (status)
{
case AVAssetExportSessionStatusCompleted:
{
break;
}
case AVAssetExportSessionStatusFailed:
{
break;
}
default:
{
NSLog(@"Exporter status not fialed or complete: %ld", status);
break;
}
}
}];
网友评论