在AVMutableComposition的末尾添加空白视频片段失败

时间:2018-07-10 20:25:55

标签: ios xcode avmutablecomposition

我正在努力使自己发疯。

我有一个AVMutableComposition,正在向其中添加视频和动画层。

在整个合成的结尾,我想显示5秒的JPEG。

我尝试使用insertEmptyTimeRange,但是很显然,在合成结尾处使用此方法会失败。

所以我创建了一个5秒的视频,该视频仅显示黑屏,并将其添加到作品的结尾。

最终目标是将整个视频保存到设备的AVExportSession。

这可以处理我缝合在一起的一定数量的视频,但是经过一定数量后,我会收到一条奇怪的错误消息,提示:

Export failed: Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode" UserInfo={NSLocalizedFailureReason=The media data could not be decoded. It may be damaged., NSLocalizedDescription=Cannot Decode, NSUnderlyingError=0x1c46507d0 {Error Domain=NSOSStatusErrorDomain Code=-16977 "(null)"}}

当我使用AVPlayer播放视频时,一切正常。

另一个线程中的人建议它可能与所使用的时间范围有关。但是他们都很好!

这是存储在数组中的时间的NSLog以及将视频拼接在一起时所累积的totalPlayTime:

composition start **************************
2018-07-10 22:12:03.536511+0200 StoryCatcher[6884:5992609] Part-Zero-Time 0.000000
2018-07-10 22:12:03.536595+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 5.000000
2018-07-10 22:12:03.536736+0200 StoryCatcher[6884:5992609] insert-point 5.000000
2018-07-10 22:12:03.561522+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:03.561652+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:03.565835+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:03.571170+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 316.396667
2018-07-10 22:12:03.692364+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:03.692482+0200 StoryCatcher[6884:5992609] Part-Zero-Time 316.396667
2018-07-10 22:12:03.692524+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 321.396667
2018-07-10 22:12:03.692618+0200 StoryCatcher[6884:5992609] insert-point 321.396667
2018-07-10 22:12:03.704607+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:03.704722+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:03.707101+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:03.709913+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 632.793333
2018-07-10 22:12:03.718035+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:03.718146+0200 StoryCatcher[6884:5992609] Part-Zero-Time 632.793333
2018-07-10 22:12:03.718188+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 637.793333
2018-07-10 22:12:03.718229+0200 StoryCatcher[6884:5992609] insert-point 637.793333
2018-07-10 22:12:03.729603+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:03.729669+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:03.731642+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:03.734172+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 949.190000
2018-07-10 22:12:03.749377+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:03.749457+0200 StoryCatcher[6884:5992609] Part-Zero-Time 949.190000
2018-07-10 22:12:03.749492+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 954.190000
2018-07-10 22:12:03.749530+0200 StoryCatcher[6884:5992609] insert-point 954.190000
2018-07-10 22:12:03.759300+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:03.759373+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:03.761563+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:03.764029+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1265.586667
2018-07-10 22:12:04.161698+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.161809+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1265.586667
2018-07-10 22:12:04.161849+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1270.586667
2018-07-10 22:12:04.162000+0200 StoryCatcher[6884:5992609] insert-point 1270.586667
2018-07-10 22:12:04.172305+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.172389+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.174729+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.177622+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1581.983333
2018-07-10 22:12:04.185607+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.185733+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1581.983333
2018-07-10 22:12:04.185822+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1586.983333
2018-07-10 22:12:04.185866+0200 StoryCatcher[6884:5992609] insert-point 1586.983333
2018-07-10 22:12:04.198139+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.198248+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.200642+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.203660+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 1898.380000
2018-07-10 22:12:04.211604+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.211698+0200 StoryCatcher[6884:5992609] Part-Zero-Time 1898.380000
2018-07-10 22:12:04.211734+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 1903.380000
2018-07-10 22:12:04.211803+0200 StoryCatcher[6884:5992609] insert-point 1903.380000
2018-07-10 22:12:04.221522+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.221591+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.223646+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.226646+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2214.776667
2018-07-10 22:12:04.234286+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.234377+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2214.776667
2018-07-10 22:12:04.234412+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2219.776667
2018-07-10 22:12:04.234478+0200 StoryCatcher[6884:5992609] insert-point 2219.776667
2018-07-10 22:12:04.244362+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.244448+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.246527+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.249212+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2531.173333
2018-07-10 22:12:04.257031+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.257120+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2531.173333
2018-07-10 22:12:04.257155+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2536.173333
2018-07-10 22:12:04.257226+0200 StoryCatcher[6884:5992609] insert-point 2536.173333
2018-07-10 22:12:04.267056+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.267170+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.269269+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.271888+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 2847.570000
2018-07-10 22:12:04.279912+0200 StoryCatcher[6884:5992609] 

