ASSETWriterInput 用于在 Iphone 上从 UIImages 制作视频的问题
我尝试使用以下两种方法将 UIImage 的像素缓冲区附加到 ASSETWriterInput 。除了视频文件中没有数据之外,一切看起来都不错。怎么了?
1 适配器类
AVAssetWriterInputPixelBufferAdaptor * avAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:NULL];
[avAdaptor appendPixelBufferixelBuffer withPresentationTime:CMTimeMake(1, 10)];
2 制作
// Create sample buffer.
CMSampleBufferRef sampleBuffer = NULL;
result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDef ault, pixelBuffer, true, NULL, NULL, videoInfo, &timing, &sampleBuffer);
// Ship out the frame.
NSParameterAssert(CMSampleBufferDataIsReady(sample Buffer));
NSParameterAssert([writerInput isReadyForMoreMediaData]);
BOOL success = [writerInput appendSampleBuffer:sampleBuffer];
I try the following 2 methods of appending UIImage
s pixelbuffer to ASSETWriterInput
. Everything looks good except there's No data in the video file. What's wrong?
1 Adaptor class
AVAssetWriterInputPixelBufferAdaptor * avAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:NULL];
[avAdaptor appendPixelBufferixelBuffer withPresentationTime:CMTimeMake(1, 10)];
2 Making the
// Create sample buffer.
CMSampleBufferRef sampleBuffer = NULL;
result = CMSampleBufferCreateForImageBuffer(kCFAllocatorDef ault, pixelBuffer, true, NULL, NULL, videoInfo, &timing, &sampleBuffer);
// Ship out the frame.
NSParameterAssert(CMSampleBufferDataIsReady(sample Buffer));
NSParameterAssert([writerInput isReadyForMoreMediaData]);
BOOL success = [writerInput appendSampleBuffer:sampleBuffer];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现由于某种原因我需要多次附加缓冲区。我制作的测试应用程序示例中的计时可能不正确,但由于它有效,因此应该给您一个好主意。
此方法不是必需的,但此处用作像素缓冲区源的示例:
I found that for some reason I needed to append the buffer more than once. The timing in this example from a test app I made might not be proper, but since it works it should give you a good idea.
This method is not required, but is used here as an example of a pixel buffer source:
我对这段代码遇到了一些问题。结果它给了我一个扭曲的图像。
更改:
至:
有帮助。
I've had a bit of a problem with this code. It geave me a skewed image as a result.
Changing:
To:
helped.
等等,虽然@Peter DeWeese给出的答案是遵循的方向,但代码有两个巨大的问题:首先,你需要等待系统准备好附加新媒体,其次,你有很好的记忆力泄漏,因为您需要在将缓冲区附加到视频编写器后释放缓冲区。
在您的情况下确实如此,但在一般情况下更是如此,您希望在多个帧中循环,如下所示:
Hold on, though the answer given by @Peter DeWeese is the direction to follow, the code has two huge issues: firstly, you need to wait while the system is ready to append a new media and secondly, you've got a great memory leak as you need to release your buffer after it was appended to the video writer.
This is true in your very case, but even more in a general case, where you want to loop in multiple frame as follows: