AVAssetWriterInput - 添加具有特定持续时间的图像

时间:2017-08-09 07:23:51

标签: swift image video avfoundation avassetwriter

我正在使用这段代码创建一个带有图像集合的间隔拍摄电影。

https://github.com/acj/TimeLapseBuilder-Swift/blob/master/TimeLapseBuilder/TimeLapseBuilder.swift

所以现在我想在图像之间添加一个特定的持续时间。例如......每个图像都应该显示3秒钟,但视频的fps应该仍然是30 fps。

有人可以帮我吗?

  videoWriterInput.requestMediaDataWhenReady(on: media_queue) {
                    let fps: Int32 = 30
                    let frameDuration = CMTimeMake(1, fps)
                    let currentProgress = Progress(totalUnitCount: Int64(self.photoURLs.count))

                    var frameCount: Int64 = 0
                    var remainingPhotoURLs = [String](self.photoURLs)

                    while videoWriterInput.isReadyForMoreMediaData && !remainingPhotoURLs.isEmpty {
                        let nextPhotoURL = remainingPhotoURLs.remove(at: 0)
                        let lastFrameTime = CMTimeMake(frameCount, fps)
                        let presentationTime = frameCount == 0 ? lastFrameTime : CMTimeAdd(lastFrameTime, frameDuration)

                        if !self.appendPixelBufferForImageAtURL(nextPhotoURL, pixelBufferAdaptor: pixelBufferAdaptor, presentationTime: presentationTime) {
                            error = NSError(
                                domain: kErrorDomain,
                                code: kFailedToAppendPixelBufferError,
                                userInfo: ["description": "AVAssetWriterInputPixelBufferAdapter failed to append pixel buffer"]
                            )

                            break
                        }

                        frameCount += 1

                        currentProgress.completedUnitCount = frameCount
                        progress(currentProgress)
                    }

                    videoWriterInput.markAsFinished()
                    videoWriter.finishWriting {
                        if let error = error {
                            failure(error)
                        } else {
                            success(videoOutputURL)
                        }

                        self.videoWriter = nil
                    }
                }
            } else {
                error = NSError(
                    domain: kErrorDomain,
                    code: kFailedToStartAssetWriterError,
                    userInfo: ["description": "AVAssetWriter failed to start writing"]
                )
            }

非常感谢你的帮助!

0 个答案:

没有答案