composition start **************************
2018-07-10 22:12:04.280009+0200 StoryCatcher[6884:5992609] Part-Zero-Time 2847.570000
2018-07-10 22:12:04.280046+0200 StoryCatcher[6884:5992609] Total-Playtime after title insert 2852.570000
2018-07-10 22:12:04.280622+0200 StoryCatcher[6884:5992609] insert-point 2852.570000
2018-07-10 22:12:04.290581+0200 StoryCatcher[6884:5992609] asset-duration 311.396667
2018-07-10 22:12:04.290669+0200 StoryCatcher[6884:5992609] Setting end time: 311.396667
2018-07-10 22:12:04.293119+0200 StoryCatcher[6884:5992609] Video size 640.000000 364.000000
2018-07-10 22:12:04.295703+0200 StoryCatcher[6884:5992609] Total-Playtime after movie insert 3163.966667
2018-07-10 22:12:04.304021+0200 StoryCatcher[6884:5992609] Transition times (
    "CMTime: {0/600 = 0.000}",
    "CMTime: {3000/600 = 5.000}",
    "CMTime: {189838/600 = 316.397, rounded}",
    "CMTime: {189838/600 = 316.397}",
    "CMTime: {192838/600 = 321.397, rounded}",
    "CMTime: {379676/600 = 632.793, rounded}",
    "CMTime: {379676/600 = 632.793}",
    "CMTime: {382676/600 = 637.793, rounded}",
    "CMTime: {569514/600 = 949.190, rounded}",
    "CMTime: {569514/600 = 949.190}",
    "CMTime: {572514/600 = 954.190, rounded}",
    "CMTime: {759352/600 = 1265.587, rounded}",
    "CMTime: {759352/600 = 1265.587}",
    "CMTime: {762352/600 = 1270.587, rounded}",
    "CMTime: {949190/600 = 1581.983, rounded}",
    "CMTime: {949190/600 = 1581.983}",
    "CMTime: {952190/600 = 1586.983, rounded}",
    "CMTime: {1139028/600 = 1898.380, rounded}",
    "CMTime: {1139028/600 = 1898.380}",
    "CMTime: {1142028/600 = 1903.380, rounded}",
    "CMTime: {1328866/600 = 2214.777, rounded}",
    "CMTime: {1328866/600 = 2214.777}",
    "CMTime: {1331866/600 = 2219.777, rounded}",
    "CMTime: {1518704/600 = 2531.173, rounded}",
    "CMTime: {1518704/600 = 2531.173}",
    "CMTime: {1521704/600 = 2536.173, rounded}",
    "CMTime: {1708542/600 = 2847.570, rounded}",
    "CMTime: {1708542/600 = 2847.570}",
    "CMTime: {1711542/600 = 2852.570, rounded}",
    "CMTime: {1898380/600 = 3163.967, rounded}"
)
2018-07-10 22:12:04.304990+0200 StoryCatcher[6884:5992609] Total Play time 3163.966667
2018-07-10 22:12:04.305050+0200 StoryCatcher[6884:5992609] Credits start 3163.966667
2018-07-10 22:12:04.311304+0200 StoryCatcher[6884:5992609] Credits duration: 5.0
2018-07-10 22:12:04.314987+0200 StoryCatcher[6884:5992609] Total Play time 3168.966667

这是我用来添加空白视频和点数图层的代码:

CMTime creditsDuration = CMTimeMakeWithSeconds(5, 600);
    CMTime creditsFadeOutStart = CMTimeMakeWithSeconds(4.5, 600);
    CMTime creditsStart = totalPlayTime;

    CMTimeRange creditsRange = CMTimeRangeMake(creditsStart, creditsDuration);

    // Insert empty video

    NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_1080p" ofType:@"mov"];
    NSURL *trackUrl = [NSURL fileURLWithPath:path];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:trackUrl options: nil];
    AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *audio = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    totalPlayTime = CMTimeAdd(totalPlayTime, creditsDuration);

    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,creditsDuration) ofTrack:track atTime:creditsStart error:nil];  // Add video
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,creditsDuration) ofTrack:audio atTime:creditsStart error:nil];  // Add audio

    AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

    [passThroughLayer setOpacity:0.0 atTime:creditsStart];

    NSMutableArray *layerInstructions = [[NSMutableArray alloc] init];

    [layerInstructions addObject:passThroughLayer];

    AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    passThroughInstruction.timeRange = creditsRange;

    passThroughInstruction.layerInstructions = [NSArray arrayWithArray:layerInstructions];
    [videoCompositionInstructions addObject:passThroughInstruction];

    UIImage *logo = [UIImage imageNamed:@"endCredit.png"];

    float logoSize = 1.0;

    if(self.isPreview == NO){
        logoSize *= sizeMultiplier;
    }else{
        logoSize *= sizeMultiplier;
    }

    CGImageRef bdImg = CGImageRetain([logo CGImage]);
    CALayer *logoLayer = [CALayer layer];
    logoLayer.contents = (__bridge_transfer id)bdImg;
    logoLayer.bounds = CGRectMake(0, 0, 480 * logoSize, 320 * logoSize);
    logoLayer.anchorPoint = CGPointMake(0.5, 0.5);

    logoLayer.position = CGPointMake(0, 0);

    [self.animationLayer addSublayer:logoLayer];

    float fadeEnd = CMTimeGetSeconds(CMTimeAdd(creditsStart, CMTimeMakeWithSeconds(4, 600)));
    float fadeStart = CMTimeGetSeconds(creditsStart);

    [logoLayer addAnimation:[VideoComposerHelpers fadeInOut:fadeStart end:fadeEnd duration:0.5] forKey:nil];

帮助!!! ;)

同样,它可以正常播放大约8或9个视频,之后,我得到了上述错误消息。因此,随着时间的流逝,似乎累积了某种错误。但是我在计算中找不到任何问题。

0 个答案:

没有答案