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

相关内容

热门资讯

第6分钟辅助!wepoker私... 第6分钟辅助!wepoker私人局辅助器,微信小程序开发(普及知识开挂辅助平台);最新版2026是一...
4分钟辅助!poker红龙辅助... 4分钟辅助!poker红龙辅助,手游游戏挂机辅助(重大推荐开挂辅助脚本);手游游戏挂机辅助是一项手游...
第6分钟辅助!九九山城插件程序... 第6分钟辅助!九九山城插件程序,神途免费辅助软件(研究成果开挂辅助安装);亲真的是有正版授权,小编(...
八分钟辅助!越乡游辅助工具,雀... 八分钟辅助!越乡游辅助工具,雀姬辅助脚本(今日公布开挂辅助安装);雀姬辅助脚本是一项雀姬辅助脚本软件...
第6分钟辅助!手机游戏辅助脚本... 第6分钟辅助!手机游戏辅助脚本工具,钱塘十三水其实真的有挂(玩家必看科普开挂辅助软件);1、让任何用...
第4分钟辅助!反杀大厅辅助,福... 第4分钟辅助!反杀大厅辅助,福建天天开心辅助工具(玩家必看开挂辅助插件);福建天天开心辅助工具软件透...
第八分钟辅助!中至赣州黑科技辅... 第八分钟辅助!中至赣州黑科技辅助软件,新道游房卡辅助器(透视攻略开挂辅助下载);是一款可以让一直输的...
第7分钟辅助!闲逸辅助插件,微... 第7分钟辅助!闲逸辅助插件,微信小程序加速脚本(玩家必备科普开挂辅助工具);亲,有的,ai轻松简单,...
第6分钟辅助!樱花之盛辅助软件... 第6分钟辅助!樱花之盛辅助软件,微乐小程序辅助(透视透视开挂辅助软件);小薇(透视辅助)致您一封信;...
第四分钟辅助!wpk透视辅助方... 第四分钟辅助!wpk透视辅助方法,微信新九游辅助(分享给玩家开挂辅助软件);1、让任何用户在无需AI...