swift2 - AVAssetWriter输入: how to get the right aspect ratio of the images within the movie?

标签 swift2 avfoundation video-processing avassetwriter

我尝试根据集合图像创建视频。 图像的大小保存在 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/

相关文章:

swift - Swift 中的底部类型

ios - 从 Swift 中的 Cell 的 indexPath 获取索引整数

ios - 解析 : PFObject is loaded without Attributes

vb.net - ffmpeg,如何扩展视频,同一个剪辑多次concat

xcode - 我可以使用 Xcode 7 在 Swift 1(适用于 iOS 7 和 8)中编写应用程序吗?

ios - 此媒体所需的编码器正忙

ios - 使用 AVAssetImageGenerator 的 copyCGImageAtTime 为 Assets 的特定帧抛出 -11800 未知错误

iphone - 如何在 iPhone 中使用 URL 链接播放音乐文件?

ffmpeg - 使用ffmpeg垂直或水平堆叠(马赛克)几个视频?

c++ - 在opencv中读取视频文件的连续图像