简体   繁体   English

iOS:如何使用AVAssetWriter制作包含一系列图像的电影

[英]iOS: How to make a movie with a series of images using AVAssetWriter

I have seen this question asked many times in different forms both here and in other forums. 我已经在这里和其他论坛中以不同的形式多次看到这个问题。 Some of the questions get answered, some do not. 有些问题得到了解答,有些则没有。 There are a few where the answerer or author claims to have had success. 回答者或作者声称有一些成功的地方。 I have implemented the examples from those that claim success, but have yet to see the same results. 我已经实现了那些声称成功的例子,但还没有看到相同的结果。

I am able to successfully use AVAssetWriter (and AVAssetWriterInputPixelBufferAdaptor) to write image data and audio data simultaneously when the sample buffers are obtained from an AVCaptureSession. 当从AVCaptureSession获取样本缓冲区时,我能够成功地使用AVAssetWriter(和AVAssetWriterInputPixelBufferAdaptor)同时写入图像数据和音频数据。 However, if I have CGImageRef's that were generated in some other way, and build a CVPixelBufferRef "from scratch", the appendPixelBuffer:withPresentationTime method of AVAssetWriterInputPixelBufferAdaptor succeeds for a few frames and then fails for all subsequent frames. 但是,如果我有以其他方式生成的CGImageRef,并且“从头开始”构建CVPixelBufferRef,则AVAssetWriterInputPixelBufferAdaptor的appendPixelBuffer:withPresentationTime方法会成功几帧,然后对所有后续帧都会失败。 The resulting movie file is of course not valid. 生成的影片文件当然无效。

You can see my example code at: http://pastebin.com/FCJZJmMi 您可以在http://pastebin.com/FCJZJmMi上看到我的示例代码

The images are valid, and are verified by displaying them in a debug window (see lines 50-53). 图像有效,并通过在调试窗口中显示来验证(参见第50-53行)。 The app has been tested with Instruments, and memory utilization is low throughout the running of the app. 该应用程序已经过仪器测试,在整个应用程序运行期间内存利用率很低。 It does not get any memory warnings. 它没有得到任何内存警告。

As far as I can tell I have followed the documentation that is available. 据我所知,我已经遵循了可用的文档。 Why does the example code fail? 为什么示例代码失败? What needs to be done to fix it? 需要做些什么来解决它?

If anybody has successfully gotten AVAssetWriterInputPixelBufferAdaptor to work with their own images, please chime in. 如果有人成功获得AVAssetWriterInputPixelBufferAdaptor来处理他们自己的图像,请加入。

使用示例中的AVAssetWriter制作带有一系列图像的电影 - https://github.com/caferrara/img-to-video

There are two things that were required to make this work. 完成这项工作需要两件事。

If you are debugging your own AVAssetWriterInputPixelBufferAdaptor be careful to make sure you don't skip CMTime's and also make sure you don't ever repeat a CMTime (have exactly one frame per time slow ALWAYS). 如果您正在调试自己的AVAssetWriterInputPixelBufferAdaptor,请小心确保不要跳过CMTime,并确保不要重复CMTime(每次只有一帧慢一直)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM