2017-07-16 115 views
7

出口VideoAsset後:AVMutableComposition - 導出錯誤的視頻轉換

的問題:

  1. 視頻取向不是原來的變換
  2. 導出視頻的層似乎總是景觀

努力:

  1. 變換視頻層取向 - 旋轉至原來的方向
  2. 視頻層尺寸 - 使其全屏大小(按原來的方向)
一些說明:
  • videoAsset的CGRect與開頭相反。

after export, video transform is wrong出口後,視頻轉換是錯誤的

tried to rotate with no success for full size layer嘗試沒有成功的全尺寸層

  AVURLAsset*videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil]; 

      AVMutableComposition* mixComposition = [AVMixerBase compositionVideoTrackAssetUrl:videoAsset]; 

      AVMutableVideoComposition *videoComposition=[AVMutableVideoComposition videoComposition]; 
      videoComposition.frameDuration=CMTimeMake(1, 30); //frames per seconds 
      videoComposition.renderSize = videoAsset.naturalSize; 
      //videoComposition.renderScale = 1.0; 

      videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; 


    AVMutableCompositionTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; //was AVAssetTrack *videoTrack 
    AVMutableVideoCompositionLayerInstruction *layerInstruction = [self layerInstructionAfterFixingOrientationForAsset:videoAsset 
                               forTrack:videoTrack atTime:videoAsset.duration]; 



AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 
    [layerInstruction setTransform:videoTrack.preferredTransform atTime:kCMTimeZero]; 
    [layerInstruction setOpacity:0.0 atTime:videoAsset.duration]; 




      AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
       instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]); 
       instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; 
       videoComposition.instructions = [NSArray arrayWithObject:instruction]; 

旋轉設置軌道

  +(AVMutableComposition *)compositionVideoTrackAssetUrl:(AVURLAsset*)assetUrl{ 

       AVMutableComposition* mixComposition = [AVMutableComposition composition]; 
       AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
       AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 


       AVAssetTrack *clipVideoTrack = [[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
       AVAssetTrack *clipAudioTrack = [[assetUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
       [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil]; 
       [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 
       [compositionVideoTrack setPreferredTransform:[[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 
       return mixComposition; 
      } 


so after researching all stack overflow discussions about export rotation, 
I'm still having video rotation to size issue... 
+0

你解決呢? –

+0

似乎stackoverflow上的解決方案已過時。 –

+0

@RoiMulia,請看我的回答 –

回答

0

OK,我的回答代碼沒有組織,但會做這項工作:

注意:在使用 「導出會話」 你,設置呈現大小爲代碼:

[AVMixerBase共享] .renderSize

1。

AVURLAsset*videoAsset = [[AVURLAsset alloc]initWithURL:url options:nil]; 
AVAssetTrack*videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject]; 
[[AVMixerBase shared]setVideoAsset:videoAsset]; 

AVMutableComposition *mixComposition = [AVMixerBase compositionVideoTrackAssetUrl:videoAsset]; /* audio + video */ 
AVMutableCompositionTrack *compositionVideoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] firstObject]; 
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; 
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; 

CALayer *videoLayer = [self videoLayerAssetUrl:videoAsset]; 
CALayer *parentLayer = [CALayer layer]; 
CGSize videoSize = [videoAssetTrack naturalSize]; 
_segments = file.prefferedTranslation.customSegments; 
NSLog(@"_segments %@",_segments); 


//set render sizes: 
//3.0_______COMBINE ALL 
UIImageOrientation orientation = [self videoOrientation:videoAsset]; 
switch (orientation) { 
    case UIImageOrientationUp:{ NSLog(@"Up"); //camera - lansdscape right 
     [AVMixerBase shared].renderSize = CGSizeMake(videoSize.width, videoSize.height); 
     break; 
    } 
    case UIImageOrientationDown:{ NSLog(@"Down"); //camera = lansdscape left 
     [AVMixerBase shared].renderSize = CGSizeMake(videoSize.width, videoSize.height); 
     videoLayer.affineTransform = CGAffineTransformMakeRotation(M_PI); 
     break; 
    } 
    case UIImageOrientationLeft:{ NSLog(@"Left"); 

     break; 
    } 
    case UIImageOrientationRight:{ NSLog(@"Right"); 
     //camera = up 
     CGAffineTransform t1 = CGAffineTransformMakeTranslation(videoSize.height, 0); 
     CGAffineTransform t2 = CGAffineTransformRotate(t1, DEGREES_TO_RADIANS(90.0)); 
     [layerInstruction setTransform:t2 atTime:kCMTimeZero]; 
     [AVMixerBase shared].renderSize = CGSizeMake(videoSize.height, videoSize.width); 
     break; 
    } 

    default: 
     break; 
} 

+(AVMutableComposition )compositionVideoTrackAssetUrl:(AVURLAsset)assetUrl { AVMutableComposition * mixComposition = [AVMutableComposition組合物]; mixComposition = [AVMixerBase addVideoTrackAssetUrl:assetUrl mixComposition:mixComposition]; return mixComposition; }

+(AVMutableComposition )addVideoTrackAssetUrl:(AVURLAsset)assetUrl mixComposition:(AVMutableComposition *)mixComposition {

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

AVAssetTrack *clipVideoTrack = [[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
AVAssetTrack *clipAudioTrack = [[assetUrl tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil]; 
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetUrl.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil]; 
[compositionVideoTrack setPreferredTransform:[[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 

return mixComposition; 

}

- (CALayer的*)videoLayerAssetUrl:(AVURLAsset *)assetUrl CGSize sizeOfVideo = [[[assetUrl tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]; CALayer * videoLayer = [CALayer圖層]; videoLayer.frame = CGRectMake(0,0,sizeOfVideo.width,sizeOfVideo.height); // videoLayer.frame = CGRectMake(0,0,sizeOfVideo.height,sizeOfVideo.width); // [videoLayer setAffineTransform:assetUrl.preferredTransform];

return videoLayer; 

}