AV Foundation专题

AVFoundation:媒体的编辑和组合

2020-09-04  本文已影响0人  MonKey_Money
image.png image.png

媒体的编辑

自己手动实现的编辑和播放

-(void)combinateVideo {
    dispatch_queue_t queue = dispatch_queue_create("1222", NULL);
    dispatch_async(queue, ^{
        
        self.composition = [AVMutableComposition composition];
        AVAsset *firstVideoAsset = [AVAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"01_nebula" withExtension:@"mp4"]];
        AVAsset *secondVideoAsset = [AVAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"03_nebula" withExtension:@"mp4"]];
        AVAsset *firstAudioAsset =[AVAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"02 Keep Going" withExtension:@"m4a"]];
        AVMutableCompositionTrack *videoTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        
        AVMutableCompositionTrack *audioTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        
        AVAssetTrack *assetTrack;
        NSError *error;
        assetTrack = [[firstVideoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
        [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(5, 1)) ofTrack:assetTrack atTime:kCMTimeZero error:&error];
        
        assetTrack = [[secondVideoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
        [videoTrack insertTimeRange:CMTimeRangeMake(CMTimeMake(2, 1), CMTimeMake(5, 1)) ofTrack:assetTrack atTime:CMTimeMake(5, 1) error:&error];
        
        assetTrack = [[firstAudioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
        [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(10, 1)) ofTrack:assetTrack atTime:kCMTimeZero error:&error];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self playe];
        });
        
        
    });
    
}

-(void)playe {
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:[self.composition copy]];
    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
    //创建AVPlayer
    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
        
    //将Player赋值给AVPlayerViewController
    playerVC.player = player;

    [self presentViewController:playerVC animated:YES completion:nil];
    
}

通过两个轨道一个音频一个视频,进行音频或者视频的编辑。playe方法是通过AVKit进行播放视频。

音频视频的编辑

在音频组合中,我们可以看到方法

[videoTrack insertTimeRange:CMTimeRangeMake(CMTimeMake(2, 1), CMTimeMake(5, 1)) ofTrack:assetTrack atTime:CMTimeMake(5, 1) error:&error];

其中timeRange参数为CMTimeRange类型,start为需要加入AVAsset资源的起始时间位置,duration是时长,我们可以通过这个参数来进行对视频的剪切。
insertEmptyTimeRange:可以加长音视频。
removeTimeRange:可以缩短音视频

导出音视频

导出音视频用到AVAssetExportSession类

 NSString *preset = AVAssetExportPresetHighestQuality;
self.exportSession = [AVAssetExportSession exportSessionWithAsset:[self.composition copy]
                                             presetName:preset];
 self.exportSession.outputURL = [self exportURL];
    self.exportSession.outputFileType = AVFileTypeMPEG4;
    [self.exportSession exportAsynchronouslyWithCompletionHandler:^{        // 2

        dispatch_async(dispatch_get_main_queue(), ^{                        // 1
            AVAssetExportSessionStatus status = self.exportSession.status;
            if (status == AVAssetExportSessionStatusCompleted) {
//可以写入到相册中
            } else {
             
            }
        });
    }];

混合音频

初始化

AVAudioMix类及其相关类的示意图

image.png

混合音频只能在AVPlayerItem和AVAssetExportSession设置

   AVMutableAudioMixInputParameters *audioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
        [audioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:0.5 timeRange:CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(5, 1))];
        [audioMixInputParameters setVolumeRampFromStartVolume:0.5 toEndVolume:0.0 timeRange:CMTimeRangeMake(CMTimeMake(5, 1), CMTimeMake(5, 1))];
        

        self.myMutableAudioMix = [AVMutableAudioMix audioMix];
        self.myMutableAudioMix.inputParameters = @[audioMixInputParameters];

在AVPlayerItem设置

 AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:[self.composition copy]];
    item.audioMix = self.myMutableAudioMix;
    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
    //创建AVPlayer
    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
        
    //将Player赋值给AVPlayerViewController
    playerVC.player = player;

    [self presentViewController:playerVC animated:YES completion:nil];

AVAssetExportSession导出视频中设置混合效果

  NSString *preset = AVAssetExportPresetHighestQuality;
    self.exportSession = [AVAssetExportSession exportSessionWithAsset:self.composition presetName:preset];
     self.exportSession.outputURL = [self exportURL];
    self.exportSession.outputFileType = AVFileTypeMPEG4;
    self.exportSession.audioMix = self.myMutableAudioMix;

    [self.exportSession exportAsynchronouslyWithCompletionHandler:^{

        dispatch_async(dispatch_get_main_queue(), ^{
            AVAssetExportSessionStatus status = self.exportSession.status;
            if (status == AVAssetExportSessionStatusCompleted) {
               
              //可以保存相册 
            }
        });
    }];
上一篇下一篇

猜你喜欢

热点阅读