1

When I am blending two videos with AVAssetExportSession in ios 9 its working perfectly. but when i blend with AVAssetExportSession in iOS 10, it in not working. Please help me if any know the reason, Thank you.

actualy code is working for iphone 6s and earlier, but not for working for iPhone 7

for example

-(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];
        });
    }];
}

this code will run in ios 9 and even iOS 10 in iPhone 6s, 6,5 etc but this code will not run in iPhone 7 simulator.

The solution is we need to use latest XCode 8.1 beta for running this

6
  • 1
    Share the code!
    – voromax
    Commented Sep 28, 2016 at 11:03
  • any simple code not even writing over ios 10 Commented Sep 28, 2016 at 11:06
  • actualy code is working for iphone 6s and earlier, but not for working for iPhone 7 Commented Sep 28, 2016 at 11:07
  • those who downvote should know this bug Commented Sep 28, 2016 at 11:38
  • Share the code where you configure AVAssetExportSession and I will remove it ;-)
    – voromax
    Commented Sep 28, 2016 at 11:50

2 Answers 2

2
It's a bug.

It's fixed in Xcode 8.1 beta.

Xcode 8.1 beta [AVAssetExportSession allExportPresets] iPhone 7 Simulator now returns

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

Xcode 8.0 [AVAssetExportSession allExportPresets] iPhone 7 Simulator returns an empty array

6
  • 1
    Nice to see you found an answer, we all appreciate it. But it is still unclear, what you've asked. Please, update your question so this Q&A could help anybody else. Post a code and the error message within the question.
    – pedrouan
    Commented Sep 28, 2016 at 11:51
  • question is simple, AVAssetExportSession not work in iPhone Commented Sep 28, 2016 at 12:02
  • answer is simple, is will run in XCode beta 8.1 Commented Sep 28, 2016 at 12:02
  • What 'exact' is not working regarding AVAssetExportSession? Regardless you know it, post it to the question, as it is still unclear though.
    – pedrouan
    Commented Sep 28, 2016 at 12:09
  • Much better... Didn't it return any visible error? The code was just ignored right?
    – pedrouan
    Commented Sep 29, 2016 at 4:39
1

AVAssetExportSession can be NULL, so need to check NULL before work on it https://developer.apple.com/library/content/qa/qa1730/_index.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.