2016-09-28 2 views
1

2 개의 비디오를 iOS 9의 AVAssetExportSession과 블렌딩 할 때 완벽하게 작동합니다. 하지만 iOS 10의 AVAssetExportSession과 결합하면 작동하지 않습니다. 이유를 알면 도와주세요, 고마워요.iOS 10의 AVAssetExportSession이 iPhone 7에서 작동하지 않습니다.

actualy 코드는 예를 들어 아이폰 7

에 대한 작업을위한 아이폰 기가 근무 및 이전 것이 아니라

-(void) blendVideoOverVideo:(NSURL*)mainVideoUrl andBlendVideoUrl:(NSURL*)liveEffectUrl 
{ 
    AVURLAsset *mainVideoUrlAsset =[AVURLAsset URLAssetWithURL:mainVideoUrl options:nil]; 
    // AVPlayerItem* mainVideoPlayerItem =[[AVPlayerItem alloc]initWithAsset:mainVideoUrlAsset]; 
    AVAssetTrack* mainVideoTrack =[[mainVideoUrlAsset tracksWithMediaType:AVMediaTypeVideo]firstObject]; 
    CGSize mainVideoSize = [mainVideoTrack naturalSize]; 

    AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init]; 

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:mainVideoUrl options:nil]; 
    if(mainVideoUrl!=nil) 
    { 
     if([[audioAsset tracksWithMediaType:AVMediaTypeAudio] count]) 
     { 
      AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                           preferredTrackID:kCMPersistentTrackID_Invalid]; 
      [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, mainVideoUrlAsset.duration) 
               ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
               atTime:kCMTimeZero 
                error:nil]; 
     } 
    } 

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

    [mainVideoConpositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, mainVideoUrlAsset.duration) ofTrack:mainVideoTrack atTime:kCMTimeZero error:nil]; 

    AVMutableVideoCompositionLayerInstruction *mainVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mainVideoConpositionTrack]; 

    //SEcond Track 
    AVURLAsset *blendVideoUrlAsset =[AVURLAsset URLAssetWithURL:liveEffectUrl options:nil]; 
    // AVPlayerItem* blendVideoPlayerItem =[[AVPlayerItem alloc]initWithAsset:blendVideoUrlAsset]; 
    AVAssetTrack* blendVideoTrack =[[blendVideoUrlAsset tracksWithMediaType:AVMediaTypeVideo]firstObject]; 
    CGSize blendVideoSize = [blendVideoTrack naturalSize]; 

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

    CMTime oldTime=CMTimeMakeWithSeconds(CMTimeGetSeconds(blendVideoUrlAsset.duration), blendVideoUrlAsset.duration.timescale); 

// CMTime timeNew=CMTimeMakeWithSeconds(CMTimeGetSeconds(blendVideoUrlAsset.duration)/2, blendVideoUrlAsset.duration.timescale); 


    [blendVideoConpositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, oldTime) ofTrack:blendVideoTrack atTime:kCMTimeZero error:nil]; 

    AVMutableVideoCompositionLayerInstruction *blendVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:blendVideoConpositionTrack]; 

    AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
    MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, mainVideoUrlAsset.duration); 

    CGAffineTransform Scale = CGAffineTransformMakeScale(1.0f,1.0f); 
    CGAffineTransform Move = CGAffineTransformMakeTranslation(0,0); 
    [mainVideoLayerInstruction setTransform:CGAffineTransformConcat(Scale,Move) atTime:kCMTimeZero]; 

    [blendVideoLayerInstruction setOpacity:0.5 atTime:kCMTimeZero]; 
