我正在尝试使 Mac 广播客户端使用 FFmpeg 而不是 x264 库编码到 H264 中。所以基本上,我能够在CMSampleBufferRef
或Picture
中从 Foundation 中获取原始帧。那么有没有办法使用 Apple 框架将一系列这些图片编码为 H264 帧,例如VideoCodecH264
。我知道使用 Assetwriter 将其编码的方式,但我不希望
在参考VideoCore project之后,我可以使用 Apple 的 VideoToolbox 框架在硬件中进行编码。
启动 VTCompressionSession:
// Create compression session
err = VTCompressionSessionCreate(kCFAllocatorDefault,
frameW,
frameH,
kCMVideoCodecType_H264,
encoderSpecifications,
NULL,
NULL,
(VTCompressionOutputCallback)vtCallback,
self,
&compression_session);
if(err == noErr) {
comp_session = session;
}
将原始帧推送到 VTCompressionSession
// Delegate method from the CaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(CaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(CaptureConnection *)connection {
// get pixelbuffer out from samplebuffer
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
//set up all the info for the frame
// call VT to encode frame
VTCompressionSessionEncodeFrame(compression_session, pixelBuffer, pts, dur, NULL, NULL, NULL);
}
获取 VTCallback 中的编码帧,这是一个要用作 VTCompressionSessionCreate()的参数的 C 方法
void vtCallback(void *outputCallbackRefCon,
void *sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CMSampleBufferRef sampleBuffer ) {
// Do whatever you want with the sampleBuffer
CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(87条)