2014-03-13 4 views
0

안녕하세요. 간단하게 비디오 파일을 자르고 AVAssetExportSession 디렉터리로 내 보냅니다. 하지만 문제는 세로 방향의 비디오를 가로 방향으로 변경하고 저장 한 디렉토리에서 90도 회전 된 것처럼 보입니다. 고정되는 방식을 이해하지 못합니다. 내 코드에 문제가 있습니까? 이해하길 바랍니다.비디오 파일 자르기에서 방향 문제가 발생했습니다.

firstAsset = [AVAsset assetWithURL:firstUrl]; 

if(firstAsset !=nil) 
{ 

AVVideoComposition *origionalComposition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:firstAsset]; 
    AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init]; 

    //VIDEO TRACK 

    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [firstTrack insertTimeRange:timeRange ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

AVMutableVideoCompositionInstruction MainInstruction * = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; MainInstruction.timeRange = timeRange; 당신은 AVMutableVideoCompositionLayerInstruction을 추가하지 않은

답변

0

사전에

AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack]; 
    AVAssetTrack *FirstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 


    [FirstlayerInstruction setTransform:FirstAssetTrack.preferredTransform atTime:kCMTimeZero]; 

    MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil]; 

    AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition]; 
    MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction]; 
    MainCompositionInst.frameDuration = origionalComposition.frameDuration; 
    MainCompositionInst.renderScale = 1.0; 
    MainCompositionInst.renderSize = CGSizeMake(FirstAssetTrack.naturalSize.height, FirstAssetTrack.naturalSize.width); 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Crop Videos"]; 

    NSString *videoName =[NSString stringWithFormat:@"Video_%d.mov",videoNumber]; 
    NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:videoName]; 


    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality]; 
    exporter.outputURL=url; 
    exporter.outputFileType = AVFileTypeQuickTimeMovie; 
    exporter.shouldOptimizeForNetworkUse = YES; 
    [exporter exportAsynchronouslyWithCompletionHandler:^ 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self exportDidFinish:exporter]; 
     }); 
    }]; 
} 

누군가가 알고 있다면 답변 주시기 바랍니다 .. 감사합니다, 당신은

// 1.0 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation. 

AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

BOOL isVideoAssetPortrait_ = NO; 
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform; 
if (videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0) { 
    isVideoAssetPortrait_ = YES; 
} 
if (videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0) { 

    isVideoAssetPortrait_ = YES; 
} 
if (videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0) { 
    isVideoAssetPortrait_ = NO; 
} 
if (videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0) { 
    isVideoAssetPortrait_ = NO; 
} 
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero]; 
[videolayerInstruction setOpacity:0.0 atTime:self.videoAsset.duration]; 

// 1.2 - Add instructions 
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil]; 

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition]; 

CGSize naturalSize; 
if(isVideoAssetPortrait_){ 
    naturalSize = CGSizeMake(videoAssetTrack.naturalSize.height, videoAssetTrack.naturalSize.width); 
} else { 
    naturalSize = videoAssetTrack.naturalSize; 
} 

mainCompositionInst.renderSize = naturalSize; 
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction]; 
mainCompositionInst.frameDuration = CMTimeMake(1, 30); 
+0

당신을 감사합니다,하지만 난 그것을 작동하지 않습니다 죄송하고 내가 여러 번 시도했습니다. 다른 어떤 방법이 있습니까 ?????? –

+0

당신이 한 것은 귀하의 코드가 귀하가 사용한 것을 반영하지 않았기 때문입니다. 작업 한 코드를 업데이트하십시오. – Vizllx

+0

내가 볼 수있는 코드가 업데이트되었습니다. –

0

따라이 실행되는 코드를 변경해야합니다 :

AVURLAsset* videoAsset = [AVURLAsset assetWithURL:"PATH OF VIDEO"]; 
//create mutable composition 
AVMutableComposition *mixComposition = [AVMutableComposition composition]; 

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

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
                 atTime:kCMTimeZero 
                  error:&videoInsertError]; 



AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 

MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration); 

//FIXING ORIENTATION// 
AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; 

AVAssetTrack *FirstAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

UIImageOrientation FirstAssetOrientation_ = UIImageOrientationUp; 

BOOL isFirstAssetPortrait_ = NO; 

CGAffineTransform firstTransform = FirstAssetTrack.preferredTransform; 

if(firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0) 
{ 
    FirstAssetOrientation_= UIImageOrientationRight; 
    isFirstAssetPortrait_ = YES; 
} 
if(firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) 
{ 
    FirstAssetOrientation_ = UIImageOrientationLeft; 
    isFirstAssetPortrait_ = YES; 
} 
if(firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) 
{ 
    FirstAssetOrientation_ = UIImageOrientationUp; 
} 
if(firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) 
{ 
    FirstAssetOrientation_ = UIImageOrientationDown; 
} 

CGFloat FirstAssetScaleToFitRatio = 1.0; 

if(isFirstAssetPortrait_) 
{ 
    FirstAssetScaleToFitRatio = FirstAssetTrack.naturalSize.width/FirstAssetTrack.naturalSize.height; 
    CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio); 
    [FirstlayerInstruction setTransform:CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor) atTime:kCMTimeZero]; 
} 
else 
{ 
    CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio); 
    [FirstlayerInstruction setTransform:CGAffineTransformConcat(CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor),CGAffineTransformMakeTranslation(0, 0)) atTime:kCMTimeZero]; 
} 
[FirstlayerInstruction setOpacity:0.0 atTime:videoAsset.duration]; 

MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil]; 

AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition]; 
MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction]; 
MainCompositionInst.frameDuration = CMTimeMake(1, 30); 
//MainCompositionInst.renderSize = CGSizeMake(FirstAssetTrack.naturalSize.width, FirstAssetTrack.naturalSize.height); 
MainCompositionInst.renderSize = [[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]; 

그리고 마지막으로 save 또는 AVAssetExportSession 클래스 '수출'

export.outputURL=FinalPath; 
관련 문제