AVAssetWriterInput:如何获得电影中图像的正确纵横比?

AVAssetWriterInput: how to get the right aspect ratio of the images within the movie?

我尝试根据 collection 图片制作视频。 图像的大小保存在 inputSize 中。 视频的大小保存在 outputSize.

我希望视频中的图像纵横比适合全高。

但由于某些我无法解释的原因,最终视频显示了全高图像(良好)但在水平轴上被拉伸了。

如何获得正确的宽度(具有正确的纵横比)?

我的代码片段如下:

static let videoW: CGFloat =  720
static let videoH: CGFloat = 1280
let videoRatio = videoW / videoH

var inputSize = CGSize(width: 480, height:700)
static let outputSize = CGSize(width: videoW, height: videoH)



    if let videoWriter = videoWriter {
        let videoSettings: [String : AnyObject] = [
            AVVideoCodecKey  : AVVideoCodecH264,
            AVVideoWidthKey  : MovieBuilder.outputSize.width,
            AVVideoHeightKey : MovieBuilder.outputSize.height,
        ]

        let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)

        let sourceBufferAttributes = [String : AnyObject](dictionaryLiteral:
            (kCVPixelBufferPixelFormatTypeKey as String, Int(kCVPixelFormatType_32ARGB)),
            (kCVPixelBufferWidthKey as String, Float(inputSize.width)),
            (kCVPixelBufferHeightKey as String, Float(inputSize.height))
        )

您好,在您的视频设置中尝试将此添加到您的字典的一部分

AVVideoScalingModeKey : AVVideoScalingModeResizeAspectFill.

源 bufferAttributes 也应反映与视频设置相同的大小