2016-03-11 41 views
1

我嘗試基於收集圖像創建視頻。 圖像的大小保存在inputSize中。 視頻的大小保存在outputSize中。AVAssetWriterInput:如何獲得影片中圖像的正確縱橫比?

我希望視頻中的圖像方面適合全高。

但由於某些原因,我無法解釋,最終的視頻顯示全高度圖像(好),但橫向軸伸展。

我怎樣才能得到正確的寬度(與正確的長寬比)?

我的代碼片段如下:

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)) 
     ) 

回答

2

您好您的視頻設置,你可以添加以下到您的字典

AVVideoScalingModeKey的一部分:AVVideoScalingModeResizeAspectFill。

源緩衝區屬性還應該反映與視頻設置相同的大小

+0

您是對的。我現在像一種魅力。謝謝。 –