是否可以使用 AVAssetWriter 完全裁剪视频?

Is it possible fully crop a video with AVAssetWriter?

我正在使用 AVAssetWriter 创建一个视频文件,我想裁剪它并找到 AVVideoCleanApertureKey 的键。这让我可以为视频设置一个视口,看起来就像是在裁剪视频。但实际上并没有,全帧仍然存在于视频文件中,是否使用“裁剪”似乎取决于视图播放器。

那么,有没有办法用 AVAssetWriter 丢弃视口周围的“外部”数据?还是我需要彻底改变我的方法。

这些图像显示相同的视频文件,但只有 QuickTime 关心视口。

我认为问题的直接答案是否定的。所以最后我切换到 AVMutableVideoCompositionAVAssetExportSession 正如网络上的其他答案所建议的那样。

    AVAsset *video = [AVAsset assetWithURL:outputURL];
    AVAssetTrack *assetVideoTrack = [[video tracksWithMediaType:AVMediaTypeVideo] lastObject];
    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:assetVideoTrack];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-self.rect.origin.x, -self.rect.origin.y);
    [layerInstruction setTransform:transform atTime:kCMTimeZero];
    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.layerInstructions = @[layerInstruction];
    instruction.timeRange = assetVideoTrack.timeRange;
    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    
    // 
    videoComposition.renderSize = CGSizeMake(floor(self.rect.size.width / 16) * 16, 
                                             floor(self.rect.size.height / 16) * 16);
    videoComposition.renderScale = 1.0;
    
    videoComposition.frameDuration = CMTimeMake(1, 30);
    videoComposition.instructions = @[instruction];
    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPreset1280x720];
    exportSession.shouldOptimizeForNetworkUse = NO;
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.videoComposition = videoComposition;
    exportSession.outputURL = outputURL2;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
        NSLog(@"done processing video!");
    }];