AVFoundation exporting orientation wrong

前端 未结 2 1801
不思量自难忘°
不思量自难忘° 2021-02-03 10:29

I\'m attempting to combine an image and a video. I have them combining and exporting however it\'s rotated side ways.

Sorry for the bulk code paste. I\'ve seen answers

相关标签:
2条回答
  • 2021-02-03 11:02

    here is the document for the orientation at Apple:

    https://developer.apple.com/library/ios/qa/qa1744/_index.html

    if your original video was taken in portrait mode iOS, it's nature size will still be landscape, but it comes with a metadata of rotate in the mov file. In order to rotate your video, you need to make changes to your 1st piece of code with the following:

    videoLayer.frame    = CGRectMake(0, 0, videoSize.height, videoSize.width) //notice the switched width and height
    ...
    videoComp.renderSize = CGSizeMake(videoSize.height,videoSize.width) //this make the final video in portrait
    ...
    layerInstruction.setTransform(videoTrack.preferredTransform, atTime: kCMTimeZero) //important piece of information let composition know you want to rotate the original video in output
    

    Yes, you are really close!

    0 讨论(0)
  • 2021-02-03 11:21

    Maybe U should check the videoTrack's preferredTransform so to give it a exact renderSize and transform:

    CGAffineTransform transform = assetVideoTrack.preferredTransform;
    CGFloat rotation = [self rotationWithTransform:transform]; 
    //if been rotated
            if (rotation != 0)
            {
                //if rotation is 360°
                if (fabs((rotation - M_PI * 2)) >= valueOfError) {
    
                    CGFloat m = rotation / M_PI;
                    CGAffineTransform t1;
                    //rotation is 90° or 270°
                    if (fabs(m - 1/2.0) < valueOfError || fabs(m - 3/2.0) < valueOfError) {
                        self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);
                        t1 = CGAffineTransformMakeTranslation(assetVideoTrack.naturalSize.height, 0);
                    }
                    //rotation is 180°
                    if (fabs(m - 1.0) < valueOfError) {
                        t1 = CGAffineTransformMakeTranslation(assetVideoTrack.naturalSize.width, assetVideoTrack.naturalSize.height);
                    }
                    CGAffineTransform t2 = CGAffineTransformRotate(t1,rotation);
                    //                CGAffineTransform transform = makeTransform(1.0, 1.0, 90, videoTrack.naturalSize.height, 0);
                    [passThroughLayer setTransform:t2 atTime:kCMTimeZero];
                }
            }
    
    //convert transform to radian
    - (CGFloat)rotationWithTransform:(CGAffineTransform)t
    {
        return atan2f(t.b, t.a);
    }
    
    0 讨论(0)
提交回复
热议问题