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

相关内容

热门资讯

透视玄学!pokemomo辅助... 透视玄学!pokemomo辅助软件(透视)开心十三张透视(总是有辅助平台)-哔哩哔哩;1、开心十三张...
透视app!wepoker俱乐... 您好,透视辅助器免费欢聚水鱼这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
此事备受玩家关注!wpk私人局... 此事备受玩家关注!wpk私人局辅助是真的吗(透视)来来拼十辅助(好像是真的辅助插件)-哔哩哔哩1、点...
透视黑科技!wepoker辅助... 透视黑科技!wepoker辅助器软件下载(透视)新九天作必弊系统(本来存在有辅助下载)-哔哩哔哩1、...
出乎意料的是!hhpoker脚... 出乎意料的是!hhpoker脚本(透视)多乐游戏小程序辅助(确实真的是有辅助插件)-哔哩哔哩1、多乐...
透视真的!wepoker能不能... 透视真的!wepoker能不能透视(透视)川南休闲辅助(竟然有辅助平台)-哔哩哔哩一、川南休闲辅助游...
透视透视!大菠萝辅助器(透视)... 透视透视!大菠萝辅助器(透视)杭州都莱到底有没有挂(其实真的是有辅助平台)-哔哩哔哩1.杭州都莱到底...
一直以来!淘宝买wepoker... 一直以来!淘宝买wepoker透视有用吗(透视)创思维激k透视在哪里买(一贯真的是有辅助辅助器)-哔...
据文件显示!wepoker脚本... 据文件显示!wepoker脚本(透视)微信牵手跑有没有挂(好像真的有辅助app)-哔哩哔哩1、许多玩...
透视玄学!wepoker有没有... 透视玄学!wepoker有没有辅助(透视)瑞安玉海楼茶苑辅助器(竟然真的是有辅助app)-哔哩哔哩;...