美文网首页文章集请进
裁剪视频为正方形

裁剪视频为正方形

作者: coming芝 | 来源:发表于2016-09-01 17:52 被阅读0次

#pragma mark 裁剪视频

-(void)caiJianVideo:(NSURL*)videoUrl{

//1 — 采集

AVAsset *videoAsset = [AVAsset assetWithURL:videoUrl];

CMTime assetTime = [videoAsset duration];

Float64 duration = CMTimeGetSeconds(assetTime);

NSDate *d = [NSDate dateWithTimeIntervalSince1970:duration];

NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];

if (duration/3600 >= 1) {

[_dateFormatter setDateFormat:@"HH:mm:ss"];

} else {

[_dateFormatter setDateFormat:@"mm:ss"];

}

NSString *newTime = [_dateFormatter stringFromDate:d];

NSLog(@"视频时长 %@",newTime);

// 2 创建AVMutableComposition实例. apple developer 里边的解释 【AVMutableComposition is a mutable subclass of AVComposition you use when you want to create a new composition from existing assets. You can add and remove tracks, and you can add, remove, and scale time ranges.】

AVMutableComposition *mixComposition = [AVMutableComposition composition];

// 3 - 视频通道  工程文件中的轨道,有音频轨、视频轨等,里面可以插入各种对应的素材

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo

preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

NSError *error = nil;

// 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)

[videoTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(1.0f, 30), CMTimeMakeWithSeconds(CMTimeGetSeconds([videoAsset duration])-1.0f, 30))

ofTrack:videoAssetTrack

atTime:kCMTimeZero

error:&error];

// 3.1 AVMutableVideoCompositionInstruction 视频轨道中的一个视频,可以缩放、旋转等

AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);

// 3.2 AVMutableVideoCompositionLayerInstruction 一个视频轨道,包含了这个轨道上的所有视频素材

AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

UIImageOrientation videoAssetOrientation_  = UIImageOrientationUp;

BOOL isVideoAssetPortrait_  = NO;

CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;

if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) {

videoAssetOrientation_ = UIImageOrientationRight;

isVideoAssetPortrait_ = YES;

}

if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) {

videoAssetOrientation_ =  UIImageOrientationLeft;

isVideoAssetPortrait_ = YES;

}

if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) {

videoAssetOrientation_ =  UIImageOrientationUp;

}

if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {

videoAssetOrientation_ = UIImageOrientationDown;

}

[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];

[videolayerInstruction setOpacity:0.0 atTime:videoAsset.duration];

// 3.3 - Add instructions

mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];

// AVMutableVideoComposition:管理所有视频轨道,可以决定最终视频的尺寸,裁剪需要在这里进行

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];

CGSize naturalSize;

if(isVideoAssetPortrait_){

naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width);

} else {

naturalSize = videoAssetTrack.naturalSize;

}

float renderWidth, renderHeight;

renderWidth = naturalSize.width;

renderHeight = naturalSize.height;

float value = renderWidth>renderHeight?renderHeight:renderWidth;

mainCompositionInst.renderSize = CGSizeMake(value, value);

mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];

mainCompositionInst.frameDuration = CMTimeMake(1, 30);

// 4 - Get path

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:

[NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]];

self.videoUrl= [NSURL fileURLWithPath:myPathDocs];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition

presetName:AVAssetExportPresetHighestQuality];

exporter.outputURL=self.videoUrl;

exporter.outputFileType = AVFileTypeQuickTimeMovie;

exporter.shouldOptimizeForNetworkUse = YES;

exporter.videoComposition = mainCompositionInst;

[exporter exportAsynchronouslyWithCompletionHandler:^{

dispatch_async(dispatch_get_main_queue(), ^{

[self exportDidFinish:exporter];

});

}];

}

- (void)exportDidFinish:(AVAssetExportSession*)session {

if (session.status == AVAssetExportSessionStatusCompleted) {

NSURL *outputURL = session.outputURL;

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {

//保存视频到本地相册

[library writeVideoAtPathToSavedPhotosAlbum:outputURL completionBlock:^(NSURL *assetURL, NSError *error){

dispatch_async(dispatch_get_main_queue(), ^{

if (error) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"

delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];

} else {

_playItem = [AVPlayerItem playerItemWithURL:self.videoUrl];

_player = [AVPlayer playerWithPlayerItem:_playItem];

_playerLayer =[AVPlayerLayer playerLayerWithPlayer:_player];

_playerLayer.frame = _videoView.layer.bounds;

//  _playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//视频填充模式

[self.videoView.layer addSublayer:_playerLayer];

[_player play];

}

});

}];

}

}

}

相关文章

  • 裁剪视频为正方形

    #pragma mark 裁剪视频 -(void)caiJianVideo:(NSURL*)videoUrl{ /...

  • 转载:裁剪正方形视频

    转自:http://blog.csdn.net/coderyue/article/details/52830564...

  • 自定义正方形圆角

    在最近的机顶盒媒体中心开发中,遇到这样一个需求 需要将每张图片展示为正方形,并且拥有圆角。所以先将图片裁剪为正方形...

  • Android 使用FFmpeg 裁剪出正方形视频

    到目前为止Android中还不能直接录制正方形的视频,虽然不能直接录但是我们也有一些方式来处理录制后的视频,之前我...

  • 如何在Photoshop中裁剪出圆形图像

    通常我们裁剪图像时,只能将图片裁剪成正方形,在本教程中,我们将学习如何使用Photoshop将图像裁剪成一个圆圈!...

  • IOS UIImage切圆角、正方形、旋转

    一、UIImage裁圆 二、UIImageView裁圆 有的时候image本身不是正方形的这个时候可以裁剪成正方形...

  • 图片裁剪

    其实我最主要的目的只是需要将图片裁剪成正方形... ** AGSimpleImageEditorView ** 你...

  • 视频裁剪

    AVAsset AVAsset是一个表现音视频媒体的抽象类.AVAsset对象给我们开发提供了媒体文件的访问接口....

  • 21天摄影训练营Day14

    Day14,9月10日作业:正方形构图 拍摄对象不限制 (这是补的作业) 正方形构图,不是裁剪。真正去用正方...

  • 短视频SDK如何做到视频原始比例裁剪?

    1.概述 目前阿里云短视频SDK裁剪视频提供了多种模式。填充模式和裁剪模式. 但是demo没有演示裁剪视频原始大小...

网友评论

    本文标题:裁剪视频为正方形

    本文链接:https://www.haomeiwen.com/subject/tifhettx.html