如何编码图片到H264在Mac上使用AVFoundation,不使用x264 [英] How to encode picture to H264 use AVFoundation on Mac, not use x264

查看:531
本文介绍了如何编码图片到H264在Mac上使用AVFoundation,不使用x264的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个Mac广播客户端使用FFmpeg编码到H264而不是x264库。
所以基本上,我可以从 CMSampleBufferRef AVPicture 中的AVFoundation中获取原始帧。那么有没有办法使用Apple框架将一系列这些图片编码成H264框架,像 AVVideoCodecH264
我知道编码的方式使用AVAssetWriter,但只保存视频到文件,但我不想要的文件,而是我想要有AVPacket,所以我可以发送使用FFmpeg。有人有什么想法吗?谢谢。

I'm trying to make a Mac broadcast client to encode into H264 using FFmpeg but not x264 library. So basically, I am able to get raw frames out from AVFoundation in either CMSampleBufferRef or AVPicture. So is there a way to encode a series of those pictures into H264 frames using Apple framework, like AVVideoCodecH264. I know the way to encode it use AVAssetWriter, but that only saves the video into file, but I don't want the file, instead, I'd want to have AVPacket so I can send out using FFmpeg. Does anyone have any idea? Thank you.

推荐答案

参考 VideoCore项目,我可以使用苹果的VideoToolbox框架进行硬件编码。

After refer to VideoCore project, I'm able to use Apple's VideoToolbox framework to encode in hardware.


  1. 启动一个VTCompressionSession:

  1. Start an 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

  • push the raw frame to the 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);
        }
    


  • 获取VTCallback中的编码帧,这是一个C方法用作VTCompressionSessionCreate()的参数

  • Get the encoded frame in the VTCallback, this is a C method to be used as a parameter of VTCompressionSessionCreate()

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


  • 这篇关于如何编码图片到H264在Mac上使用AVFoundation,不使用x264的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