我拍了一张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* myDocumentPath = [self getDocumentDirectoryPathWithFileName:convertedVideoUUID]; NSURL *outputFileURL = [[NSURL alloc] initFileURLWithPath:myDocumentPath]; //Check if the file already exists then remove the prevIoUs file [self removeFileIfExistAtPAth:myDocumentPath]; 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:myDocumentPath]; 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]; }