博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS AVAssetExportSession 视频剪切、合并、压缩
阅读量:6436 次
发布时间:2019-06-23

本文共 4299 字,大约阅读时间需要 14 分钟。

1 - (void) loadVideoByPath:(NSString*) v_strVideoPath andSavePath:(NSString*) v_strSavePath { 2   3   NSLog(@"\nv_strVideoPath = %@ \nv_strSavePath = %@\n ",v_strVideoPath,v_strSavePath); 4     AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:v_strVideoPath]]; 5     CMTime assetTime = [avAsset duration]; 6     Float64 duration = CMTimeGetSeconds(assetTime); 7     NSLog(@"视频时长 %f\n",duration); 8     9     AVMutableComposition *avMutableComposition = [AVMutableComposition composition];10    11     AVMutableCompositionTrack *avMutableCompositionTrack = [avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];12    13     AVAssetTrack *avAssetTrack = [[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];14    15     NSError *error = nil;16     // 这块是裁剪,rangtime .前面的是开始时间,后面是裁剪多长 (我这裁剪的是从第二秒开始裁剪,裁剪2.55秒时长.)17     [avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(2.0f, 30), CMTimeMakeWithSeconds(2.55f, 30))18                                        ofTrack:avAssetTrack19                                         atTime:kCMTimeZero20                                          error:&error];21    22     AVMutableVideoComposition *avMutableVideoComposition = [[AVMutableVideoComposition videoComposition] retain];23 // 这个视频大小可以由你自己设置。比如源视频640*480.而你这320*480.最后出来的是320*480这么大的,640多出来的部分就没有了。并非是把图片压缩成那么大了。24     avMutableVideoComposition.renderSize = CGSizeMake(320.0f, 480.0f);25     avMutableVideoComposition.frameDuration = CMTimeMake(1, 30); 26 // 这句话暂时不用理会,我正在看是否能添加logo而已。 27     [self addDataToVideoByTool:avMutableVideoComposition.animationTool];28    29     AVMutableVideoCompositionInstruction *avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];30    31     [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];32    33     AVMutableVideoCompositionLayerInstruction *avMutableVideoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:avAssetTrack];34     [avMutableVideoCompositionLayerInstruction setTransform:avAssetTrack.preferredTransform atTime:kCMTimeZero];35 36     avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositionLayerInstruction];37        38 39     avMutableVideoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];40 41    42     NSFileManager *fm = [[NSFileManager alloc] init];43     if ([fm fileExistsAtPath:v_strSavePath]) {44         NSLog(@"video is have. then delete that");45         if ([fm removeItemAtPath:v_strSavePath error:&error]) {46             NSLog(@"delete is ok");47         }else {48             NSLog(@"delete is no error = %@",error.description);49         }50     }51     [fm release];52    53     AVAssetExportSession *avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset640x480];54     [avAssetExportSession setVideoComposition:avMutableVideoComposition];55     [avAssetExportSession setOutputURL:[NSURL fileURLWithPath:v_strSavePath]];56     [avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie];57     [avAssetExportSession setShouldOptimizeForNetworkUse:YES];58     [avAssetExportSession exportAsynchronouslyWithCompletionHandler:^(void){59         switch (avAssetExportSession.status) {60             case AVAssetExportSessionStatusFailed:61                 NSLog(@"exporting failed %@",[avAssetExportSession error]);62                 break;63             case AVAssetExportSessionStatusCompleted:64                 NSLog(@"exporting completed");65                 // 想做什么事情在这个做66                 [self cutImageByVideoPath:v_strSavePath];67                 break;68             case AVAssetExportSessionStatusCancelled:69                 NSLog(@"export cancelled");70                 break;71         }72     }];73     if (avAssetExportSession.status != AVAssetExportSessionStatusCompleted){74         NSLog(@"Retry export");75     }76     [avMutableVideoComposition release];77      [avAssetExportSession release];78 }

 

转载于:https://www.cnblogs.com/devfan/p/7238590.html

你可能感兴趣的文章
bzoj1230[Usaco2008 Nov]lites 开关灯*
查看>>
四则运算2的单元测试
查看>>
MySQL拷贝表的几种方式
查看>>
Windows环境下使用 Caffe在ImageNet上训练网络
查看>>
基于matlab 读取文件夹 保存文件夹
查看>>
Oracle synonym 同义词
查看>>
导入Jquery.min.js时 JQuery 上打红X了
查看>>
To set up, build, and run the WCF sample
查看>>
WCF实例 - 简介
查看>>
php课程---Ajax(老师详解)
查看>>
Python菜鸟之路:Python基础-Python操作RabbitMQ
查看>>
字符串文档的去重
查看>>
打印a*a的乘法表
查看>>
String、StringBuffer、StringBuilder区别
查看>>
Android--批量插入数据到SQLite数据库
查看>>
LeetCode算法题-K-diff Pairs in an Array(Java实现)
查看>>
[问题解决]基于注解配置dubbo遇到ConnectionLoss for /dubbo/xxx问题解决
查看>>
读《嵌入式系统项目分析入门与实践》⑤
查看>>
自动化Cobbler安装
查看>>
深度解析 TypeConverter & TypeConverterAttribute (二)
查看>>