swiftui视频_swiftui视频播放器实时处理核心图像

本文介绍如何使用SwiftUI创建一个视频播放器,并结合Core Image进行实时视频处理。通过链接提供的Medium文章,了解详细步骤和实现方法。
摘要由CSDN通过智能技术生成

swiftui视频

SwiftUI in iOS 14 introduced native support for playing movie files. The new VideoPlayer control lets you play movie files from URLs or local resources.

iOS 14中的SwiftUI引入了对播放电影文件的本机支持。 新的VideoPlayer控件使您可以从URL或本地资源播放电影文件。

All you need to do is import AVKit and pass the instance of AVPlayer as shown below:

您需要做的就是import AVKit并传递AVPlayer实例,如下所示:

VideoPlayer(player: AVPlayer(url: enter_url_here)

Video Player works great on devices but can cause problems with simulators, especially when loading from a web URL.

Video Player在设备上运行良好,但会导致模拟器出现问题,尤其是从Web URL加载时。

You can also add custom overlay SwiftUI views over the video player. At the time of writing, it isn’t possible to get rid of default playback controls.

您还可以在视频播放器上添加自定义叠加SwiftUI视图。 在撰写本文时,不可能摆脱默认的播放控件。

It’s worth noting that YouTube URLs won’t work in AVPlayer as they are exactly movie files.

值得注意的是,YouTube网址完全是电影文件,因此无法在AVPlayer中使用。

Video Player opens up a door for doing video processing in pure SwiftUI applications. This means you can write a smart AI-based video player in less than 50 lines of code.

Video Player为纯SwiftUI应用程序中的视频处理打开了一扇门。 这意味着您可以用少于50行的代码编写一个基于AI的智能视频播放器。

In the next sections, we’ll see how to apply filters to Video Player and will implement a bunch of Core Image filters to run on movie files in real-time.

在接下来的部分中,我们将看到如何将过滤器应用于Video Player,并将实现一堆Core Image过滤器以在电影文件上实时运行。

如何在SwiftUI视频播放器中访问视频帧 (How to Access Video Frames in SwiftUI Video Player)

The most common way of accessing video frames from the AV player is by using AVPlayerItemVideoOutput. To do so, we’d typically use the CADisplayLink timer to get hold of frames after certain intervals.

从AV播放器访问视频帧的最常见方法是使用AVPlayerItemVideoOutput 。 为此,我们通常使用CADisplayLink计时器在特定间隔后获取帧。

But CADisplayLink doesn’t support native listeners in SwiftUI. And selectors for listening to changes don’t work without bringing UIKit into the scene.

但是CADisplayLinkCADisplayLink中不支持本机侦听器。 如果不将UIKit引入场景,则用于侦听更改的选择器将不起作用。

So, we’d fall onto the other option, which is AVVideoComposition.

因此,我们将使用另一个选项AVVideoComposition

AVVideoComposition contains the pixel buffer of every frame. By setting it on the AVPlayerItem property, we can retrieve frames and apply CIFilter to give the video a totally different look.

AVVideoComposition包含每个帧的像素缓冲区。 通过在AVPlayerItem属性上进行设置,我们可以检索帧并应用CIFilter来赋予视频完全不同的外观。

将CIFilter应用于SwiftUI中的Video Stream (Apply CIFilter to Video Stream in SwiftUI)

Let’s create a new SwiftUI project and add the following contents to it:

让我们创建一个新的SwiftUI项目并向其中添加以下内容:

There are two main things happening in the above code:

上面的代码中发生了两件事:

  • The AVPlayerItem contains videoComposition property over which we’re setting the instance of AVVideoComposition. Inside AVVideoComposition we pass the media property, which is of the type AVAsset.

    AVPlayerItem包含videoComposition属性,通过它我们可以设置AVVideoComposition的实例。 在AVVideoComposition内部,我们传递了media属性,该属性的类型为AVAsset

  • applyingCIFiltersWithHandler is where the image processing of every frame takes place. We’re setting passing the sourceImage into the currently selected CIFilter (which is picked from the SegmentedControl).

    applyingCIFiltersWithHandler是每帧图像处理的地方。 我们正在设置将sourceImage传递到当前选择的CIFilter (从SegmentedControl )。

We ran the above application on a local video and got the following results with the four CIFilters:

我们在本地视频上运行了上述应用程序,并通过四个CIFilters获得了以下结果:

Image for post

You can add more filters and set slider control for using different intensities. Adding a remote video URL works almost as smoothly as using a local one (if the network connection is good).

您可以添加更多滤镜并设置滑块控件以使用不同的强度。 添加远程视频URL的效果几乎与使用本地视频URL一样流畅(如果网络连接良好)。

结论 (Conclusion)

The full source code of this SwiftUI application is available in the GitHub repository.

该SwiftUI应用程序的完整源代码可在GitHub存储库中找到

SwiftUI Video Player has opened the door for many amazing effects, such as applying a Style transfer.

SwiftUI Video Player为许多惊人的效果打开了大门,例如应用了样式转换。

That’s it for this one. Thanks for reading.

这就是它了。 谢谢阅读。

翻译自: https://medium.com/better-programming/swiftui-video-player-real-time-processing-with-core-image-ef0688745c57

swiftui视频

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值