同时录制覆盖AVFoundation iOS

时间:2013-03-26 15:16:41

标签: iphone ios avfoundation

我现在已经完全设置了使用AVFoundation框架录制视频的功能,这一切都很好,但现在我希望在记录期间添加叠加层(也可以在AVCaptureVideoPreviewLayer图层上看到)

我可以将此叠加UIView对象添加到VideoPreviewLayer,但我正在努力如何在录制的视频上获得相同的视图。此UIView可能包含来自UILabel s的任何内容到UIImageView s。

4 个答案:

答案 0 :(得分:10)

我不确定这是否是您正在寻找的东西,但我想您可以使用Brad Larson的GPU库,有一个名为GPUImageElement的类可以让您添加叠加层和视图。请查看示例,尤其是示例调用Filter showcase并滚动到名为UIElement的东西。

以下是一些示例代码:

 else if (filterType == GPUIMAGE_UIELEMENT)
        {
            GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
            blendFilter.mix = 1.0;

            NSDate *startTime = [NSDate date];

            UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 240.0f, 320.0f)];
            timeLabel.font = [UIFont systemFontOfSize:17.0f];
            timeLabel.text = @"Time: 0.0 s";
            timeLabel.textAlignment = UITextAlignmentCenter;
            timeLabel.backgroundColor = [UIColor clearColor];
            timeLabel.textColor = [UIColor whiteColor];

            uiElementInput = [[GPUImageUIElement alloc] initWithView:timeLabel];

            [filter addTarget:blendFilter];
            [uiElementInput addTarget:blendFilter];

            [blendFilter addTarget:filterView];

            __unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

            [filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
                timeLabel.text = [NSString stringWithFormat:@"Time: %f s", -[startTime timeIntervalSinceNow]];
                [weakUIElementInput update];
            }];
        }

答案 1 :(得分:6)

您希望覆盖UIView,但如果您不介意使用CALayer,则可以  使用AVAssetExportSession的{​​{1}}属性导出后添加叠加层。它有一个属性AVVideoComposition,可让您在输出中添加动画AVVideoCompositionCoreAnimationTool *animationTool,但如果CALayer无法描述叠加层的外观,我认为您运气不好。您的显示标题的示例可能是可能的,尽管我想象的东西就像当前时间计数器那样简单。如果你能忍受这个限制,WWDC 2010代码示例'AVEditDemo'是一个很好的起点。

如果您需要更多控制,可以使用CABasicAnimation手动将覆盖UIView渲染到捕获帧上,然后使用[view.layer renderInContext:contextToThenRenderToFrame]将这些帧写入文件(一旦捕获帧到记忆你不能再使用AVAssetWriter)。

警告:您捕获的帧可能无法达到统一的速率,并且取决于环境照明甚至系统负载。如果叠加层的更改速率高于捕获视频,则需要在第二个解决方案中重复帧。这是由AVCaptureMovieFileOutput在第一个解决方案中为您处理的。

P.S。解决方案二是繁琐的,但没有详细说明,iOS7似乎使这更容易。

答案 2 :(得分:1)

您可以在editing部分

中的AVFoundation编程指南中找到它

有一个部分处理图像的叠加。我将尝试提供一个示例代码/项目。

答案 3 :(得分:0)

要弄清楚你的问题,有关raywenderlich教程网站的两个非常详细的解释。这是一个两部分的教程。

第一个是:http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios

第二个是:http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

祝你好运,希望这有助于!!