声明:此文我是根据
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+|+iPhone+Developer+and+Gamer)
这个编写出来的。
原文是合并视频.而我的项目要求是裁剪视频,所以我跟他的代码实现了自己的需求而已。
目前这块代码应该是能给视频里面添加logo之类的东西。但是我目前没实现,网上的代码我也暂时没找到。
- (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath {
NSLog(@"nv_strVideoPath = %@ nv_strSavePath = %@n ",v_strVideoPath,v_strSavePath);
AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]];
CMTime assetTime = [avAsset duration];
Float64 duration = CMTimeGetSeconds(assetTime);
NSLog(@"视频时长 %fn",duration);
AVMutableComposition *avMutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrac k *avMutableCompositionTrac k = [avMutableComposition addMutableTrackWithMedia Type:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
NSError *error = nil;
// 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)
[avMutableCompositionTrac k insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))
ofTrack:avAssetTrack
atTime:kCMTimeZero
error:&error];
AVMutableVideoCompositio n *avMutableVideoCompositio n = [[AVMutableVideoCompositio n videoComposition] retain];
// 这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。
avMutableVideoCompositio n.renderSize = CGSizeMake(320.0f, 480.0f);
avMutableVideoCompositio n.frameDuration = CMTimeMake(1, 30);
// 这句话暂时不用理会,我正在看是否能添加logo而已。
[self addDataToVideoByTool:avMutableVideoCompositio n.animationTool];
AVMutableVideoCompositio nInstruction *avMutableVideoCompositio nInstruction = [AVMutableVideoCompositio nInstruction videoCompositionInstruct ion];
[avMutableVideoCompositio nInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];
AVMutableVideoCompositio nLayerInstruction *avMutableVideoCompositio nLayerInstruction = [AVMutableVideoCompositio nLayerInstruction videoCompositionLayerIns tructionWithAssetTrack:avAssetTrack];
[avMutableVideoCompositio nLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];
avMutableVideoCompositio nInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositio nLayerInstruction];
avMutableVideoCompositio n.instructions = [NSArray arrayWithObject:avMutableVideoCompositio nInstruction];
NSFileManager *fm = [[NSFileManager alloc] init];
if ([fm fileExistsAtPath:v_strSavePath]) {
NSLog(@"video is have. then delete that");
if ([fm removeItemAtPath:v_strSavePath error:&error]) {
NSLog(@"delete is ok");
}else {
NSLog(@"delete is no error = %@",error.description);
}
}
[fm release];
AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x4 80];
[avAssetExportSession setVideoComposition:avMutableVideoCompositio n];
[avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];
[avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie ];
[avAssetExportSession setShouldOptimizeForNetw orkUse:YES];
[avAssetExportSession exportAsynchronouslyWith CompletionHandler:^(void){
switch (avAssetExportSession.status) {
case AVAssetExportSessionStat usFailed:
NSLog(@"exporting failed %@",[avAssetExportSession error]);
break;
case AVAssetExportSessionStat usCompleted:
NSLog(@"exporting completed");
// 想做什么事情在这个做
[self cutImageByVideoPath:v_strSavePath];
break;
case AVAssetExportSessionStat usCancelled:
NSLog(@"export cancelled");
break;
}
}];
if (avAssetExportSession.status != AVAssetExportSessionStat usCompleted){
NSLog(@"Retry export");
}
[avMutableVideoCompositio n release];
[avAssetExportSession release];
}
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+RayWenderlich+(Ray+Wenderlich+|+iPhone+Developer+and+Gamer)
这个编写出来的。
原文是合并视频.而我的项目要求是裁剪视频,所以我跟他的代码实现了自己的需求而已。
目前这块代码应该是能给视频里面添加logo之类的东西。但是我目前没实现,网上的代码我也暂时没找到。
- (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath {
NSLog(@"nv_strVideoPath = %@ nv_strSavePath = %@n ",v_strVideoPath,v_strSavePath);
AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]];
CMTime assetTime = [avAsset duration];
Float64 duration = CMTimeGetSeconds(assetTime);
NSLog(@"视频时长 %fn",duration);
AVMutableComposition *avMutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrac k *avMutableCompositionTrac k = [avMutableComposition addMutableTrackWithMedia Type:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
NSError *error = nil;
// 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)
[avMutableCompositionTrac k insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))
ofTrack:avAssetTrack
atTime:kCMTimeZero
error:&error];
AVMutableVideoCompositio n *avMutableVideoCompositio n = [[AVMutableVideoCompositio n videoComposition] retain];
// 这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。
avMutableVideoCompositio n.renderSize = CGSizeMake(320.0f, 480.0f);
avMutableVideoCompositio n.frameDuration = CMTimeMake(1, 30);
// 这句话暂时不用理会,我正在看是否能添加logo而已。
[self addDataToVideoByTool:avMutableVideoCompositio n.animationTool];
AVMutableVideoCompositio nInstruction *avMutableVideoCompositio nInstruction = [AVMutableVideoCompositio nInstruction videoCompositionInstruct ion];
[avMutableVideoCompositio nInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];
AVMutableVideoCompositio nLayerInstruction *avMutableVideoCompositio nLayerInstruction = [AVMutableVideoCompositio nLayerInstruction videoCompositionLayerIns tructionWithAssetTrack:avAssetTrack];
[avMutableVideoCompositio nLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];
avMutableVideoCompositio nInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositio nLayerInstruction];
avMutableVideoCompositio n.instructions = [NSArray arrayWithObject:avMutableVideoCompositio nInstruction];
NSFileManager *fm = [[NSFileManager alloc] init];
if ([fm fileExistsAtPath:v_strSavePath]) {
NSLog(@"video is have. then delete that");
if ([fm removeItemAtPath:v_strSavePath error:&error]) {
NSLog(@"delete is ok");
}else {
NSLog(@"delete is no error = %@",error.description);
}
}
[fm release];
AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x4 80];
[avAssetExportSession setVideoComposition:avMutableVideoCompositio n];
[avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];
[avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie ];
[avAssetExportSession setShouldOptimizeForNetw orkUse:YES];
[avAssetExportSession exportAsynchronouslyWith CompletionHandler:^(void){
switch (avAssetExportSession.status) {
case AVAssetExportSessionStat usFailed:
NSLog(@"exporting failed %@",[avAssetExportSession error]);
break;
case AVAssetExportSessionStat usCompleted:
NSLog(@"exporting completed");
// 想做什么事情在这个做
[self cutImageByVideoPath:v_strSavePath];
break;
case AVAssetExportSessionStat usCancelled:
NSLog(@"export cancelled");
break;
}
}];
if (avAssetExportSession.status != AVAssetExportSessionStat usCompleted){
NSLog(@"Retry export");
}
[avMutableVideoCompositio n release];
[avAssetExportSession release];
}
本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1772553,如需转载请自行联系原作者