美文网首页iOS程序猿程序员
短视频从无到有 (八)视频速率调节

短视频从无到有 (八)视频速率调节

作者: 卢叁 | 来源:发表于2018-07-31 15:20 被阅读12次

    录制好的视频我们可以处理成慢速或者快速的效果。还是十分简单,AVMutableCompositionTrack这个类已经开放了接口scaleTimeRange: toDuration:,我们只需要处理成所想要的效果即可。不多说,还是直接上代码:

      // 获取视频
        AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:_fileURL options:nil];
                                  
        // 视频混合
        AVMutableComposition* mixComposition = [AVMutableComposition composition];
       // 视频轨道
       AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        // 音频轨道
      AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
      // 视频的方向
     CGAffineTransform videoTransform = [videoAsset tracksWithMediaType:AVMediaTypeVideo].lastObject.preferredTransform;
    if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) {
        NSLog(@"垂直拍摄");
        videoTransform = CGAffineTransformMakeRotation(M_PI_2);
        
     }else if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) {
        NSLog(@"倒立拍摄"); videoTransform = CGAffineTransformMakeRotation(-M_PI_2);
        
    }else if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) {
         NSLog(@"Home键右侧水平拍摄");
         videoTransform = CGAffineTransformMakeRotation(0);
        
    }else if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) {
            NSLog(@"Home键左侧水平拍摄");
            videoTransform = CGAffineTransformMakeRotation(M_PI);
            
    }
       // 根据视频的方向同步视频轨道方向
       compositionVideoTrack.preferredTransform = videoTransform;
        //时间刻度
        compositionVideoTrack.naturalTimeScale = 600;
    // 插入视频轨道
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(videoAsset.duration.value, videoAsset.duration.timescale)) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject] atTime:kCMTimeZero error:nil];
    // 插入音频轨道
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(videoAsset.duration.value, videoAsset.duration.timescale)) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] firstObject] atTime:kCMTimeZero error:nil];
     // 适配视频速度比率 譬如慢速4 快速0.3
        //10s的视频会处理成40s
        CGFloat scale = 4;
        
        [compositionVideoTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(videoAsset.duration.value,videoAsset.duration.timescale)) toDuration:CMTimeMake(videoAsset.duration.value*scale, videoAsset.duration.timescale)];
        
        
        [compositionAudioTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeMake(videoAsset.duration.value,videoAsset.duration.timescale)) toDuration:CMTimeMake(videoAsset.duration.value*scale, videoAsset.duration.timescale)];
        
        NSLog(@"输出时间结构体");
        CMTimeShow(videoAsset.duration);
        //合成视频输出路径
        NSString *path =[self getVideoSaveFilePathString:@".MOV" addPathArray:NO];
        //输出
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                          presetName:AVAssetExportPreset1280x720];
        
        exporter.outputURL = [NSURL fileURLWithPath:path];
        exporter.outputFileType = AVFileTypeQuickTimeMovie;
        exporter.shouldOptimizeForNetworkUse = YES;
        [exporter exportAsynchronouslyWithCompletionHandler:^{
           
            dispatch_async(dispatch_get_main_queue(), ^{
               
                switch (exporter.status) {
                    case AVAssetExportSessionStatusCompleted:{
                        
                        //保存到相册
                        [self savePhotosAlbum:exporter.outputURL];
                        
                        
                    }
                        
                        break;
                        
                    default:
                        break;
                }
                
                
                
                
            });
            
            
        }];
        
    

    注意这里与实时慢速拍摄视频不同。实时慢速拍摄视频也十分简单,查阅GPUImage的头文件发现GPUImageVideoCamera下发现了frameRate这个属性,譬如我们可以设置videoCamera.frameRate =5;这样就可以实现实时慢速拍摄视频了,简单吧。经过实验,发现frameRate的取值范围在0-30之间。

    有什么问题,欢迎留言讨论。

    相关文章

      网友评论

        本文标题:短视频从无到有 (八)视频速率调节

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