音视频裁剪以及合成

音视频合成

针对多个资源数据进行混合

音频文件和视频文件准备好

AVURLAsset 生成对应的asset

[AVURLAsset tracksWithMediaType:AVMediaTypeVideo] 

[AVURLAsset tracksWithMediaType:AVMediaTypeAudio] 

是否包含音视频轨道

音视频合成

AVMutableComposition *trimComposition = [AVMutableComposition composition]; 

AVMutableCompositionTrack *compositionVideoTrack = [trimComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

 [compositionVideoTrack insertTimeRange:trimRange ofTrack:assetVideoTrack atTime:insertionPoint error:&error];

导出文件

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:trimComposition presetName:AVAssetExportPresetPassthrough];

单个视频资源

直接裁剪,不用压缩视频格式;

利用AVURLAsset和timeRange设置,

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetPassthrough]; 

注意presentName和输出文件outputFileType是否支持,需要检测一下。

音频中断处理

添加对应的同志
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioInteruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

- (void)handleAudioInteruption:(NSNotification *)note {
    NSDictionary *info = note.userInfo;
    AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
    if (type == AVAudioSessionInterruptionTypeBegan) {
        //start interruption
    } else {
        AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
        if (options == AVAudioSessionInterruptionOptionShouldResume) {
            //handle resume
        }
    }
}

你可能感兴趣的:(音视频裁剪以及合成)