2017-07-16 59 views
7

Po wyeksportowaniu VideoAsset:AVMutableComposition - Eksport źle wideo przekształcić

kwestie:

  1. orientacja wideo jest nie oryginalne przekształcić
  2. Exported warstwie wideo wydaje się być zawsze krajobraz.

próby:

  1. przekształcić orientacji warstw wideo - obrócić do orientacji oryginału
  2. rozmiar warstwa wideo - sprawiają, że pełny rozmiar ekranu (o orientacji oryginału)

niektóre uwagi:

  • CGRect videoAsset jest przeciwny od początku.

after export, video transform is wrong po eksporcie, video przekształcić jest źle

tried to rotate with no success for full size layer starał się obrócić bez powodzenia dla pełnego rozmiaru warstwy

  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]; 

ustawienie utworowi

  +(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

Czy go rozwiązać? –

+0

Wygląda na to, że rozwiązanie na stackoverflow jest nieaktualne. –

+0

@RoiMulia, proszę zobaczyć moją odpowiedź –

Odpowiedz

0

OK, moja odpowiedź kod nie jest zorganizowany, ale to zadziała:

UWAGA: jeśli używasz "Sesja na eksport", należy ustawić renderowanie wielkości jak kod:

[AVMixerBase wspólne] .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 skład]; 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]; videoLayer CALayer * = [warstwę 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; 

}