合并具有不同分辨率的剪辑

Merging Clips with Different Resolutions

我有一组视频剪辑,我想将它们合并在一起,然后在上面加水印。

我可以单独执行这两个功能,但是同时执行它们时会出现问题。

将合并的所有剪辑都是 1920x1080 或 960x540。

出于某种原因,AVAssetExportSession 不能很好地将它们一起显示。

以下是基于 3 种不同场景的 2 个错误:

这张图片的结果是:

如您所见,这里没有任何问题,输出视频产生了预期的效果。

但是,当我随后尝试添加水印时,它会产生以下问题:

这张图片的结果是:

BUG 1: 视频中的某些剪辑出于某种原因调整了大小,而其他剪辑则没有。

这张图片的结果是:

错误 2 现在需要调整大小的剪辑已调整大小,但是旧的未调整大小的剪辑仍然存在。

Merging/Resizing代码:

-(void) mergeClips{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];      
    
    AVMutableCompositionTrack *mutableVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    
    AVMutableCompositionTrack *mutableAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    // loop through the list of videos and add them to the track
    CMTime currentTime = kCMTimeZero;
    
    NSMutableArray* instructionArray = [[NSMutableArray alloc] init];
    if (_clipsArray){
        for (int i = 0; i < (int)[_clipsArray count]; i++){
            NSURL* url = [_clipsArray objectAtIndex:i];
            
            AVAsset *asset = [AVAsset assetWithURL:url];
            
            AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
            AVAssetTrack *audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
            
            CGSize size = videoTrack.naturalSize;
            CGFloat widthScale = 1920.0f/size.width;
            CGFloat heightScale = 1080.0f/size.height;
            
// lines that performs resizing
            AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableVideoTrack];
            CGAffineTransform scale = CGAffineTransformMakeScale(widthScale,heightScale);
            CGAffineTransform move = CGAffineTransformMakeTranslation(0,0);
            [layerInstruction setTransform:CGAffineTransformConcat(scale, move) atTime:currentTime];
            [instructionArray addObject:layerInstruction];
            
            
            [mutableVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                ofTrack:videoTrack
                                 atTime:currentTime error:nil];
            
            [mutableAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                ofTrack:audioTrack
                                 atTime:currentTime error:nil];
            
            currentTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(asset.duration) + CMTimeGetSeconds(currentTime), asset.duration.timescale);
        }
    }
    
    AVMutableVideoCompositionInstruction * mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, currentTime);
    mainInstruction.layerInstructions = instructionArray;
    
    
    // 4 - Get path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *lastPostedDayPath = [documentsDirectory stringByAppendingPathComponent:@"lastPostedDay"];
    
    //Check if folder exists, if not create folder
    if (![[NSFileManager defaultManager] fileExistsAtPath:lastPostedDayPath]){
        [[NSFileManager defaultManager] createDirectoryAtPath:lastPostedDayPath withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    
    NSString *fileName = [NSString stringWithFormat:@"%li_%li_%li.mov", (long)_month, (long)_day, (long)_year];
    
    NSString *finalDayPath = [lastPostedDayPath stringByAppendingPathComponent:fileName];
    
    NSURL *url = [NSURL fileURLWithPath:finalDayPath];
    
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:finalDayPath];
    if (fileExists){
        NSLog(@"file exists");
        [[NSFileManager defaultManager] removeItemAtURL:url error:nil];
    }
    
    AVMutableVideoComposition *mainComposition = [AVMutableVideoComposition videoComposition];
    
    mainComposition.instructions = [NSArray arrayWithObject:mainInstruction];
    mainComposition.frameDuration = CMTimeMake(1, 30);
    mainComposition.renderSize = CGSizeMake(1920.0f, 1080.0f);
    
    // 5 - Create exporter
    _exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                            presetName:AVAssetExportPresetHighestQuality];
    _exportSession.outputURL=url;
    _exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    _exportSession.shouldOptimizeForNetworkUse = YES;
    _exportSession.videoComposition = mainComposition;
    
    [_exportSession exportAsynchronouslyWithCompletionHandler:^{
        [merge_timer invalidate];
        merge_timer = nil;
        
        switch (_exportSession.status) {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export failed -> Reason: %@, User Info: %@",
                      _exportSession.error.localizedDescription,
                      _exportSession.error.userInfo.description);
                [self showSavingFailedDialog];
                break;
                
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export cancelled");
                [self showSavingFailedDialog];
                
                break;
                
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export finished");
                [self addWatermarkToExportSession:_exportSession];
                
                break;
                
            default:
                break;
        }
    }];
});
}

完成此操作后,我 运行 通过不同的导出会话仅添加水印。

我的代码或过程中有什么地方做错了吗? 有没有更简单的方法来实现这一点?

感谢您的宝贵时间!

我能够解决我的问题。 出于某种原因,AVAssetExportSession 实际上不会创建合并剪辑的 'flat' 视频文件,因此它在添加导致它们调整大小的水印时仍然识别较低分辨率的剪辑及其位置。

我解决这个问题的方法是,首先使用 AVAssetWriter 合并我的剪辑并创建一个 'flat' 文件。然后我可以添加水印而不会出现调整大小问题。

希望这对以后可能遇到此问题的任何人有所帮助!

我也遇到了同样的问题, 您可以像这样在一个视频结束后设置不透明度:

[layerInstruction setOpacity:0.0 atTime:duration];