美文网首页
iOS 视频方向修正

iOS 视频方向修正

作者: _Waiting_ | 来源:发表于2024-02-17 10:07 被阅读0次
    - (void)fixVideoDirection:(AVURLAsset *)asset resultBlcok:(void (^)(NSURL *outputURL))resultBlock
    {
        
        NSString *videoName = [NSString stringWithFormat:@"%@.mp4",[[NSDate date] stringWithFormat:@"YYYY-MM-dd HH:mm:ss"]];
        NSString *outputURL = [[FileTools createDirectoryInDocumentDirectory:@"/Videos"] stringByAppendingPathComponent:videoName];
    
        AVMutableComposition *composition = [AVMutableComposition composition];
        
        // 获取音频轨道
        AVAssetTrack *audioTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
        // 插入音频轨道
        AVMutableCompositionTrack *audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:audioTrack atTime:kCMTimeZero error:nil];
        
        // 获取视频轨道
        AVAssetTrack *videoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
        // 插入视频轨道
        AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
        [videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];
        
        // 获取视频修正方向(默认为摄像头方向)
        CGAffineTransform t = videoTrack.preferredTransform;
        CGAffineTransform transform = CGAffineTransformIdentity;
        CGSize renderSize = CGSizeMake(videoTrack.naturalSize.width, videoTrack.naturalSize.height);
        
        if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0) { // LandscapeRight, 0度
           //不需要作处理
            NSLog(@"视频没有被旋转");
            
       } else if (t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0){ //90度
           
            transform = CGAffineTransformTranslate(transform, videoTrack.naturalSize.height, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2*1);
            renderSize = CGSizeMake(videoTrack.naturalSize.height, videoTrack.naturalSize.width);
            NSLog(@"视频被旋转了90度");
           
        } else if (t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0) { //180度
            
            transform = CGAffineTransformTranslate(transform, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
            transform = CGAffineTransformRotate(transform, M_PI_2*2);
            renderSize = CGSizeMake(videoTrack.naturalSize.width, videoTrack.naturalSize.height);
            NSLog(@"视频被旋转了180度");
    
        } else if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) { //270度
            
            transform = CGAffineTransformTranslate(transform, 0, videoTrack.naturalSize.width);
            transform = CGAffineTransformRotate(transform, M_PI_2*3);
            renderSize = CGSizeMake(videoTrack.naturalSize.height, videoTrack.naturalSize.width);
            NSLog(@"视频被旋转了270度");
        }
        
        AVMutableVideoCompositionLayerInstruction *layerInstruciton = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack];
        [layerInstruciton setTransform:transform atTime:kCMTimeZero]; //旋转
        
        AVMutableVideoCompositionInstruction *compositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
        compositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
        compositionInstruction.layerInstructions = @[layerInstruciton];
        
        AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
        videoComposition.instructions = @[compositionInstruction];
        videoComposition.frameDuration = CMTimeMakeWithSeconds( 1 / videoTrack.nominalFrameRate, 600);;
        videoComposition.renderScale = 1;
        videoComposition.renderSize = renderSize;
        
        // 创建视频导出会话
        AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
        session.outputFileType = AVFileTypeMPEG4;
        session.outputURL = [NSURL fileURLWithPath:outputURL];
        session.shouldOptimizeForNetworkUse = YES;
        session.videoComposition = videoComposition;
           
        // 开始导出
        [session exportAsynchronouslyWithCompletionHandler:^{
            if (session.status == AVAssetExportSessionStatusCompleted) {
                resultBlock([NSURL fileURLWithPath:outputURL]);
            } else {
                NSLog(@"视频导出失败");
            }
        }];
    }
    
    

    原文地址

    iOS 视频方向修正

    相关文章

      网友评论

          本文标题:iOS 视频方向修正

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