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、黑科技讲解!德州之星辅助软件(wpk到底有没有外挂)原先是真的有挂(了解有挂)-哔哩哔哩;该软件...
辅助黑科技!wpk有挂(德扑a... 辅助黑科技!wpk有挂(德扑ai智能机器人)原本真的是有挂(果真有挂)-哔哩哔哩是一款可以让一直输的...
黑科技挂!智星德州菠萝安全(德... 黑科技挂!智星德州菠萝安全(德州ai辅助软件)原本有挂(有挂技巧)-哔哩哔哩是一款可以让一直输的玩家...
黑科技app!哈糖大菠萝切牌规... 黑科技app!哈糖大菠萝切牌规律(微扑克透牌)原来真的有挂(有挂教程)-哔哩哔哩1、快速入门:当你通...
黑科技能赢!cloudpoke... 黑科技能赢!cloudpoker外挂(aapoker真的有猫腻吗)起初是有挂(有挂秘笈)-哔哩哔哩1...
黑科技软件!扑克之城可以那假(... 黑科技软件!扑克之城可以那假(wepokeai代打)从来真的是有挂(有挂助手)-哔哩哔哩1、起透看视...
黑科技有挂!wepoke是有辅... 黑科技有挂!wepoke是有辅助(德州辅助神器软件)往昔真的有挂(有挂总结)-哔哩哔哩是一款可以让一...
黑科技辅助挂!aapoker辅... 黑科技辅助挂!aapoker辅助透视(微扑克如何让系统发好牌)一向是有挂(有挂辅助)-哔哩哔哩1、这...
黑科技中牌率!wpk透视辅助可... 1、黑科技中牌率!wpk透视辅助可测试真的假的(aapoker辅助工具ai)好像有挂(的确有挂)-哔...
黑科技美元局!智星德州安卓版下... 黑科技美元局!智星德州安卓版下载方法(微扑克有挂么)原生真的有挂(有挂教学)-哔哩哔哩,您好,微扑克...