AVMutableComposition - Exporting wrong video transform

三世轮回 提交于 2019-12-05 07:53:00
Ofir Malachi

OK, my answer code is not organized, but that will do the work:

NOTE: when you using "export Session", set render size as the code below :-

"[AVMixerBase shared].renderSize"

AVURLAsset*videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil];
AVAssetTrack*videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
[[AVMixerBase shared]setVideoAsset:videoAsset];

AVMutableComposition *mixComposition = [AVMixerBase compositionVideoTrackAssetUrl:videoAsset]; /* audio + video */
AVMutableCompositionTrack *compositionVideoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] firstObject];
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];

CALayer *videoLayer = [self videoLayerAssetUrl:videoAsset];
CALayer *parentLayer = [CALayer layer];
CGSize videoSize = [videoAssetTrack naturalSize];
_segments = file.prefferedTranslation.customSegments;
NSLog(@"_segments %@",_segments);


//set render sizes:
//3.0_______COMBINE ALL
UIImageOrientation orientation = [self videoOrientation:videoAsset];
switch (orientation) {
    case UIImageOrientationUp:{ NSLog(@"Up"); //camera - lansdscape right
        [AVMixerBase shared].renderSize = CGSizeMake(videoSize.width, videoSize.height);
        break;
    }
    case UIImageOrientationDown:{ NSLog(@"Down"); //camera = lansdscape left
        [AVMixerBase shared].renderSize = CGSizeMake(videoSize.width, videoSize.height);
        videoLayer.affineTransform = CGAffineTransformMakeRotation(M_PI);
        break;
    }
    case UIImageOrientationLeft:{ NSLog(@"Left");

        break;
    }
    case UIImageOrientationRight:{ NSLog(@"Right");
        //camera = up
        CGAffineTransform t1 = CGAffineTransformMakeTranslation(videoSize.height, 0);
        CGAffineTransform t2 = CGAffineTransformRotate(t1, DEGREES_TO_RADIANS(90.0));
        [layerInstruction setTransform:t2 atTime:kCMTimeZero];
        [AVMixerBase shared].renderSize = CGSizeMake(videoSize.height, videoSize.width);
        break;
    }

    default:
        break;
}


  +(AVMutableComposition *)compositionVideoTrackAssetUrl:(AVURLAsset*)assetUrl{
AVMutableComposition* mixComposition = [AVMutableComposition composition];
mixComposition = [AVMixerBase addVideoTrackAssetUrl:assetUrl mixComposition:mixComposition];
return mixComposition;
}


   +(AVMutableComposition *)addVideoTrackAssetUrl:(AVURLAsset*)assetUrl mixComposition:(AVMutableComposition*)mixComposition{

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *clipVideoTrack = [[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *clipAudioTrack = [[assetUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
[compositionVideoTrack setPreferredTransform:[[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

return mixComposition;
}


-(CALayer*)videoLayerAssetUrl:(AVURLAsset*)assetUrl{
CGSize sizeOfVideo = [[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];
CALayer *videoLayer = [CALayer layer];
videoLayer.frame = CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height);
//    videoLayer.frame = CGRectMake(0, 0, sizeOfVideo.height, sizeOfVideo.width);
//    [videoLayer setAffineTransform:assetUrl.preferredTransform];


return videoLayer;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!