Iphone 在当前帧内旋转视频

Iphone Rotate video within current frame

嗨,我有一个视频 宽度 = 720 高度 = 1280

所以视频是人像模式。

我想让视频旋转 90 度。 但是想要旋转的视频在相同大小的视频中,即 720,1280。 是的视频将被缩放, 但我需要这种旋转。

请检查图像 之前之后 :

这是我的代码:

 CGAffineTransform rotationTransform =  CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90.0));
    CGAffineTransform rotateTranslate =   CGAffineTransformTranslate(rotationTransform,400,320);
    [layerInst setTransform:rotateTranslate atTime:kCMTimeZero];

您可以同时使用缩放、旋转和平移变换,并将每个变换连接到原始资产变换。请针对您的问题尝试以下代码

        // here assetTransform is your AVAssetTrack preferred transform
        CGAffineTransform defaultTransfrom = assetTransform;
        // rotate 90 degree
        CGAffineTransform rotateTransform = CGAffineTransformMakeRotation( M_PI_2);
        //get scale factor of resized video
        float scaleFactor = videoSize.width/videoSize.height;
        CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
        // get blank Space At Bottom after rotation and scaling
        float blankSpaceAtBottom = (videoSize.height-scaleFactor*videoSize.width);
        //increase y translation according to scaling factor
        float ytranslation = blankSpaceAtBottom/2/scaleFactor;
        CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(videoSize.height,ytranslation);
        //concat all transform
        CGAffineTransform finalTransform = CGAffineTransformConcat(CGAffineTransformConcat(defaultTransfrom,rotateTransform),translationTransform);
       finalTransform = CGAffineTransformConcat(finalTransform,scaleTransform);
        //apply final transform to layer instruction
        [layerInstruction setTransform:finalTransform atTime:kCMTimeZero];

如果您需要更多说明,请告诉我。

fileprivate func rotateTrack(for track: AVAssetTrack, orientation: Orientation) -> CGAffineTransform {
    let size = track.naturalSize
    var transform: CGAffineTransform = .identity
    //get scale factor of resized video
    var scaleFactor: CGFloat = CGFloat(track.naturalSize.width / track.naturalSize.height)
    // get blank Space At Bottom after rotation and scaling
    var blankSpaceAtBottom = 0
    //update x translation according to scaling factor
    var x_translation: CGFloat = 0
    //increase y translation according to scaling factor
    var y_translation: CGFloat = 0

    var defaultRotated = false
    if size.width > size.height {
        defaultRotated = true
        scaleFactor = CGFloat(track.naturalSize.height / track.naturalSize.width)
    }
    let rotate_90: (CGFloat, CGFloat, CGFloat) = (.pi/2, 0, -size.height)
    let rotate_180: (CGFloat, CGFloat, CGFloat) = (.pi, -size.width, -size.height)
    let rotate_270: (CGFloat, CGFloat, CGFloat) = ((3 * .pi) / 2, -size.width, 0)
    let rotate_360: (CGFloat, CGFloat, CGFloat) = (2 * .pi, 0, 0)

    var transformValue: (CGFloat, CGFloat, CGFloat) = (0, 0, 0)
    switch orientation {
        case .upside:
            transformValue = defaultRotated ? rotate_90 : rotate_360
            if defaultRotated {
                blankSpaceAtBottom = Int(track.naturalSize.height - CGFloat(scaleFactor) * track.naturalSize.width)
                x_translation = track.naturalSize.height
                y_translation = CGFloat(blankSpaceAtBottom / 2) / scaleFactor
            }
            scaleFactor = 1
        case .left:
            transformValue = defaultRotated ? rotate_360 : rotate_270
            if defaultRotated {
                scaleFactor = 1
            } else {
                blankSpaceAtBottom = Int(track.naturalSize.height + CGFloat(scaleFactor) * track.naturalSize.width)
                x_translation = 0
                y_translation = CGFloat(blankSpaceAtBottom / 2) / scaleFactor
            }
        case .upsideDown:
            transformValue = defaultRotated ? rotate_270 : rotate_180
            if defaultRotated {
                blankSpaceAtBottom = Int(track.naturalSize.height + CGFloat(scaleFactor) * track.naturalSize.width)
                x_translation = 0
                y_translation = CGFloat(blankSpaceAtBottom / 2) / scaleFactor
            } else {
                x_translation = track.naturalSize.width
                y_translation = track.naturalSize.height
            }
            scaleFactor = 1
        case .right:
            transformValue = defaultRotated ? rotate_180 : rotate_90
            if defaultRotated {
                scaleFactor = 1
                x_translation = track.naturalSize.width
                y_translation = track.naturalSize.height
            } else {
                blankSpaceAtBottom = Int(track.naturalSize.height - CGFloat(scaleFactor) * track.naturalSize.width)
                x_translation = track.naturalSize.height
                y_translation = CGFloat(blankSpaceAtBottom / 2) / scaleFactor
            }
    }
    print(x_translation, y_translation)
    transform = applyTransformValue(transformValue.0, transformValue.1, transformValue.2)

    let scaleTransform = CGAffineTransform(scaleX: CGFloat(scaleFactor), y: CGFloat(scaleFactor))
    let translationTransform = CGAffineTransform(translationX: x_translation, y: CGFloat(y_translation))
    var finalTransform = transform.concatenating(translationTransform)
    finalTransform = finalTransform.concatenating(scaleTransform)
    return finalTransform
}

private func applyTransformValue(_ rotation: CGFloat, _ xValue: CGFloat, _ yValue: CGFloat) -> CGAffineTransform {
    var transform: CGAffineTransform = .identity
    transform = transform.rotated(by: rotation)
    return transform
}