繁体   English   中英

如何在Mac上使用AVFoundation将图片编码为H264,而不使用x264

[英]How to encode picture to H264 use AVFoundation on Mac, not use x264

我正在尝试使Mac广播客户端使用FFmpeg而不是x264库编码为H264。 因此,基本上,我可以从CMSampleBufferRefAVPicture AVFoundation中获取原始帧。 因此,有一种方法可以使用Apple框架(例如AVVideoCodecH264将一系列这些图片编码为H264帧。 我知道使用AVAssetWriter对其进行编码的方法,但这仅将视频保存到文件中,但是我不想要该文件,而是想要AVPacket,以便可以使用FFmpeg发送出去。 有人有什么主意吗? 谢谢。

参考VideoCore项目后 ,我可以使用Apple的VideoToolbox框架在硬件中进行编码。

  1. 启动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; } 
  2. 将原始帧推送到VTCompressionSession

     // Delegate method from the AVCaptureVideoDataOutputSampleBufferDelegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)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); } 
  3. 获取VTCallback中的编码帧,这是一个C方法,用作VTCompressionSessionCreate()的参数

     void vtCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer ) { // Do whatever you want with the sampleBuffer CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer); } 

暂无
暂无

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

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