ios – 尝试使用AVFoundation为视频上的图像添加水印

ios – 尝试使用AVFoundation为视频上的图像添加水印,第1张

概述我拍了一张png图像和一个用于水印的视频,两者都是肖像.我已经把它作为水印成像了一个视频图像. 水印后,在横向模式下获得合并视频,并在反时钟方向上翻转90度. 我无法找到视频从纵向模式转换为横向模式的确切原因.而图像显示拉伸肖像. 请帮忙.提前致谢. 使用下面的代码: – - (void)addWatermarkAtVideoFile:(NSURL *)videoURL image:(UIImag 我拍了一张png图像和一个用于水印的视频,两者都是肖像.我已经把它作为水印成像了一个视频图像.

水印后,在横向模式下获得合并视频,并在反时钟方向上翻转90度.
我无法找到视频从纵向模式转换为横向模式的确切原因.而图像显示拉伸肖像.
请帮忙.提前致谢.

使用下面的代码: –

- (voID)adDWatermarkAtVIDeofile:(NSURL *)vIDeoURL image:(UIImage *)image withConvertedVIDeoUUID:(Nsstring *)convertedVIDeoUUID response:(voID(^)(BOol success,Nsstring *vIDeoUUID,NSURL *vIDeoPath))responseBlock {    AVURLAsset* vIDeoAsset = [[AVURLAsset alloc]initWithURL:vIDeoURL options:nil];    AVMutableComposition* mixComposition = [AVMutableComposition composition];    AVAssetTrack *clipVIDeoTrack = [[vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0];    if(clipVIDeoTrack) {        AVMutableCompositionTrack *compositionVIDeoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];        [compositionVIDeoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,vIDeoAsset.duration) ofTrack:clipVIDeoTrack atTime:kCMTimeZero error:nil];        [compositionVIDeoTrack setPreferredtransform:[[[vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0] preferredtransform]];    }    AVAssetTrack *clipAudioTrack = [[vIDeoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];    if(clipAudioTrack) {        AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID];        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,vIDeoAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];    }    CGSize sizeOfVIDeo=[vIDeoAsset naturalSize];    //Image of watermark    UIImage *myImage = image;    CALayer *layerCa = [CALayer layer];    layerCa.contents = (ID)myImage.CGImage;    layerCa.frame = CGRectMake(0,sizeOfVIDeo.wIDth,sizeOfVIDeo.height);    layerCa.opacity = 1.0;    CALayer *parentLayer=[CALayer layer];    CALayer *vIDeolayer=[CALayer layer];    parentLayer.frame=CGRectMake(0,sizeOfVIDeo.height);    vIDeolayer.frame=CGRectMake(0,sizeOfVIDeo.height);    [parentLayer addSublayer:vIDeolayer];    [parentLayer addSublayer:layerCa];    AVMutableVIDeoComposition *vIDeoComposition=[AVMutableVIDeoComposition vIDeoComposition] ;    vIDeoComposition.frameDuration=CMTimeMake(1,30);    vIDeoComposition.renderSize=sizeOfVIDeo;    vIDeoComposition.animationTool=[AVVIDeoCompositionCoreAnimationTool vIDeoCompositionCoreAnimationToolWithPostProcessingAsVIDeolayer:vIDeolayer inLayer:parentLayer];    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];    vIDeoComposition.instructions = [NSArray arrayWithObject: instruction];    //Creating temp path to save the converted vIDeo    Nsstring* my@R_403_4617@Path = [self get@R_403_4617@DirectoryPathWithfilename:convertedVIDeoUUID];    NSURL *outputfileURL = [[NSURL alloc] initfileURLWithPath:my@R_403_4617@Path];    //Check if the file already exists then remove the prevIoUs file    [self removefileIfExistAtPAth:my@R_403_4617@Path];    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetname:AVAssetExportPresetHighestQuality];    exportSession.vIDeoComposition=vIDeoComposition;    exportSession.outputURL = outputfileURL;    exportSession.outputfileType = AVfileTypeQuickTimeMovIE;    [exportSession exportAsynchronouslyWithCompletionHandler:^{        switch (exportSession.status)        {            case AVAssetExportSessionStatusCompleted:                NSLog(@"Export OK");                [self saveInPhotoAlbum:my@R_403_4617@Path];                break;            case AVAssetExportSessionStatusFailed:                NSLog (@"AVAssetExportSessionStatusFailed: %@",exportSession.error);                break;            case AVAssetExportSessionStatusCancelled:                NSLog(@"Export Cancelled");                break;        }        BOol statusSuccess = [exportSession status] == AVAssetExportSessionStatusCompleted;        responseBlock(statusSuccess ? YES : NO,statusSuccess ? convertedVIDeoUUID : nil,statusSuccess ? outputfileURL : nil);    }];}
解决方法 我认为这是AVFoundation的默认行为,它可能与水印功能无关.

你可以使用CGAffinetransform:

if(height > wIDth) {    CGAffinetransform rotationtransform = CGAffinetransformMakeRotation(M_PI_2);    [layerInstruction settransform:rotationtransform atTime:kCMTimeZero];}
总结

以上是内存溢出为你收集整理的ios – 尝试使用AVFoundation为视频上的图像添加水印全部内容,希望文章能够帮你解决ios – 尝试使用AVFoundation为视频上的图像添加水印所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存