gpt4 book ai didi

ios - AVAsset在iOS中使用图像叠加功能保存视频会破坏视频

转载 作者:行者123 更新时间:2023-12-01 16:29:04 24 4
gpt4 key购买 nike

我正在尝试将图像叠加层和视频合并到新的视频文件中。这是我正在使用的代码:

- (void)mergeVideoUrl:(NSURL *)videoURL andOverlayImage:(UIImage *)overlayImage
{
NSLog(@"%s", __PRETTY_FUNCTION__);

// Create an AVURLAsset
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:clipVideoTrack
atTime:kCMTimeZero
error:nil];

[compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

CGSize videoSize = [clipVideoTrack naturalSize];

// Get our image layer
CALayer *aLayer = [CALayer layer];
aLayer.contents = (id)overlayImage.CGImage;
aLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
aLayer.opacity = 1.0;

// Sort the layer order
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:aLayer];

// Create the composition and add the instructions to insert the layer
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, 30);
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

// Instruction
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];

// Export
AVAssetExportSession *assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
assetExport.videoComposition = videoComp;

NSString* videoName = @"mynewwatermarkedvideo.mov";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];

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

assetExport.outputFileType = AVFileTypeQuickTimeMovie;
assetExport.outputURL = exportUrl;
assetExport.shouldOptimizeForNetworkUse = YES;

[assetExport exportAsynchronouslyWithCompletionHandler:^(void)
{
//FINALIZATION CODE HERE
NSLog(@"DONE: %@", exportUrl);
}];
}

问题在于它使视频失真。知道为什么吗?
我感觉视频帧的大小可能与它或宽高比有关,就好像我设置了 (2000,2000)的大小一样,似乎可以正常工作,但有很多黑边。

enter image description here

编辑 - - -
似乎如果我将 CGSize videoSize设置为2/3的比率,例如 320x4801280x1920可以正常工作,但是生成的视频在右侧下方有一个黑条。为什么 [clipVideoTrack naturalSize]报告 1080x1920的大小,但是除非我使用 1280x1920否则它会搞砸?我认为这可能是转换/裁剪问题?

编辑2 -----
AVAssetExportSessionAVAssetExportPresetLowQualityAVAssetExportPresetMediumQuality之间更改 AVAssetExportPresetHighestQuality预设名称会产生不同大小的视频。所以我认为这以某种方式覆盖了输出视频帧的大小。

最佳答案

CGSize videoSize = [clipVideoTrack naturalSize];

这为您提供了录音的尺寸,但是您需要查看音轨的 prefferedTransform来找到录音的方向。您可能会发现视频宽度实际上是 naturalSize“高度”,具体取决于录制过程中设备的握持方式。

我看到您在合成中设置了它,但是您需要检查它以便为 CALayers分配正确的帧

关于ios - AVAsset在iOS中使用图像叠加功能保存视频会破坏视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33171302/

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