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

相关内容

热门资讯

透视app(WePoKer)w... 透视app(WePoKer)wepoker免费钻石(透视)切实有挂(透明挂教程)1、任何wepoke...
透视科技(WePoKer)we... 透视科技(WePoKer)wepoker透视苹果系统(透视)原来是真的有挂(软件教程);1、用户打开...
透视真的(wepoker)we... 透视真的(wepoker)wepoker有没有插件(透视)都是是真的有挂(安装教程)1、首先打开we...
透视透视(WePoKer)we... 透视透视(WePoKer)wepokerplus作弊(透视)一贯有挂(可靠教程);1、这是跨平台的w...
透视规律(WEPOKER)we... 透视规律(WEPOKER)wepoker黑侠辅助器正版下载(透视)原来真的有挂(透明教程)1、金币登...
透视计算(WePoKer)we... 透视计算(WePoKer)wepoker黑侠辅助器正版下载(透视)本来有挂(黑科技教程);1、许多玩...
透视智能ai(WePoKer)... 透视智能ai(WePoKer)wepoker透视脚本视频(透视)果然存在有挂(2025新版)1、首先...
透视新版(wepoker)we... 透视新版(wepoker)wepoker有没有挂(透视)切实真的有挂(德州教程)一、wepoker有...
透视app(WePoKer)w... 透视app(WePoKer)wepoker挂(透视)都是真的有挂(解说技巧)1、操作简单,无需注册,...
透视规律(wepoker)we... 透视规律(wepoker)wepoker怎么增加运气(透视)确实存在有挂(实用技巧);1、这是跨平台...