objective-c - 如何显示 [AVPlayerItemVideoOutput copyPixelBufferForItemTime :]? 的结果

标签 objective-c ios6 avfoundation

我在 iOS 6.1 下有一个 CADisplayLink 和一个 AVPlayerItemVideoOutput。我现在有一个 CVPixelBufferRef 并想修改它(例如在视频上显示时间码等)。我想我已经正确实现了修改部分,现在有一个带有新框架的 CGImageRef 或 CGContextRef。

我现在的问题是:我应该如何显示它?我找到了适用于 OS X 的特殊类,但没有找到适用于 iOS 的。有没有?我可以使用 GPUImage 框架作为快捷方式吗? (但是专用的 GPUImage Movie 播放器不播放音频 :( )。

(有什么高级AVFoundation的好书吗?)

这是我的代码,但绘制的文本未显示在视频中(视频和音频播放效果很好)。通过 CADisplayLink 回调调用(我检查过)代码。

CVPixelBufferRef ref = [_playerItemVideoOutput copyPixelBufferForItemTime:_playerItem.currentTime itemTimeForDisplay:nil];
size_t width = CVPixelBufferGetWidth(ref);
size_t height = CVPixelBufferGetHeight(ref);
size_t bytes_per_row = CVPixelBufferGetBytesPerRow(ref);

CVPixelBufferLockBaseAddress(ref, 0);
void *pxdata = CVPixelBufferGetBaseAddress(ref);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, width,
        height, 8, bytes_per_row, rgbColorSpace, kCGImageAlphaNoneSkipFirst);
CGContextRetain(context);

CGContextSelectFont(context, "Helvetica", 10, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextShowTextAtPoint(context, 0.0, 0.0, "O BROTHER WHERE ARE THOU?", 20);

CGColorSpaceRelease(rgbColorSpace);
CVPixelBufferUnlockBaseAddress(ref, 0);
CGContextRelease(context);
CVBufferRelease(ref);

编辑: 我的问题是/曾经是,您无法使用 AVPlayerLayer 显示修改后的像素缓冲区。您必须通过其他方法输出它(在 Mac 上有一个 AVSampleBufferOutput,但在 iOS 上没有公共(public)等效项:/)。

最佳答案

我想你问错了问题。似乎您想实时处理视频,但随后您询问了 CGImageRef

我推荐你学习 Session 517 of WWDC 2012 。有 sample code for OSX and iOS ,下载所有 [相关] AVFoundation 示例并检查它们。搜索新的 iOS 6 方法来缩小您的搜索范围。

一旦您准备好代码(基本上是一个 CADisplayLink 回调,您将在适当的时候使用 copyPixelBufferForItemTime:itemTimeForDisplay: 提取像素缓冲区),您可以使用 GPUImage、Core Image 或您自己的 OpenGL 代码修改像素缓冲区.所有这些都必须在视频即将播放时完成 AVPlayer , 否则你只能显示图像而不能显示音频。至少不同步音频。

如果你问一些具体的问题,我可能会帮助你更多。

关于objective-c - 如何显示 [AVPlayerItemVideoOutput copyPixelBufferForItemTime :]? 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355482/

相关文章:

iphone - iOS - UILocalNotification 不触发(在后台设置时)

ios - Swift 3 - Tesseract 识别任何图像处理的抛出错误

ios - AVFoundation 的 iPhone 5 视网膜显示屏长宽比

iphone - 将对象作为参数传递给 iOS 上 Objective C 中的 wcf 服务

objective-c - 在一个库中声明变量或函数并在另一个库中定义它

iphone - UIWebview 中的 Youtube 视频不起作用

cordova - IFrame 滚动无法通过phonegap 在 ios 应用程序中工作

ios - iPad 6.0 中 UINavigationbar 背景色问题 setTintColor

ios - AVAudioSession setCategory Swift 4.2 iOS 12 - 静音播放声音

iphone - 使用标签检索 UITextField