iOS开发基础138-视频编码

为完善视频编码的封装和提供一定的拓展性,以下是视频编码的详细示例,其中包括编码参数设置和数据提取处理。以下示例侧重于视频编码部分。

视频编码器示例

下面的代码示例展示了一个视频编码器的实现,包括如何设置关键编码参数和从回调中提取H.264数据。

// VideoEncoder.h
#import <Foundation/Foundation.h>
#import <VideoToolbox/VideoToolbox.h>

@protocol VideoEncoderDelegate <NSObject>
- (void)videoEncoderDidEncodeData:(NSData *)data isKeyFrame:(BOOL)isKeyFrame;
@end

@interface VideoEncoder : NSObject
@property (weak, nonatomic) id<VideoEncoderDelegate> delegate;
- (instancetype)initWithWidth:(int)width height:(int)height;
- (void)encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer;
@end
// VideoEncoder.m
#import "VideoEncoder.h"

@interface VideoEncoder ()
@property (assign, nonatomic) VTCompressionSessionRef compressionSession;
@property (assign, nonatomic) int width;
@property (assign, nonatomic) int height;
@end

@implementation VideoEncoder

- (instancetype)initWithWidth:(int)width height:(int)height {
    if ((self = [super init])) {
        _width = width;
        _height = height;
        [self setupCompressionSession];
    }
    return self;
}

- (void)setupCompressionSession {
    if (VTCompressionSessionCreate(NULL, _width, _height, kCMVideoCodecType_H264, NULL, NULL, NULL, compressionOutputCallback, (__bridge void *)(self), &_compressionSession) != noErr) {
        NSLog(@"Failed to create compression session!");
        return;
    }

    // 更详细的编码器配置
    VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(1000000)); // 比特率
    VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue); // 实时编码
    VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Main_AutoLevel); // 编码等级
    VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, (__bridge CFTypeRef)@(30)); // 关键帧间隔

    VTCompressionSessionPrepareToEncodeFrames(_compressionSession);
}

static void compressionOutputCallback(void *outputCallbackRefCon,
                                       void *sourceFrameRefCon,
                                       OSStatus status,
                                       VTEncodeInfoFlags infoFlags,
                                       CMSampleBufferRef sampleBuffer) {
    if (!sampleBuffer) return;
    if (status != noErr) {
        NSLog(@"Compression failed with status: %d", status);
        return;
    }

    VideoEncoder *encoder = (__bridge VideoEncoder *)outputCallbackRefCon;

    // 判断是否为关键帧
    BOOL isKeyFrame = !CFDictionaryContainsKey(CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0), kCMSampleAttachmentKey_NotSync);

    // 提取数据
    NSData *data = [encoder dataFromSampleBuffer:sampleBuffer];
    
    [encoder.delegate videoEncoderDidEncodeData:data isKeyFrame:isKeyFrame];
}

- (NSData *)dataFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
    size_t length;
    char *dataPointer;
    CMBlockBufferGetDataPointer(blockBuffer, 0, NULL, &length, &dataPointer);
    
    return [NSData dataWithBytes:dataPointer length:length];
}

- (void)encodeSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    VTCompressionSessionEncodeFrame(_compressionSession, sampleBuffer, kCMTimeInvalid, kCMTimeInvalid, NULL, NULL, NULL);
}

- (void)dealloc {
    if (_compressionSession != NULL) {
        VTCompressionSessionInvalidate(_compressionSession);
        CFRelease(_compressionSession);
        _compressionSession = NULL;
    }
}

@end

使用方式

以下是如何使用VideoEncoder类的示例:

// 定义一个属性以保持对编码器的引用
@property (strong, nonatomic) VideoEncoder *videoEncoder;

// 初始化编码器
self.videoEncoder = [[VideoEncoder alloc] initWithWidth:1920 height:1080];
self.videoEncoder.delegate = self;

// 在获得视频数据的地点调用编码方法(例如,AVCaptureVideoDataOutputSampleBufferDelegate的回调中)
[self.videoEncoder encodeSampleBuffer:sampleBuffer];

// 实现 VideoEncoderDelegate 的回调方法
- (void)videoEncoderDidEncodeData:(NSData *)data isKeyFrame:(BOOL)isKeyFrame {
    // 在这里处理或者存储编码后的数据
}

注意事项

  1. 位率(Bitrate):这个例子中设置的位率是1Mbps,这个值可以根据视频的质量要求和网络条件进行调整。

  2. 实时性能:实时属性告诉编码器尽可能快地进行编码,可能以牺牲一些编码质量为代价。

  3. 关键帧间隔(Key Frame Interval):设置较低的关键帧间隔可以提高视频在网络传输中的恢复能力,但可能增加数据量。

  4. 处理编码后的数据:在实际应用中,编码后的数据经常用于存储或网络传输,这可能需要对数据进行封装(例如,添加SPS/PPS头部信息用于H.264)。

  5. 性能和内存管理:编码过程尤其是在高分辨率下,对性能和内存有较高的要求。必须确保及时释放不再使用的对象和资源,以避免内存泄漏。

posted @ 2024-07-23 15:54  Mr.陳  阅读(2)  评论(0编辑  收藏  举报