我尝试根据集合图像创建视频。 图像的大小保存在 inputSize 中。 视频的大小保存在outputSize中。
我希望图像宽高适合视频中的全高。
但由于某些我无法解释的原因,最终视频显示全高图像(良好),但在水平轴上拉伸(stretch)。
如何获得正确的宽度(具有正确的纵横比)?
我的代码片段如下:
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 还应反射(reflect)与视频设置相同的大小
关于swift2 - AVAssetWriter输入: how to get the right aspect ratio of the images within the movie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35942638/