在Objective-C中截屏视频,可以通过以下步骤实现:
以下是一个简单的示例代码:
#import <AVFoundation/AVFoundation.h>
@interface ViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoDataOutput *videoOutput;
@property (nonatomic, strong) AVCaptureConnection *videoConnection;
@property (nonatomic, strong) AVAssetWriter *assetWriter;
@property (nonatomic, strong) AVAssetWriterInput *videoWriterInput;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建捕获会话
self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
// 创建视频输入源
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (error) {
NSLog(@"Error: %@", error.localizedDescription);
return;
}
[self.captureSession addInput:videoInput];
// 创建视频输出
self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
self.videoOutput.videoSettings = @{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
self.videoOutput.alwaysDiscardsLateVideoFrames = YES;
[self.captureSession addOutput:self.videoOutput];
// 设置视频输出代理
dispatch_queue_t queue;
queue = dispatch_queue_create("videoOutputQueue", DISPATCH_QUEUE_SERIAL);
[self.videoOutput setSampleBufferDelegate:self queue:queue];
// 创建视频写入器
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"output.mp4"];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];
self.assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeMPEG4 error:&error];
if (error) {
NSLog(@"Error: %@", error.localizedDescription);
return;
}
// 设置视频编码器
NSDictionary *videoCompressionProperties = @{(id)kVTCompressionPropertyKey_ProfileLevel: (id)kVTProfileLevel_H264_High_4_0,
(id)kVTCompressionPropertyKey_AverageBitRate: @(1000000)};
NSDictionary *videoOutputSettings = @{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
(id)kCVPixelBufferWidthKey : @(self.view.bounds.size.width),
(id)kCVPixelBufferHeightKey : @(self.view.bounds.size.height),
(id)kCVPixelBufferBytesPerRowAlignmentKey : @(4),
(id)kCVPixelBufferCGBitmapContextCompatibilityKey : @(YES),
(id)kCVPixelBufferCGImageCompatibilityKey : @(YES),
(id)kCVPixelBufferIOSurfacePropertiesKey : @{}};
self.videoWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoOutputSettings];
self.videoWriterInput.expectsMediaDataInRealTime = YES;
self.videoWriterInput.transform = CGAffineTransformMakeScale(-1, 1);
[self.assetWriter addInput:self.videoWriterInput];
// 开始捕获视频
[self.captureSession startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if (self.videoWriterInput.readyForMoreMediaData) {
if (self.videoConnection == nil) {
self.videoConnection = connection;
}
if (connection == self.videoConnection) {
[self.videoWriterInput appendSampleBuffer:sampleBuffer];
}
}
}
- (void)dealloc {
[self.captureSession stopRunning];
[self.assetWriter finishWritingWithCompletionHandler:^{
NSLog(@"视频已保存");
}];
}
@end
这段代码将捕获屏幕上的视频,并将其写入一个MP4文件中。在视频捕获过程中,可以通过AVCaptureVideoDataOutputSampleBufferDelegate协议的回调方法captureOutput:didOutputSampleBuffer:fromConnection:来处理每一帧视频数据。在视频捕获结束时,可以通过AVAssetWriter的finishWriting方法将视频数据写入文件。
领取专属 10元无门槛券
手把手带您无忧上云