iOS 对视频进行剪辑

2019-12-20  本文已影响0人  流云_henry

/**

 剪切视频

 @param videoPath 视频path

 @paramstart 起始时间

 @paramend 结束时间

 @return返回asset

 */

- (AVAsset *)cutVideoWithPath:(NSString *)videoPath startTime:(NSTimeInterval)start endTime:(NSTimeInterval)end;



- (AVAsset *)cutVideoWithPath:(NSString *)videoPath startTime:(NSTimeInterval)start endTime:(NSTimeInterval)end{
///更具视频路径来创建asset
    AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:videoPath]];
//1创建一个AVMutableComposition
    AVMutableComposition *composition = [[AVMutableComposition alloc] init];
//2 创建一个音频和视频的轨道,类型都为AVMediaTypeAudio
    AVMutableCompositionTrack *muTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

//创建一个轨道级检查界面的对象
    AVAssetTrack *originTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;

    AVAssetTrack *originAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
////获取videoPath的音视频插入轨道
[muTrackinsertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(start, asset.duration.timescale), CMTimeMakeWithSeconds(end, asset.duration.timescale)) ofTrack:originTrack atTime:kCMTimeZero error:nil];
 [audioTrackinsertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(start, asset.duration.timescale), CMTimeMakeWithSeconds(end, asset.duration.timescale)) ofTrack:originAudioTrack atTime:kCMTimeZero error:nil];

    muTrack.preferredTransform = originTrack.preferredTransform;

    returncomposition;

}




上一篇 下一篇

猜你喜欢

热点阅读