gpt4 book ai didi

ios - AVAssetExportSession,AVMutableVideoComposition 让视频更红

转载 作者:可可西里 更新时间:2023-11-01 05:50:36 24 4
gpt4 key购买 nike

我从 livephoto 中获取视频,并多次导出这样的视频。
它变得比源 livephoto 更红。这是一个错误?
这是导出代码,最后有一个演示项目。

- (void)clipMovie:(NSString *)path block:(void (^)(BOOL success))completionBlock
{
UIColor *bgcolor = (UIColor *)[UIColor blackColor];

AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path] options:nil];

AVMutableComposition *composition = [AVMutableComposition composition];
NSError *error;
[composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoasset.duration) ofAsset:videoasset atTime:kCMTimeZero error:&error];

AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
videoInstruction.backgroundColor = bgcolor.CGColor;


if ([[composition tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
AVAssetTrack *videoTrack = [[composition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoCompositionLayerInstruction *cropLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

// CGSize naturalSize = videoTrack.naturalSize;

CGSize renderSize = CGSizeMake(1080, 1080);

AVAssetTrack *videoTracktmp = [[videoasset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
CGAffineTransform transform = videoTracktmp.preferredTransform;

CGRect drawrect = CGRectMake(0, 0, 1080, 1080);

[cropLayer setTransform:transform atTime:kCMTimeZero];

CALayer *backgroundLayer = [CALayer layer];
// [backgroundLayer setContents:(id)self.patternBgView.image.CGImage];
backgroundLayer.frame = CGRectMake(0, 0, renderSize.width, renderSize.height);
[backgroundLayer setMasksToBounds:YES];
backgroundLayer.backgroundColor = bgcolor.CGColor;

CALayer *videoLayer = [CALayer layer];


videoLayer.frame = CGRectMake(drawrect.origin.x, (renderSize.height-drawrect.size.height-drawrect.origin.y), drawrect.size.width, drawrect.size.height);
CALayer *parentLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, renderSize.width, renderSize.height);
[parentLayer addSublayer:backgroundLayer];
[parentLayer addSublayer:videoLayer];

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
videoInstruction.layerInstructions = @[cropLayer];
videoComposition.instructions = @[videoInstruction];

videoComposition.renderSize = CGSizeMake(renderSize.width, renderSize.height);
videoComposition.frameDuration = CMTimeMake(1, 15);

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoasset presetName:AVAssetExportPresetHighestQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.videoComposition = videoComposition;
// exportSession.metadata = videoasset.metadata;

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] stringByAppendingPathComponent:@"a"];
BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
NSString *result = [path stringByAppendingPathComponent:@"a.mov"];

NSString *exportVideoPath = result;
// NSLog(@"%@",result);

if ([[NSFileManager defaultManager] fileExistsAtPath:exportVideoPath]) {
[[NSFileManager defaultManager] removeItemAtPath:exportVideoPath error:nil];
}

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
// exportSession.metadata = [self updateMetadata:videoasset.metadata];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
switch ((int)exportSession.status) {
case AVAssetExportSessionStatusFailed:{
// NSLog (@"FAIL");
completionBlock(NO);
break;
}
case AVAssetExportSessionStatusCompleted: {
// NSLog (@"SUCCESS");
completionBlock(YES);
break;
}
};
});

}];
}

}

然后视频比源livephoto更红。

这里是 demo link on github

  1. 运行项目
  2. 它会在 1 秒后呈现 imagepicker。
  3. 选择实时照片
  4. 然后点击选择
  5. 然后将新视频保存到您的相册。它变红了!

最佳答案

哎呀!就去做吧!

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoasset presetName:AVAssetExportPresetPassthrough];

This export option will cause the media of all tracks to be passed through to the output exactly as stored in the source asset, except for tracks for which passthrough is not possible, usually because of constraints of the container format as indicated by the specified outputFileType. This option is not included in the arrays returned by -allExportPresets and -exportPresetsCompatibleWithAsset.

关于ios - AVAssetExportSession,AVMutableVideoComposition 让视频更红,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com