// [blendVideoLayerInstruction setOpacity:0.0 atTime:timeNew]; 

    CGFloat cropOffX = 1.0; 
    CGFloat cropOffY = 1.0; 
    if(blendVideoSize.height>mainVideoSize.height) 
    { 
     cropOffY = mainVideoSize.height/blendVideoSize.height; 
    }else{ 

     cropOffY = mainVideoSize.height/blendVideoSize.height; 

    } 
    if(blendVideoSize.width>mainVideoSize.width) 
    { 
     cropOffX = mainVideoSize.width/blendVideoSize.width; 
    } 
    Scale = CGAffineTransformMakeScale(cropOffX,cropOffY); 
    Move = CGAffineTransformMakeTranslation(0.1, 0.1); 
    [blendVideoLayerInstruction setTransform:CGAffineTransformConcat(Scale,Move) atTime:kCMTimeZero]; 

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

    AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition]; 
    MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction]; 
    MainCompositionInst.frameDuration = CMTimeMake(1, 30); 
    MainCompositionInst.renderSize = mainVideoSize; 


    NSString *fullName= [NSString stringWithFormat:@"video%d.mov",arc4random() % 1000]; 



    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:fullName]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:myPathDocs]) 
    { 
     [[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil]; 
    } 
    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality]; 
    exporter.outputURL=url; 

    CMTime start; 
    CMTime duration; 

    NSLog(@"Main Video dura %f blend dura - %f, ",CMTimeGetSeconds(mainVideoUrlAsset.duration),CMTimeGetSeconds(blendVideoUrlAsset.duration)); 


    if(CMTimeGetSeconds(blendVideoUrlAsset.duration)>CMTimeGetSeconds(mainVideoUrlAsset.duration)) 
    { 
     start = CMTimeMakeWithSeconds(0.0, blendVideoUrlAsset.duration.timescale); 
     duration = CMTimeMakeWithSeconds(CMTimeGetSeconds(mainVideoUrlAsset.duration), blendVideoUrlAsset.duration.timescale); 
    } 
    else if(CMTimeGetSeconds(mainVideoUrlAsset.duration)>CMTimeGetSeconds(blendVideoUrlAsset.duration)) 
    { 
     start = CMTimeMakeWithSeconds(0.0, mainVideoUrlAsset.duration.timescale); 
     duration = CMTimeMakeWithSeconds(CMTimeGetSeconds(mainVideoUrlAsset.duration), mainVideoUrlAsset.duration.timescale); 
    } 
    CMTimeRange range = CMTimeRangeMake(start, duration); 

    exporter.timeRange = range; 
    [exporter setVideoComposition:MainCompositionInst]; 
    exporter.outputFileType = AVFileTypeQuickTimeMovie; 

    __weak typeof(self) weakSelf = self; 

    [weakSelf createMBCircularProgress:exporter]; 


    [exporter exportAsynchronouslyWithCompletionHandler:^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [weakSelf exportDidFinish:exporter]; 
     }); 
    }]; 
} 

IOS 9, 심지어 아이폰 6S의 아이폰 OS 10, 6에서 실행됩니다 코드 , 5 등하지만이 코드는 iPhone 7 시뮬레이터에서 실행되지 않습니다.

이 솔루션은 우리가이

+1

코드를 공유하십시오! – voromax

+0

간단한 코드가 iOS를 통해 작성되지 않습니다. –

+0

실제 코드는 iPhone 7에서 작동하지만 iPhone 7에서는 작동하지 않습니다. –

답변

2
It's a bug. 

It's fixed in Xcode 8.1 beta. 

엑스 코드 8.1 베타 [AVAssetExportSession allExportPresets이] 아이폰 7 시뮬레이터 지금 반환

AVAssetExportPreset1920x1080, 
AVAssetExportPresetLowQuality, 
AVAssetExportPresetAppleM4A, 
AVAssetExportPreset640x480, 
AVAssetExportPreset3840x2160, 
AVAssetExportPresetHighestQuality, 
AVAssetExportPreset1280x720, 
AVAssetExportPresetMediumQuality, 
AVAssetExportPreset960x540 

엑스 코드 8.0 [AVAssetExportSession allExportPresets]를 실행하기위한 최신 엑스 코드 8.1 베타를 사용할 필요가있다 iPhone 7 시뮬레이터에서 빈 배열을 반환합니다.

+1

답변을 찾은 것을 확인해 주셔서 감사합니다. 모두 감사드립니다. 그러나 당신이 물어 본 것은 아직도 불분명하다. 이 Q & A가 다른 사람을 도울 수 있도록 질문을 업데이트하십시오. 질문 내에 코드와 오류 메시지를 게시하십시오. – pedrouan

+0

질문은 간단합니다. AVAssetExportSession이 iPhone에서 작동하지 않습니다. –

+0

대답은 간단합니다. Xcode 베타 8.1에서 실행됩니다. –