2017-06-16 7 views
4

透かしのためのpng画像とビデオを撮影しました。どちらもポートレートです。そして、私はそれをウォーターマークとしてビデオでイメージしました。AVFoundationを使用してビデオの画像に透かしを入れようとしています

ウォーターマーキングの後、横長モードでビデオをマージし、反時計方向に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, 0, sizeOfVideo.width, sizeOfVideo.height); 
    layerCa.opacity = 1.0; 

    CALayer *parentLayer=[CALayer layer]; 
    CALayer *videoLayer=[CALayer layer]; 
    parentLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, sizeOfVideo.height); 
    videoLayer.frame=CGRectMake(0, 0, sizeOfVideo.width, 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); 
    }]; 
} 

答えて

0

私はそれがAVFoundationとデフォルトの動作だと思うが、それはおそらく、透かし機能にリンクされていません。

あなたはCGAffineTransformを使用することができます。AVAsset

if(height > width) { 
    CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2); 
    [layerInstruction setTransform:rotationTransform atTime:kCMTimeZero]; 
} 
0

naturalSizeは、ビデオの最終的な回転を考慮していません。
ツアーウォーターマークを正しく配置する場合は、AVMutableVideoCompositionrenderSizeを使用するか、いくつかの変換を適用することを検討してください。あなたは回転を考慮し、実際のサイズを取得することができ、このいずれかで

func orientationForAsset(_ asset: AVAsset) -> (orientation: UIImageOrientation, isPortrait: Bool) { 
     let videoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).first! 
     let transformMatrix = videoTrack.preferredTransform 
     return orientationFromTransform(transformMatrix) 
    } 

    func orientationFromTransform(_ transform: CGAffineTransform) -> (orientation: UIImageOrientation, isPortrait: Bool) { 
     var assetOrientation = UIImageOrientation.up 
     var isPortrait = false 
     if transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0 { 
      assetOrientation = .right 
      isPortrait = true 
     } else if transform.a == 0 && transform.b == -1.0 && transform.c == 1.0 && transform.d == 0 { 
      assetOrientation = .left 
      isPortrait = true 
     } else if transform.a == 1.0 && transform.b == 0 && transform.c == 0 && transform.d == 1.0 { 
      assetOrientation = .up 
     } else if transform.a == -1.0 && transform.b == 0 && transform.c == 0 && transform.d == -1.0 { 
      assetOrientation = .down 
     } 
     return (assetOrientation, isPortrait) 
    } 

: このスニペットあなたの資産の実際の方向を示します。

func resolutionSizeForAsset(_ asset: AVAsset) -> CGSize? { 
     guard let track = asset.tracks(withMediaType: AVMediaTypeVideo).first else { return nil } 
     let size = track.naturalSize.applying(track.preferredTransform) 
     return CGSize(width: fabs(size.width), height: fabs(size.height)) 
    } 
+0

ありがとうございました。わかった。 –

関連する問題