帧持续时间 – UIImage数组到电影

我通过此代码成功完成了“将UIImage数组导出为电影”部分。 在导出的video中,每个图像显示1秒钟。 但我需要的是,每个图像都会在导出的video中显示5秒钟。 我需要做的最小改变是什么? 这是我的代码……

var outputSize = CGSize(width: 1920, height: 1280) func build(outputSize outputSize: CGSize) { let fileManager = NSFileManager.defaultManager() let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) guard let documentDirectory: NSURL = urls.first else { fatalError("documentDir Error") } let videoOutputURL = documentDirectory.URLByAppendingPathComponent("OutputVideo.mp4") if NSFileManager.defaultManager().fileExistsAtPath(videoOutputURL!.path!) { do { try NSFileManager.defaultManager().removeItemAtPath(videoOutputURL!.path!) } catch { fatalError("Unable to delete file: \(error) : \(#function).") } } guard let videoWriter = try? AVAssetWriter(URL: videoOutputURL!, fileType: AVFileTypeMPEG4) else { fatalError("AVAssetWriter error") } let outputSettings = [AVVideoCodecKey : AVVideoCodecH264, AVVideoWidthKey : NSNumber(float: Float(outputSize.width)), AVVideoHeightKey : NSNumber(float: Float(outputSize.height))] guard videoWriter.canApplyOutputSettings(outputSettings, forMediaType: AVMediaTypeVideo) else { fatalError("Negative : Can't apply the Output settings...") } let videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings) let sourcePixelBufferAttributesDictionary = [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(unsignedInt: kCVPixelFormatType_32ARGB), kCVPixelBufferWidthKey as String: NSNumber(float: Float(outputSize.width)), kCVPixelBufferHeightKey as String: NSNumber(float: Float(outputSize.height))] let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoWriterInput, sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary) if videoWriter.canAddInput(videoWriterInput) { videoWriter.addInput(videoWriterInput) } if videoWriter.startWriting() { videoWriter.startSessionAtSourceTime(kCMTimeZero) assert(pixelBufferAdaptor.pixelBufferPool != nil) let media_queue = dispatch_queue_create("mediaInputQueue", nil) videoWriterInput.requestMediaDataWhenReadyOnQueue(media_queue, usingBlock: { () -> Void in let fps: Int32 = 1 let frameDuration = CMTimeMake(1, fps) var frameCount: Int64 = 0 var appendSucceeded = true while (!self.choosenPhotos.isEmpty) { if (videoWriterInput.readyForMoreMediaData) { let nextPhoto = self.choosenPhotos.removeAtIndex(0) let lastFrameTime = CMTimeMake(frameCount, fps) let presentationTime = frameCount == 0 ? lastFrameTime : CMTimeAdd(lastFrameTime, frameDuration) var pixelBuffer: CVPixelBuffer? = nil let status: CVReturn = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pixelBufferAdaptor.pixelBufferPool!, &pixelBuffer) if let pixelBuffer = pixelBuffer where status == 0 { let managedPixelBuffer = pixelBuffer CVPixelBufferLockBaseAddress(managedPixelBuffer, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0))) let data = CVPixelBufferGetBaseAddress(managedPixelBuffer) let rgbColorSpace = CGColorSpaceCreateDeviceRGB() let context = CGBitmapContextCreate(data, Int(self.outputSize.width), Int(self.outputSize.height), 8, CVPixelBufferGetBytesPerRow(managedPixelBuffer), rgbColorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue) CGContextClearRect(context!, CGRectMake(0, 0, CGFloat(self.outputSize.width), CGFloat(self.outputSize.height))) let horizontalRatio = CGFloat(self.outputSize.width) / nextPhoto.size.width let verticalRatio = CGFloat(self.outputSize.height) / nextPhoto.size.height let aspectRatio = min(horizontalRatio, verticalRatio) // ScaleAspectFit let newSize:CGSize = CGSizeMake(nextPhoto.size.width * aspectRatio, nextPhoto.size.height * aspectRatio) let x = newSize.width < self.outputSize.width ? (self.outputSize.width - newSize.width) / 2 : 0 let y = newSize.height  Void in print("FINISHED!!!!!") } }) } } 

 let lastFrameTime = CMTimeMake(frameCount * 5, fps) let presentationTime = frameCount == 0 ? lastFrameTime : CMTimeAdd(lastFrameTime, frameDuration) CMTime t2 = CMTimeMake(2, 1); // 2 seconds It represent value/timeScale. 

只是改变:

 let frameDuration = CMTimeMake(5, fps) 

并改变这一行:

  let lastFrameTime = CMTimeMake(frameCount, fps) 

 let lastFrameTime = CMTimeMake(frameCount*5, fps) 

要么

 let lastFrameTime = frameDuration 

希望这可以帮助。