AVCaptureVideoDataOutput消耗的内存比AVCaptureMovieFileOutput多三倍。
创始人
2024-11-12 22:30:19
0

如果想要减少 AVCaptureVideoDataOutput 消耗的内存,可以使用 AVCaptureMovieFileOutput 来替代。代码示例如下:

AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
if (videoInput) {
    [captureSession addInput:videoInput];
}

AVCaptureMovieFileOutput *movieOutput = [[AVCaptureMovieFileOutput alloc] init];
if ([captureSession canAddOutput:movieOutput]) {
    [captureSession addOutput:movieOutput];
}

[captureSession startRunning];

NSURL *outputURL = //设置输出文件路径

NSString *audio = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"m4a"];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:audio] options:nil];

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeAudio][0];

NSError *error = nil;
BOOL ok = [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                ofTrack:assetTrack
                                 atTime:kCMTimeZero
                                  error:&error];
if (!ok) {
    // Handle the error.
}

AVMutableVideoCompositionInstruction *instruction = [[AVMutableVideoCompositionInstruction alloc] init];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);

AVMutableVideoCompositionLayerInstruction *layerInstruction = [[AVMutableVideoCompositionLayerInstruction alloc] init];
[layerInstruction setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];

instruction.layerInstructions = @[layerInstruction];

AVMutableVideoComposition *videoComposition = [[AVMutableVideoComposition alloc] init];
videoComposition.instructions = @[instruction];
videoComposition.frameDuration = CMTimeMake(1, 30);
videoComposition.renderSize = CGSizeMake(1280, 720);

NSError *exportError = nil;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPreset1280x720];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.videoComposition = videoComposition;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    if (exportSession.status == AVAssetExportSession

相关内容

热门资讯

一分钟教会你!仟众部落辅助,微... 一分钟教会你!仟众部落辅助,微乐自建房辅助,详细脚本(竟然有挂)1、微乐自建房辅助有没有辅助教程、微...
记者揭秘!陕西三代攻略,雀神广... 记者揭秘!陕西三代攻略,雀神广东插件智能解码,详细技巧(有挂教学)1、下载好雀神广东插件智能解码脚本...
一分钟了解!多乐手游辅助软件,... 一分钟了解!多乐手游辅助软件,广东闲来app辅助,详细攻略(有挂细节)1、多乐手游辅助软件有没有辅助...
实测必看!微信小程序功夫川辅助... 实测必看!微信小程序功夫川辅助,吉林心悦游戏辅助,详细技巧(有挂辅助)运微信小程序功夫川辅助辅助工具...
六分钟了解!陕西三代辅助器免费... 六分钟了解!陕西三代辅助器免费,广西八一字牌辅助,详细挂(新版有挂)1、陕西三代辅助器免费破解器简单...
科技介绍!创思维激k软件助手,... 科技介绍!创思维激k软件助手,广西微乐小程序控制器,详细方法(有挂规律)1、游戏颠覆性的策略玩法,独...
揭秘真相!杭州都莱大菠萝辅助器... 揭秘真相!杭州都莱大菠萝辅助器,鸿狐大厅脚本,详细软件(有挂解密)1、任何杭州都莱大菠萝辅助器透视是...
记者发布!微友圈app辅助工具... 记者发布!微友圈app辅助工具,微信小程序加速脚本,详细工具(真的有挂)1、全新机制【微信小程序加速...
9分钟了解!天天贵阳游戏辅助,... 9分钟了解!天天贵阳游戏辅助,蛮王大厅脚本辅助,详细挂(有挂功能)1、进入到天天贵阳游戏辅助是否有挂...
最新研发!常州茶苑辅助器下载,... 最新研发!常州茶苑辅助器下载,手游辅助平台,详细软件(果真有挂)1、很好的工具软件,可以解锁游戏的常...