gpt4 book ai didi

objective-c - 我可以使用 AVFoundation 将下载的视频帧流式传输到 OpenGL ES 纹理中吗?

转载 作者:IT王子 更新时间:2023-10-29 08:05:31 26 4
gpt4 key购买 nike

我已经能够使用 AVFoundation 的 AVAssetReader 类将视频帧上传到 OpenGL ES 纹理中。它有一个 caveat ,但是,因为它在与指向远程媒体的 AVURLAsset 一起使用时失败。这种失败没有得到很好的记录,我想知道是否有任何方法可以解决这个缺点。

最佳答案

有一些随 iOS 6 一起发布的 API,我已经能够使用它们来使这个过程变得轻而易举。它根本不使用 AVAssetReader,而是依赖于一个名为 AVPlayerItemVideoOutput 的类。此类的实例可以通过新的 -addOutput: 方法添加到任何 AVPlayerItem 实例。

AVAssetReader 不同,此类适用于由远程 AVURLAsset 支持的 AVPlayerItem,并且还具有以下优势允许更复杂的播放界面,通过 -copyPixelBufferForItemTime:itemTimeForDisplay: 支持非线性播放(而不是 AVAssetReader 的严格限制 -copyNextSampleBuffer 方法。


示例代码

// Initialize the AVFoundation state
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:someUrl options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{

NSError* error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:&error];
if (status == AVKeyValueStatusLoaded)
{
NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] };
AVPlayerItemVideoOutput* output = [[[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings] autorelease];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addOutput:[self playerItemOutput]];
AVPlayer* player = [AVPlayer playerWithPlayerItem:playerItem];

// Assume some instance variable exist here. You'll need them to control the
// playback of the video (via the AVPlayer), and to copy sample buffers (via the AVPlayerItemVideoOutput).
[self setPlayer:player];
[self setPlayerItem:playerItem];
[self setOutput:output];
}
else
{
NSLog(@"%@ Failed to load the tracks.", self);
}
}];

// Now at any later point in time, you can get a pixel buffer
// that corresponds to the current AVPlayer state like this:
CVPixelBufferRef buffer = [[self output] copyPixelBufferForItemTime:[[self playerItem] currentTime] itemTimeForDisplay:nil];

获得缓冲区后,您可以根据需要将其上传到 OpenGL。我推荐可怕的记录 CVOpenGLESTextureCacheCreateTextureFromImage() 函数,因为您将在所有较新的设备上获得硬件加速,这比 glTexSubImage2D()快很多。参见 Apple 的 GLCameraRippleRosyWriter示例演示。

关于objective-c - 我可以使用 AVFoundation 将下载的视频帧流式传输到 OpenGL ES 纹理中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12500408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com