首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAssetExportSession exportAsynchronouslyWithCompletionHandler有时无法工作

AVAssetExportSession exportAsynchronouslyWithCompletionHandler有时无法工作
EN

Stack Overflow用户
提问于 2015-01-19 16:48:59
回答 1查看 907关注 0票数 1

我想从iPod-Library导出和压缩视频,但"exportAsynchronouslyWithCompletionHandler“可以正常工作的一些视频,而不是一些视频的工作,没有抛出异常。但奇怪的是,如果我注释掉"setVideoComposition:“方法,"exportAsynchronouslyWithCompletionHandler”就可以正常工作。下面是我的代码:

代码语言:javascript
复制
AVAsset *_videoAsset = [AVAsset assetWithURL:[NSURL URLWithString:filmElementModel.alassetUrl]];
    CMTime assetTime = [_videoAsset duration];
    AVAssetTrack *avAssetTrack = [[_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    Float64 duration = CMTimeGetSeconds(assetTime);
    AVMutableComposition *avMutableComposition = [AVMutableComposition composition];
    AVMutableCompositionTrack *avMutableCompositionTrack = [avMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    NSError *error = nil;
    [avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0.0f, 30), CMTimeMakeWithSeconds(duration>8.0f?8.0f:duration, 30))
                                                    ofTrack:avAssetTrack
                                                     atTime:kCMTimeZero
                                                      error:&error];
    AVMutableVideoComposition *avMutableVideoComposition = [AVMutableVideoComposition videoComposition];
    avMutableVideoComposition.frameDuration = CMTimeMake(1, 30);


    AVMutableVideoCompositionLayerInstruction *layerInstruciton = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:avMutableComposition.tracks[0]];
    [layerInstruciton setTransform:[[[_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform] atTime:kCMTimeZero];
    [layerInstruciton setOpacity:0.0f atTime:[_videoAsset duration]];

    AVMutableVideoCompositionInstruction *avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [avMutableComposition duration])];

    avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:layerInstruciton];

    if (avAssetTrack.preferredTransform.a) {
        NSLog(@"横向");
        avMutableVideoComposition.renderSize = CGSizeMake(avAssetTrack.naturalSize.width, avAssetTrack.naturalSize.height);
    }else
    {
        avMutableVideoComposition.renderSize = CGSizeMake(avAssetTrack.naturalSize.height, avAssetTrack.naturalSize.width);

    }
    avMutableVideoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];
    // the url for save video
    NSString *outUrlString = ITTPathForBabyShotResource([NSString stringWithFormat:@"%@/%@.mp4",DATA_ENV.userModel.userId,filmElementModel.filmElementId]);
    NSFileManager *fm = [[NSFileManager alloc] init];
    if ([fm fileExistsAtPath:outUrlString]) {
        NSLog(@"video is have. then delete that");
        if ([fm removeItemAtPath:outUrlString error:&error]) {
            NSLog(@"delete is ok");
        }else {
            NSLog(@"delete is no error = %@",error.description);
        }
    }

    CGSize renderSize = CGSizeMake(1280, 720);
    if (MIN(avAssetTrack.naturalSize.width, avAssetTrack.naturalSize.height)<720) {
        renderSize =avAssetTrack.naturalSize;
    }
    long long fileLimite =renderSize.width*renderSize.height*(duration>8.0f?8.0f:duration)/2;


    _avAssetExportSession = [[AVAssetExportSession alloc] initWithAsset:avMutableComposition presetName:AVAssetExportPreset1280x720];
    [_avAssetExportSession setVideoComposition:avMutableVideoComposition];
    [_avAssetExportSession setOutputURL:[NSURL fileURLWithPath:outUrlString]];
    [_avAssetExportSession setOutputFileType:AVFileTypeQuickTimeMovie];
    [_avAssetExportSession setFileLengthLimit: fileLimite];
    [_avAssetExportSession setShouldOptimizeForNetworkUse:YES];
    [_avAssetExportSession exportAsynchronouslyWithCompletionHandler:^(void){
        switch (_avAssetExportSession.status) {
            case AVAssetExportSessionStatusFailed:
            {

            }
                break;
            case AVAssetExportSessionStatusCompleted:
            {


            }
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"export cancelled");
                break;
            case AVAssetExportSessionStatusExporting:
                NSLog(@"AVAssetExportSessionStatusExporting");
                break;
            case AVAssetExportSessionStatusWaiting:
                NSLog(@"AVAssetExportSessionStatusWaiting");
                break;
        }
    }];
    if (_avAssetExportSession.status != AVAssetExportSessionStatusCompleted){
        NSLog(@"Retry export");
    }
EN

回答 1

Stack Overflow用户

发布于 2015-01-22 01:01:26

我解决了这个问题!

代码语言:javascript
复制
[avMutableCompositionTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0.0f, 30), CMTimeMakeWithSeconds(duration>8.0f?8.0f:duration, 30))
                                                    ofTrack:avAssetTrack
                                                     atTime:kCMTimeZero
                                                      error:&error];

我将CMTimeRangeMake(CMTimeMakeWithSeconds(0.0f,30) )替换为CMTimeRangeMake(CMTimeMakeWithSeconds(0.1f,30)。

但我不知道为什么它能正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28020798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档