美文网首页
iOS 对视频进行剪辑

iOS 对视频进行剪辑

作者: 流云_henry | 来源:发表于2019-12-20 14:52 被阅读0次
    
    /**
    
     剪切视频
    
     @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;
    
    }
    
    
    
    
    

    相关文章

      网友评论

          本文标题:iOS 对视频进行剪辑

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