iPhone 在视频上进行图像叠加时出现 AVErrorInvalidVideoComposition 错误?

标签 iphone watermark video-editing

我已将找到的 2 个代码块组合起来 here成一个坚实的 block (并使用 Apple Developer Xcode 教程文件验证了该过程)。然而,当我运行它时,我收到错误。它说:

Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)"

知道为什么它会抛出 AVErrorInvalidVideoComposition 错误吗?谢谢! (我是新来的,所以如果您需要更多信息,请告诉我。)

NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];

/// UIImage into CALayer
UIImage *myImage = [UIImage imageNamed:@"Test.png"];
CALayer *aLayer = [CALayer layer];
aLayer.contents = (id)myImage.CGImage;

AVURLAsset* url = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVMutableComposition *videoComposition = [[AVMutableComposition alloc] init];
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];

AVMutableCompositionTrack *compositionVideoTrack = [videoComposition  addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[url tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];

AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition alloc] init];
videoComp.renderSize = CGSizeMake(640, 480);
videoComp.frameDuration = CMTimeMake(1, 30);
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoComp.renderSize.width, videoComp.renderSize.height);
videoLayer.frame = CGRectMake(0, 0, videoComp.renderSize.width, videoComp.renderSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:aLayer];
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];


/// instruction
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];

/// outputs
NSString *filePath = nil;
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [filePath stringByAppendingPathComponent:@"temp.mov"]; 
NSLog(@"exporting to: %@", filePath);
if ([fileManager fileExistsAtPath:filePath]) 
{
    BOOL success = [fileManager removeItemAtPath:filePath error:&error];
    if (!success) NSLog(@"FM error: %@", [error localizedDescription]);
}

/// exporting
AVAssetExportSession *exporter;
exporter = [[AVAssetExportSession alloc] initWithAsset:videoComposition presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComp;
exporter.outputURL=[NSURL fileURLWithPath:filePath];
exporter.outputFileType=AVFileTypeQuickTimeMovie;


[exporter exportAsynchronouslyWithCompletionHandler:^(void){
    switch (exporter.status) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"exporting failed:%@",exporter.error);
            break;
        case AVAssetExportSessionStatusCompleted:
            NSLog(@"exporting completed");
            UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"export cancelled");
            break;
    }
}];

最佳答案

无效作品的一个常见问题是时间。

如果您要组合的各种资源的长度不同,请确保您创建的合成覆盖整个片段。

检查并确保您的 AVMutableVideoCompositionInstruction 的 timeRange 正确。

关于iPhone 在视频上进行图像叠加时出现 AVErrorInvalidVideoComposition 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8786789/

相关文章:

audio - FFMPEG 将视频和音频减半

ffmpeg:是否可以替换可变帧率视频中的帧?

iphone - 多行 UIAlertView 消息崩溃

php - dompdf 水印、图像、页眉和页脚问题

ffmpeg - 我应该如何使用 ffmpeg 在 RTMP h264 流上添加透明的 watermark.png?

java - 如何在 Java 中为图像添加水印?

FFMPEG 脚本用于合并列表中的所有文件并制作 1920x1080p 60fps mp4 视频

ios - 将图像添加到 View (背景)

iphone - 在 View 中使用 UITapGesture 时不调用 UIButton 操作

iphone - 在 iOS 上,为什么 shouldAutorotateToInterfaceOrientation 被调用 10、12 或 13 次?